lib/stalker.rb in stalker-0.4.0 vs lib/stalker.rb in stalker-0.4.1

- old
+ new

@@ -18,10 +18,14 @@ def job(j, &block) @@handlers ||= {} @@handlers[j] = block end + def error(&blk) + @@error_handler = blk + end + class NoJobsDefined < RuntimeError; end class NoSuchJob < RuntimeError; end def prep(jobs=nil) raise NoJobsDefined unless defined?(@@handlers) @@ -60,19 +64,20 @@ rescue Beanstalk::NotConnected => e failed_connection(e) rescue SystemExit raise rescue => e - STDERR.puts exception_message(e) + log_error exception_message(e) job.bury rescue nil log_job_end(name, 'failed') + error_handler.call(e) if error_handler 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)" + log_error exception_message(e) + log_error "*** Failed connection to #{beanstalk_url}" + log_error "*** 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? @@ -95,10 +100,14 @@ def log(msg) puts "[#{Time.now}] #{msg}" end + def log_error(msg) + STDERR.puts msg + end + def beanstalk @@beanstalk ||= Beanstalk::Pool.new([ beanstalk_host_and_port ]) end def beanstalk_url @@ -126,9 +135,14 @@ def all_jobs @@handlers.keys end + def error_handler + @@error_handler + end + def clear! @@handlers = nil + @@error_handler = nil end end