lib/stalker.rb in stalker-0.2.1 vs lib/stalker.rb in stalker-0.2.2
- old
+ new
@@ -33,10 +33,12 @@
jobs.each { |job| beanstalk.watch(job) }
loop do
work_one_job
end
+ rescue Beanstalk::NotConnected => e
+ failed_connection(e)
end
def work_one_job
job = beanstalk.reserve
name, args = JSON.parse job.body
@@ -44,15 +46,27 @@
handler = @@handlers[name]
raise(NoSuchJob, name) unless handler
handler.call(args)
job.delete
log_job_end(name)
+ rescue Beanstalk::NotConnected => e
+ failed_connection(e)
+ rescue SystemExit
+ raise
rescue => e
STDERR.puts exception_message(e)
- job.bury
+ job.bury rescue nil
+ log_job_end(name, 'failed')
end
+ def failed_connection(e)
+ STDERR.puts exception_message(e)
+ STDERR.puts "*** Failed connection to #{beanstalk_url}"
+ STDERR.puts "*** Check that beanstalkd is running (or set a different BEANSTALK_URL)"
+ exit 1
+ end
+
def log_job_begin(name, args)
args_flat = unless args.empty?
'(' + args.inject([]) do |accum, (key,value)|
accum << "#{key}=#{value}"
end.join(' ') + ')'
@@ -62,13 +76,13 @@
log [ "->", name, args_flat ].join(' ')
@job_begun = Time.now
end
- def log_job_end(name)
+ def log_job_end(name, type="finished")
ellapsed = Time.now - @job_begun
ms = (ellapsed.to_f * 1000).to_i
- log "-> #{name} finished in #{ms}ms"
+ log "-> #{name} #{type} in #{ms}ms"
end
def log(msg)
puts "[#{Time.now}] #{msg}"
end