Skip to content

Commit

Permalink
Adds Airbrake support while continuing backwards compatibility for Ho…
Browse files Browse the repository at this point in the history
…ptoad. Adds tests for Airbrake.
  • Loading branch information
ersatzryan authored and defunkt committed Sep 2, 2011
1 parent 9beb796 commit 2113127
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 19 deletions.
17 changes: 17 additions & 0 deletions 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
23 changes: 4 additions & 19 deletions lib/resque/failure/hoptoad.rb
Expand Up @@ -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.
Expand All @@ -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
33 changes: 33 additions & 0 deletions 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
27 changes: 27 additions & 0 deletions 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
1 change: 1 addition & 0 deletions test/hoptoad_test.rb
Expand Up @@ -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")
Expand Down

0 comments on commit 2113127

Please sign in to comment.