diff --git a/lib/resque/failure/airbrake.rb b/lib/resque/failure/airbrake.rb new file mode 100644 index 000000000..d3a90cf25 --- /dev/null +++ b/lib/resque/failure/airbrake.rb @@ -0,0 +1,17 @@ +begin + require 'airbrake' +rescue LoadError + raise "Can't find 'airbrake' gem. Please add it to your Gemfile or install it." +end + +require 'resque/failure/thoughtbot' + +module Resque + module Failure + class Airbrake < Base + include Resque::Failure::Thoughtbot + + @klass = ::Airbrake + end + end +end diff --git a/lib/resque/failure/hoptoad.rb b/lib/resque/failure/hoptoad.rb index 1073eac29..86eb93670 100644 --- a/lib/resque/failure/hoptoad.rb +++ b/lib/resque/failure/hoptoad.rb @@ -4,6 +4,8 @@ raise "Can't find 'hoptoad_notifier' gem. Please add it to your Gemfile or install it." end +require 'resque/failure/thoughtbot' + module Resque module Failure # A Failure backend that sends exceptions raised by jobs to Hoptoad. @@ -23,26 +25,9 @@ module Failure # end # For more information see https://github.com/thoughtbot/hoptoad_notifier class Hoptoad < Base - def self.configure(&block) - Resque::Failure.backend = self - HoptoadNotifier.configure(&block) - end - - def self.count - # We can't get the total # of errors from Hoptoad so we fake it - # by asking Resque how many errors it has seen. - Stat[:failed] - end - - def save - HoptoadNotifier.notify_or_ignore(exception, - :parameters => { - :payload_class => payload['class'].to_s, - :payload_args => payload['args'].inspect - } - ) - end + include Resque::Failure::Thoughtbot + @klass = ::HoptoadNotifier end end end diff --git a/lib/resque/failure/thoughtbot.rb b/lib/resque/failure/thoughtbot.rb new file mode 100644 index 000000000..284fdab74 --- /dev/null +++ b/lib/resque/failure/thoughtbot.rb @@ -0,0 +1,33 @@ +module Resque + module Failure + module Thoughtbot + def self.included(base) + base.extend(ClassMethods) + end + + module ClassMethods + attr_accessor :klass + + def configure(&block) + Resque::Failure.backend = self + klass.configure(&block) + end + + def count + # We can't get the total # of errors from Hoptoad so we fake it + # by asking Resque how many errors it has seen. + Stat[:failed] + end + end + + def save + self.class.klass.notify_or_ignore(exception, + :parameters => { + :payload_class => payload['class'].to_s, + :payload_args => payload['args'].inspect + } + ) + end + end + end +end diff --git a/test/airbrake_test.rb b/test/airbrake_test.rb new file mode 100644 index 000000000..452b0c313 --- /dev/null +++ b/test/airbrake_test.rb @@ -0,0 +1,27 @@ + +require 'test_helper' + +begin + require 'airbrake' +rescue LoadError + warn "Install airbrake gem to run Airbrake tests." +end + +if defined? Airbrake + require 'resque/failure/airbrake' + context "Airbrake" do + test "should be notified of an error" do + exception = StandardError.new("BOOM") + worker = Resque::Worker.new(:test) + queue = "test" + payload = {'class' => Object, 'args' => 66} + + Airbrake.expects(:notify_or_ignore).with( + exception, + :parameters => {:payload_class => 'Object', :payload_args => '66'}) + + backend = Resque::Failure::Airbrake.new(exception, worker, queue, payload) + backend.save + end + end +end diff --git a/test/hoptoad_test.rb b/test/hoptoad_test.rb index 9d9bf17c7..53f8ad62a 100644 --- a/test/hoptoad_test.rb +++ b/test/hoptoad_test.rb @@ -7,6 +7,7 @@ end if defined? HoptoadNotifier + require 'resque/failure/hoptoad' context "Hoptoad" do test "should be notified of an error" do exception = StandardError.new("BOOM")