Skip to content

Commit 2113127

Browse files
ersatzryandefunkt
authored andcommitted
Adds Airbrake support while continuing backwards compatibility for Hoptoad. Adds tests for Airbrake.
1 parent 9beb796 commit 2113127

File tree

5 files changed

+82
-19
lines changed

5 files changed

+82
-19
lines changed

lib/resque/failure/airbrake.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
begin
2+
require 'airbrake'
3+
rescue LoadError
4+
raise "Can't find 'airbrake' gem. Please add it to your Gemfile or install it."
5+
end
6+
7+
require 'resque/failure/thoughtbot'
8+
9+
module Resque
10+
module Failure
11+
class Airbrake < Base
12+
include Resque::Failure::Thoughtbot
13+
14+
@klass = ::Airbrake
15+
end
16+
end
17+
end

lib/resque/failure/hoptoad.rb

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
raise "Can't find 'hoptoad_notifier' gem. Please add it to your Gemfile or install it."
55
end
66

7+
require 'resque/failure/thoughtbot'
8+
79
module Resque
810
module Failure
911
# A Failure backend that sends exceptions raised by jobs to Hoptoad.
@@ -23,26 +25,9 @@ module Failure
2325
# end
2426
# For more information see https://github.com/thoughtbot/hoptoad_notifier
2527
class Hoptoad < Base
26-
def self.configure(&block)
27-
Resque::Failure.backend = self
28-
HoptoadNotifier.configure(&block)
29-
end
30-
31-
def self.count
32-
# We can't get the total # of errors from Hoptoad so we fake it
33-
# by asking Resque how many errors it has seen.
34-
Stat[:failed]
35-
end
36-
37-
def save
38-
HoptoadNotifier.notify_or_ignore(exception,
39-
:parameters => {
40-
:payload_class => payload['class'].to_s,
41-
:payload_args => payload['args'].inspect
42-
}
43-
)
44-
end
28+
include Resque::Failure::Thoughtbot
4529

30+
@klass = ::HoptoadNotifier
4631
end
4732
end
4833
end

lib/resque/failure/thoughtbot.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module Resque
2+
module Failure
3+
module Thoughtbot
4+
def self.included(base)
5+
base.extend(ClassMethods)
6+
end
7+
8+
module ClassMethods
9+
attr_accessor :klass
10+
11+
def configure(&block)
12+
Resque::Failure.backend = self
13+
klass.configure(&block)
14+
end
15+
16+
def count
17+
# We can't get the total # of errors from Hoptoad so we fake it
18+
# by asking Resque how many errors it has seen.
19+
Stat[:failed]
20+
end
21+
end
22+
23+
def save
24+
self.class.klass.notify_or_ignore(exception,
25+
:parameters => {
26+
:payload_class => payload['class'].to_s,
27+
:payload_args => payload['args'].inspect
28+
}
29+
)
30+
end
31+
end
32+
end
33+
end

test/airbrake_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
require 'test_helper'
3+
4+
begin
5+
require 'airbrake'
6+
rescue LoadError
7+
warn "Install airbrake gem to run Airbrake tests."
8+
end
9+
10+
if defined? Airbrake
11+
require 'resque/failure/airbrake'
12+
context "Airbrake" do
13+
test "should be notified of an error" do
14+
exception = StandardError.new("BOOM")
15+
worker = Resque::Worker.new(:test)
16+
queue = "test"
17+
payload = {'class' => Object, 'args' => 66}
18+
19+
Airbrake.expects(:notify_or_ignore).with(
20+
exception,
21+
:parameters => {:payload_class => 'Object', :payload_args => '66'})
22+
23+
backend = Resque::Failure::Airbrake.new(exception, worker, queue, payload)
24+
backend.save
25+
end
26+
end
27+
end

test/hoptoad_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
end
88

99
if defined? HoptoadNotifier
10+
require 'resque/failure/hoptoad'
1011
context "Hoptoad" do
1112
test "should be notified of an error" do
1213
exception = StandardError.new("BOOM")

0 commit comments

Comments
 (0)