lib/stalker.rb in stalker-0.4.2 vs lib/stalker.rb in stalker-0.5.0
- old
+ new
@@ -1,8 +1,9 @@
require 'beanstalk-client'
require 'json'
require 'uri'
+require 'timeout'
module Stalker
extend self
def enqueue(job, args={}, opts={})
@@ -51,16 +52,26 @@
def work(jobs=nil)
prep(jobs)
loop { work_one_job }
end
+ class JobTimeout < RuntimeError; end
+
def work_one_job
job = beanstalk.reserve
name, args = JSON.parse job.body
log_job_begin(name, args)
handler = @@handlers[name]
raise(NoSuchJob, name) unless handler
- handler.call(args)
+
+ begin
+ Timeout::timeout(job.ttr - 1) do
+ handler.call(args)
+ end
+ rescue Timeout::Error
+ raise JobTimeout, "#{name} hit #{job.ttr-1}s timeout"
+ end
+
job.delete
log_job_end(name)
rescue Beanstalk::NotConnected => e
failed_connection(e)
rescue SystemExit