diff --git a/config/initializers/resque.rb b/config/initializers/resque.rb index 79cbd2995..cea1bf977 100644 --- a/config/initializers/resque.rb +++ b/config/initializers/resque.rb @@ -33,3 +33,15 @@ if AppConfig[:mount_resque_web] require File.join(Rails.root, 'lib/admin_rack') Resque::Server.use AdminRack end + +if AppConfig[:hoptoad_api_key].present? + require 'resque/failure/multiple' + require 'resque/failure/hoptoad' + require 'resque/failure/redis' + Resque::Failure::Hoptoad.configure do |config| + config.api_key = AppConfig[:hoptoad_api_key] + config.secure = true + end + Resque::Failure::Multiple.classes = [Resque::Failure::Redis, Resque::Failure::Hoptoad] + Resque::Failure.backend = Resque::Failure::Multiple +end diff --git a/lib/resque_job_logging.rb b/lib/resque_job_logging.rb index 3402f51a1..3771b5ff5 100644 --- a/lib/resque_job_logging.rb +++ b/lib/resque_job_logging.rb @@ -1,52 +1,2 @@ module ResqueJobLogging - def around_perform_log_job(*args) - Rails.logger.auto_flushing=1 - log_string = "event=resque_job job=#{self} " - error = nil - time = Benchmark.realtime{ - begin - yield - rescue Exception => e - error = e - end - }*1000 - - log_string += "ms=#{time} " - args.each_with_index{|arg,idx| log_string += "arg#{idx.succ}=\"#{arg.to_s[0..30]}\" "} - - if error - log_string += "status=error " - log_string << "error_class=#{error.class} error_message='#{error.message}' " - log_string << "orig_error_message='#{error.original_error.message}'" if error.respond_to?(:original_error) - log_string << "annotated_source='#{error.annoted_source_code.to_s}' " if error.respond_to?(:annoted_source_code) - backtrace = application_trace(error) - log_string << "app_backtrace='#{backtrace.join(";")}' " - notify_hoptoad(error, args) if AppConfig[:hoptoad_api_key].present? - else - log_string += "status=complete " - end - - Rails.logger.info(log_string) - raise error if error - end - - def notify_hoptoad(error, job_arguments) - HoptoadNotifier.notify( - :error_class => error.class, - :error_message => error.message, - :backtrace => error.backtrace, - :parameters => { - :job_class => self.name, - :arguments => job_arguments.map!{|a| a.to_s[0..30]}.join(', '), - :controller => "Resque", - :action => self.name - } - ) if Rails.env.production? - end - - def application_trace(error) #copied from ActionDispatch::ShowExceptions - defined?(Rails) && Rails.respond_to?(:backtrace_cleaner) ? - Rails.backtrace_cleaner.clean(error.backtrace, :silent) : - error.backtrace - end end diff --git a/spec/lib/resque_job_logging_spec.rb b/spec/lib/resque_job_logging_spec.rb index 835eef484..2401e94fa 100644 --- a/spec/lib/resque_job_logging_spec.rb +++ b/spec/lib/resque_job_logging_spec.rb @@ -5,39 +5,4 @@ require 'spec_helper' describe ResqueJobLogging do - before do - Rails.stub!(:logger).and_return(mock()) - Rails.logger.should_receive(:auto_flushing=).with(1) - - silence_warnings { Object.const_set("ResqueJobLoggingDummy", Class.new(Object)) } - ResqueJobLoggingDummy.extend(ResqueJobLogging) - end - - after do - Rails.unstub!(:logger) - end - - # http://bugs.joindiaspora.com/issues/741 - it "should enumerate arguments" do - Rails.logger.should_receive(:info).with(/arg1="foo" arg2="bar" arg3="baz"/) - ## pass a nil block, so we can test the .info() output - ResqueJobLoggingDummy.around_perform_log_job("foo", "bar", "baz") {} - end - it 'logs stack traces on failure' do - Rails.logger.should_receive(:info).with(/app_backtrace=/) - error = RuntimeError.new("GRAAAAAAAAAGH") - proc { - ResqueJobLoggingDummy.around_perform_log_job("stuff"){raise error} - }.should raise_error(Regexp.new(error.message)) - end - - it 'notifies hoptoad if the hoptoad api key is set' do - Rails.logger.should_receive(:info) - AppConfig.should_receive(:[]).with(:hoptoad_api_key).and_return("what") - error = RuntimeError.new("GRAAAAAAAAAGH") - ResqueJobLoggingDummy.should_receive(:notify_hoptoad).with(error, ["stuff"]) - proc { - ResqueJobLoggingDummy.around_perform_log_job("stuff"){raise error } - }.should raise_error(Regexp.new(error.message)) - end end