Permalink
Browse files

implemented timeout for job execution

  • Loading branch information...
1 parent 079b24c commit a9aea116456087acebde8a28ba6ae4edb692d14e beat committed with bkeepers Mar 23, 2009
Showing with 14 additions and 1 deletion.
  1. +3 −1 lib/delayed/job.rb
  2. +11 −0 spec/job_spec.rb
View
@@ -1,3 +1,5 @@
+require 'timeout'
+
module Delayed
class DeserializationError < StandardError
@@ -89,7 +91,7 @@ def run_with_lock(max_run_time, worker_name)
begin
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
end
# TODO: warn if runtime > max_run_time ?
View
@@ -10,6 +10,10 @@ class ErrorJob
def perform; raise 'did not work'; end
end
+class LongRunningJob
+ def perform; sleep 250; end
+end
+
module M
class ModuleJob
cattr_accessor :runs; self.runs = 0
@@ -171,6 +175,13 @@ def perform; @@runs += 1; end
Delayed::Job.destroy_failed_jobs = default
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
@job = Delayed::Job.create :payload_object => SimpleJob.new, :attempts => 50, :failed_at => Time.now
Delayed::Job.find_available(1).length.should == 0

0 comments on commit a9aea11

Please sign in to comment.