Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of git@github.com:keithmgould/delayed_job

  • Loading branch information...
commit 238fd95e90de2d8c4b110236a41513c14bdbe889 2 parents 6ea6d4a + 71b77b3
keith@dailymugshot.com authored
Showing with 44 additions and 38 deletions.
  1. +4 −4 lib/delayed/worker.rb
  2. +40 −34 spec/worker_spec.rb
View
8 lib/delayed/worker.rb
@@ -12,7 +12,7 @@ class Worker
def initialize(options={})
@quiet = options[:quiet]
- @idle_after = options[:idle_after] || 1
+ @idle_after = options[:idle_after] || 60
Delayed::Job.min_priority = options[:min_priority] if options.has_key?(:min_priority)
Delayed::Job.max_priority = options[:max_priority] if options.has_key?(:max_priority)
end
@@ -23,7 +23,7 @@ def start
trap('TERM') { say 'Exiting...'; $exit = true }
trap('INT') { say 'Exiting...'; $exit = true }
- @next_idle = Time.new + @idle_after.minute
+ @next_idle = Time.new + @idle_after
loop do
result = nil
@@ -39,12 +39,12 @@ def start
if count.zero?
sleep(SLEEP)
else
- @next_idle = Time.new + @idle_after.minute
+ @next_idle = Time.new + @idle_after
say "#{count} jobs processed at %.4f j/s, %d failed ..." % [count / realtime, result.last]
end
if Time.new > @next_idle
- @next_idle = Time.new + @idle_after.minute
+ @next_idle = Time.new + @idle_after
on_idle
end
View
74 spec/worker_spec.rb
@@ -9,46 +9,52 @@ def perform; puts "I'm being run"; end
Delayed::Job.delete_all
end
- it "should have a default idle_after time of 1 minute" do
- w = Delayed::Worker.new
- w.idle_after.should == 1
- end
+ describe "#idle_after" do
+ it "should default to 1 minute" do
+ w = Delayed::Worker.new
+ w.idle_after.should == 1.minute
+ end
- it "should have an over-ridable idle_after time" do
- w = Delayed::Worker.new(:idle_after => 2)
- w.idle_after.should == 2
+ it "should be over-ridable" do
+ w = Delayed::Worker.new(:idle_after => 2.minutes)
+ w.idle_after.should == 2.minutes
+ end
end
- it "should start with next_idle = Time.new + idle_after" do
- worker = Delayed::Worker.new
- worker_thread = Thread.new { worker.start }
- expected_next_idle = Time.new + worker.idle_after.minute
- next_idle = worker.next_idle
- sleep(2)
- Thread.kill(worker_thread)
- next_idle.should be_close(expected_next_idle, 2)
- end
+ describe "#next_idle" do
+ it "should equal Time.now + idle_after" do
+ worker = Delayed::Worker.new
+ worker_thread = Thread.new { worker.start }
+ expected_next_idle = Time.new + worker.idle_after
+ next_idle = worker.next_idle
+ sleep(2)
+ Thread.kill(worker_thread)
+ next_idle.should be_close(expected_next_idle, 2)
+ end
- it "should update next_idle after we return from Delayed::Job.work_off" do
- worker = Delayed::Worker.new
- worker_thread = Thread.new { worker.start }
- next_idle_start = worker.next_idle
- sleep(2)
- Delayed::Job.enqueue SimpleJob.new
- sleep(10)
- next_idle_after = worker.next_idle
- Thread.kill(worker_thread)
- next_idle_after.should > next_idle_start
+ it "should update after we return from a job" do
+ worker = Delayed::Worker.new
+ worker_thread = Thread.new { worker.start }
+ next_idle_start = worker.next_idle
+ sleep(2)
+ Delayed::Job.enqueue SimpleJob.new
+ sleep(10)
+ next_idle_after = worker.next_idle
+ Thread.kill(worker_thread)
+ next_idle_after.should > next_idle_start
+ end
end
- it "should call the on_idle method if Time.new > next_idle" do
- Delayed::Worker.class_eval "def on_idle; @test_var = true; end"
- worker = Delayed::Worker.new(:idle_after => 0.05)
- worker_thread = Thread.new { worker.start }
- worker.instance_variable_defined?(:@test_var).should == false
- sleep(10)
- worker.instance_variable_defined?(:@test_var).should == true
- Thread.kill(worker_thread)
+ describe "#on_idle" do
+ it "should be called when Time.now > next_idle" do
+ Delayed::Worker.class_eval "def on_idle; @test_var = true; end"
+ worker = Delayed::Worker.new(:idle_after => 5)
+ worker_thread = Thread.new { worker.start }
+ worker.instance_variable_defined?(:@test_var).should == false
+ sleep(10)
+ worker.instance_variable_defined?(:@test_var).should == true
+ Thread.kill(worker_thread)
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.