How to create sidekiq worker with custom queue

Posted by : on

Category : Rails

Hello Friends, I am back with new real programming problem. In general practice when we are working with sidekiq and delayed jobs. The problem occurs when we want custom queue with custom sidekiq workers. In this blog I am going to discuss the same with an example Here we go:

Create Sidekiq worker:

You can create sidekiq worker class if you want that the jobs will perform by this class. This is not mandatory. Create a worker class inside app/worker/worker_name.rb.

class WorkerName

  include Sidekiq::Worker
  sidekiq_options queue: 'YOUR_QUEUE_NAME' 
  # you can add queue name here or just pass it as param when calling perform method

  def perform(params)
   # your code to perform in delayed jobs
  end
end

Now you have a worker class that will pick the jobs from queue YOUR_QUEUE_NAME.

Add queue name inside config/sidekiq.yml file:

Add YOUR_QUEUE_NAME inside config/sidekiq.yml file with weight. As like:

---
:concurrency: 4
staging:
  :concurrency: 4
production:
  :concurrency: 10
:queues:
  - [queue1, 1]
  - [queue2, 2]
  - [YOUR_QUEUE_NAME, 3]
  - default

Sidekiq check high weight queue first.

Adding sidekiq process to the queue

If you want to add dedicated sidekiq worker instance to the particular queue, so that This worker instance can handle jobs from the queueYOUR_QUEUE_NAME, then you can just add your Procfile

  • sidekiq -q YOUR_QUEUE_NAME

remove YOUR_QUEUE_NAME from your config/sidekiq.yml file, Otherwise other instance will also start executing jobs from YOUR_QUEUE_NAME queue.

  • sidekiq -C config/sidekiq.yml

To start sidekiq process for other queues. Thanks for reading for more documentation, Please refer the link



About Ram Laxman Yadav
Ram Laxman Yadav

Senior Software Engineering Professional | Tech Enthusiast | Mentor | Payments | Hospitality | E-Commerce, based in NCR, India

Email : info@ramlaxman.co.in

Website : https://ramlaxman.co.in