Skip to content

Commit

Permalink
implemented timeout for job execution
Browse files Browse the repository at this point in the history
  • Loading branch information
beat authored and bkeepers committed Jul 19, 2009
1 parent 079b24c commit a9aea11
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/delayed/job.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'timeout'

module Delayed module Delayed


class DeserializationError < StandardError class DeserializationError < StandardError
Expand Down Expand Up @@ -89,7 +91,7 @@ def run_with_lock(max_run_time, worker_name)


begin begin
runtime = Benchmark.realtime do runtime = Benchmark.realtime do
invoke_job # TODO: raise error if takes longer than max_run_time Timeout.timeout(max_run_time.to_i) { invoke_job }
destroy destroy
end end
# TODO: warn if runtime > max_run_time ? # TODO: warn if runtime > max_run_time ?
Expand Down
11 changes: 11 additions & 0 deletions spec/job_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class ErrorJob
def perform; raise 'did not work'; end def perform; raise 'did not work'; end
end end


class LongRunningJob
def perform; sleep 250; end
end

module M module M
class ModuleJob class ModuleJob
cattr_accessor :runs; self.runs = 0 cattr_accessor :runs; self.runs = 0
Expand Down Expand Up @@ -171,6 +175,13 @@ def perform; @@runs += 1; end
Delayed::Job.destroy_failed_jobs = default Delayed::Job.destroy_failed_jobs = default
end end


it "should fail after MAX_RUN_TIME" do
@job = Delayed::Job.create :payload_object => LongRunningJob.new
Delayed::Job.reserve_and_run_one_job(1.second)
@job.reload.last_error.should =~ /expired/
@job.attempts.should == 1
end

it "should never find failed jobs" do it "should never find failed jobs" do
@job = Delayed::Job.create :payload_object => SimpleJob.new, :attempts => 50, :failed_at => Time.now @job = Delayed::Job.create :payload_object => SimpleJob.new, :attempts => 50, :failed_at => Time.now
Delayed::Job.find_available(1).length.should == 0 Delayed::Job.find_available(1).length.should == 0
Expand Down

0 comments on commit a9aea11

Please sign in to comment.