Sidekiq::PrioritizedQueues
Adds numeric based priorities to your jobs. This is done by monkey patching the following classes:
Sidekiq::ClientSidekiq::StatsSidekiq::QueueSidekiq::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.
Installation
Add this line to your application's Gemfile:
gem 'sidekiq-prioritized_queues'Usage
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.
Priority
There are two ways of specifying priority:
Static
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
endDynamic
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
endThe example above would make sure that VIP accounts get processed first.
Non Prioritized Queues
By default, all queues use priority-based (sorted set) operations. However, you can configure specific queues to use traditional FIFO (list-based) operations by marking them as "non prioritized queues". This is useful when you want certain queues to maintain strict FIFO ordering without priority sorting.
Non prioritized queues are configured in config/sidekiq_prioritized_queues.yml:
non_prioritized_queues:
- webhooks
- notifications
- cleanupWith this configuration:
- Jobs in the
webhooks,notifications, andcleanupqueues will be processed in strict FIFO order - Jobs in all other queues will be processed based on their priority values
- You can still use priority-based and dynamic queue routing for workers, but if they push to a non prioritized queue, the job will be enqueued using FIFO
This is particularly useful for queues where order of execution is critical and you don't want priority-based reordering to affect the processing sequence.
Contributing
- 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