Currently, I am to using sidekiq for background jobs in rails app. Now I want to use Kafka for the same using consumer-producer mechanism. For that I am using ruby-kafka
and producing messages asynchronously.
My approach: In the perform
method of MyNewJob < ApplicationJob
Job I am calling produce
method of Kafka and when consumer
receives the message, I execute the actual piece of code(which was previously executing inside perform method) inside a new class method of MyNewJob
.
It is working as expected. I want to confirm, is it the right approach or not? If not then what should be the right approach?
Pseudo Code:
I will call my job using any of MyNewJob.perform_now
or MyNewJob.perform_later
etc.
my_new_job.rb
class MyNewJob < ApplicationJob
queue_as TestMixin::QUEUE_NAME
rescue_from(App::ServerError) do
retry_job wait: Rails.application.config.retry_interval
end
def perform(paylaod)
#producing message asynchronously
producer = kafka.async_producer
producer.produce(payload, topic: "topic_name")
end
def self.run_job(payload)
#code to executed previously written inside perform method above.
end
end
consumer.rb (pseudo code)
#client consumer topic subscribe logic and then start consuming.
consumer.each do |message|
MyNewJob.run_job(message.payload)
end
I am also thinking of another approach where I skip bringing ApplicationJob
class into existence and directly produce message in Kafka instead of calling Job.
Please confirm.
Edit 1:
I am using ruby-kafka
with asynchronous producer (kafka.async_producer
).