Skip to content

Commit

Permalink
Added Delay::Worker.delay_jobs configuration to allow delayed job to …
Browse files Browse the repository at this point in the history
…operate in a test mode. When delay_jobs=false, all jobs are executed realtime; delay_jobs=true gives normal behavior with database backend.
  • Loading branch information
edwinv committed Dec 28, 2010
1 parent 411719b commit b8a0cc5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ Delayed::Worker.destroy_failed_jobs = false. The failed jobs will be marked with

By default all jobs are scheduled with priority = 0, which is top priority. You can change this by setting Delayed::Worker.default_priority to something else. Lower numbers have higher priority.

It is possible to disable delayed jobs for testing purposes. Set Delayed::Worker.delay_jobs = false to execute all jobs realtime.

Here is an example of changing job parameters in Rails:

<pre>
Expand All @@ -214,6 +216,7 @@ Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.sleep_delay = 60
Delayed::Worker.max_attempts = 3
Delayed::Worker.max_run_time = 5.minutes
Delayed::Worker.delay_jobs = !Rails.env.test?
</pre>

h3. Cleaning up
Expand Down
7 changes: 6 additions & 1 deletion lib/delayed/message_sending.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ def initialize(payload_class, target, options)
end

def method_missing(method, *args)
Job.enqueue({:payload_object => @payload_class.new(@target, method.to_sym, args)}.merge(@options))
payload_object = @payload_class.new(@target, method.to_sym, args)
if Delayed::Worker.delay_jobs
Job.enqueue({:payload_object => payload_object}.merge(@options))
else
payload_object.perform
end
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/delayed/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

module Delayed
class Worker
cattr_accessor :min_priority, :max_priority, :max_attempts, :max_run_time, :default_priority, :sleep_delay, :logger
cattr_accessor :min_priority, :max_priority, :max_attempts, :max_run_time, :default_priority, :sleep_delay, :logger, :delay_jobs
self.sleep_delay = 5
self.max_attempts = 25
self.max_run_time = 4.hours
self.default_priority = 0
self.delay_jobs = true

# By default failed jobs are destroyed after too many attempts. If you want to keep them around
# (perhaps to inspect the reason for the failure), set this to false.
Expand Down
27 changes: 27 additions & 0 deletions spec/message_sending_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,32 @@ def spin
job.run_at.should == run_at
job.priority.should == 20
end

class FairyTail
attr_accessor :happy_ending
def tell
@happy_ending = true
end
end

it "should not delay the job when delay_jobs is false" do
Delayed::Worker.delay_jobs = false
fairy_tail = FairyTail.new
lambda {
lambda {
fairy_tail.delay.tell
}.should change(fairy_tail, :happy_ending).from(nil).to(true)
}.should_not change { Delayed::Job.count }
end

it "should delay the job when delay_jobs is true" do
Delayed::Worker.delay_jobs = true
fairy_tail = FairyTail.new
lambda {
lambda {
fairy_tail.delay.tell
}.should_not change(fairy_tail, :happy_ending)
}.should change { Delayed::Job.count }.by(1)
end
end
end

0 comments on commit b8a0cc5

Please sign in to comment.