Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push job in specific queue and limit number workers with sidekiq?

I know we can do:

sidekiq_options queue: "Foo"

But in this case it's the Worker which is assigned to only one queue: "Foo".

I need to assign a Job (and not a Worker) in specific queue. With Resque it's easy:

Resque.enqueue_to(queue_name, my_job)

Further, for concurrency problem, i need to limit the number of Worker on each queue at 1.

How can I do this?

like image 994
Matrix Avatar asked Nov 19 '13 19:11

Matrix


People also ask

How Sidekiq works or how would you implement Sidekiq?

Sidekiq is an open-source framework that provides efficient background processing for Ruby applications. It uses Redis as an in-memory data structure to store all of its job and operational data. It's important to be aware that Sidekiq by default doesn't do scheduling, it only executes jobs.

How does Sidekiq queue work?

Sidekiq server process pulls jobs from the queue in Redis and processes them. Like your web processes, Sidekiq boots Rails so your jobs and workers have the full Rails API, including Active Record, available for use. The server will instantiate the worker and call perform with the given arguments.

How many employees does Sidekiq have?

Today Sidekiq uses a default concurrency of 25. These means Sidekiq will spawn 25 worker threads and execute up to 25 jobs concurrently in a process.

Is Sidekiq a queue?

Out-of-the-box Sidekiq uses a single queue named "default," but it's possible to create and use any number of other named queues if there is at least one Sidekiq worker configured to look at every queue.


2 Answers

You might use https://github.com/brainopia/sidekiq-limit_fetch

and then:

Sidekiq::Client.push({
    'class' => GuidePdfWorker,
    'queue' => queue_name,
    'args'  => [my_job]
})
Sidekiq::Queue[queue_name].limit = 1

But you have to declare your queue_name in the config/sidekiq.yml

like image 140
Maxime Avatar answered Nov 11 '22 18:11

Maxime


I have achieved the above said functionality using the sidekiq limit fetch. You can set dynamic queue mode to true and specify the queues in the Sidekiq::Client.push

And set the process limit to the specific queue. We can either set the process limit or we can ignore and simply it process as per the sidekiq default limit

like image 27
logesh Avatar answered Nov 11 '22 20:11

logesh