Rails <3 Disque
This gem provides a simple way to use antirez's Disque as a backend for your Rails ActiveJob workers.
Installation and Setup
Add this gem to your Gemfile.
gem 'rails', '4.2.3'
gem 'activejob_disque_adapter'
And set your Rails application to use it in config/application.rb
(or config/environments/*
, depending on your preference)
module YourApp
class Application < Rails::Application
config.active_job.queue_adapter = :disque
end
end
That's all you need! You can then go on to write standard ActiveJob classes, which will be enqueued to Disque when you call #perform_later
on them.
Running your jobs
This gem also includes a handy class called Disque::ActiveJobWorker
and a rake task that leverages it. In order to run your jobs you can simply call rake disque:work
from your Procfile, like so:
disque_high: DISQUE_QUEUES=urgent rake disque:work
disque_all: DISQUE_QUEUES=urgent,medium,low rake disque:work
Configuration
You can configure the Disque adapter and Worker programatically or via environment variables, this is the list of configurable parameters:
Parameter | ENV Variable | Default Value | Description |
---|---|---|---|
Nodes | DISQUE_NODES | 'localhost:7711' | This is the list of Disque servers to connect to, it can be a single node, a list of comma-separated nodes or an array of nodes (when done programatically) |
Auth | DISQUE_AUTH | '' | Authorization credentials for Disque. |
Timeout | DISQUE_TIMEOUT | '100' | Time in milliseconds that the client will wait for the Disque server to acknowledge and replicate a job |
Cycle | DISQUE_CYCLE | '1000' | The client keeps track of which nodes are providing more jobs, after the amount of operations specified in cycle it tries to connect to the preferred node. |
Queues | DISQUE_QUEUES | 'default' | The list of queues that Disque::ActiveJobWorker will listen to, it can be a single queue name, a list of comma-separated queues or an array of queue names (when done programatically) |
Environment Variables
DISQUE_QUEUES="urgent,medium,low"
DISQUE_NODES="localhost:7711,localhost:8822"
DISQUE_TIMEOUT=200
Programatically
# Keep in mind #run is a blocking action, it will loop through the queues finding jobs to execute.
Disque::ActiveJobWorker.new(
queues: %w(urgent medium)
).run