DJ Remixes
A boat load of incredibly useful ‘plugins’ for Delayed::Job! DJ is a wonderful project and is incredibly useful, however it can be even more useful with just a few extras added in.
The Extras
-
A proper ‘Worker’ class: DJ::Worker that accepts attributes.
-
Callbacks
-
Hoptoad support, if using Hoptoad.
-
Priority settings
-
Automatic re-enqueueing
-
Better scheduling
-
Unique jobs.
-
more …
These are a few of the extras for DJ that are included here.
Installation
In your Gemfile
add the following:
gem 'delayed_job', :git => 'git://github.com/collectiveidea/delayed_job.git'
gem 'dj_remixes', '>= 0.2.0'
Then install the gems:
$ bundle install
NOTE: At this time DJ Remixes requires the latest and greatest in the delayed_job master branch. When DJ 2.1.0 is officially released this requirement should go away.
Using
Basic Worker
class FooWorker < DJ::Worker
def perform
# do work
end
end
FooWorker.enqueue
Unique Worker
Tell DJ to only allow one of this worker at a given time.
# We only want to charge the card once!
class PurchaseWorker < DJ::Worker
is_unique
def perform
# charge the credit card...
end
end
If the worker has an id
attribute that then will be used in conjunction with the class name of the worker to form the unique key.
Priority
Tell DJ to run this worker with a higher priority than others.
class FooWorker < DJ::Worker
priority :high
def perform
# do work
end
end
FooWorker.enqueue
Re-Enqueueing
Tell DJ to re-enqueue this worker after it has successfully completely. NOTE: This will actually create a new DJ object in the database, not reuse the same one.
# Run every 30 days and charge a credit card.
class SubscriptionWorker < DJ::Worker
re_enqueue
def run_at
30.days.from_now
end
def perform
# charge the credit card...
end
end
Attributes
The DJ::Worker
class can accept attributes, similar to the way an ActiveRecord
model can.
class FooWorker < DJ::Worker
priority :high
def perform
# do work
puts self.id # => 1
puts self.person # => 'Mark Bates'
end
end
worker = FooWorker.new(:id => 1, :person => 'Mark Bates')
worker.enqueue!
Contributors
-
Mark Bates
-
Stuart Garner
-
Lars Pind