Adds numeric based priorities to your jobs. This is done by monkey patching the following classes:
Sidekiq::Client
Sidekiq::Stats
Sidekiq::Queue
Sidekiq::Job
This gem also adds a new priority based Fetcher, and a middleware that sets priority on the jobs.
WARNING: This changes the type of the queue:<name>
keys. There's no migration helper in place, so the easiest way is to start off with a clean setup.
Add this line to your application's Gemfile:
gem 'sidekiq-prioritized_queues'
Simply having the gem in your Gemfile is enough to get started with prioritized jobs. As a default, priority will be the timestamp at which the job is enqueued, as to simulate the default FIFO order.
There are two ways of specifying priority:
This is useful whenever different workers share the same queue. An example would be:
class SatisfySameDayOrderWorker
include Sidekiq::Worker
sidekiq_options queue: 'orders', priority: 0
def perform(order)
# Do stuff here
end
end
class SatisfyOrderWorker
include Sidekiq::Worker
sidekiq_options queue: 'orders', priority: 1
def perform(order)
# Do stuff here
end
end
For workers on the same queue, the priority
option can take a Proc
which will be given the arguments the job was queued with. As an example:
class HeavyWorker
include Sidekiq::Worker
sidekiq_options priority: -> (account_id) {
Account.find(account_id).vip? ? 0 : 10
}
def perform(account_id)
# Do some work.
end
end
The example above would make sure that VIP accounts get processed first.
- Fork it ( https://github.com/publitas/sidekiq-prioritized_queues/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request