Skip to content

Commit 084e4df

Browse files
committed
Merge pull request resque#277 from raykrueger/hoptoad_notifier
Fixing/updating the hoptoad backend by using hoptoad_notifier
2 parents 8f8449e + c41c9ca commit 084e4df

File tree

5 files changed

+56
-119
lines changed

5 files changed

+56
-119
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ gemspec
44

55
group :test do
66
gem "rack-test", "~> 0.5"
7+
gem "mocha", "~> 0.9.7"
78
end

Gemfile.lock

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
PATH
22
remote: .
33
specs:
4-
resque (1.13.0)
4+
resque (1.15.0)
5+
hoptoad_notifier (~> 2.4.9)
56
json (~> 1.4.6)
67
redis-namespace (>= 0.10.0)
78
sinatra (>= 0.9.2)
@@ -10,23 +11,32 @@ PATH
1011
GEM
1112
remote: http://rubygems.org/
1213
specs:
14+
activesupport (3.0.7)
15+
builder (3.0.0)
16+
hoptoad_notifier (2.4.9)
17+
activesupport
18+
builder
1319
json (1.4.6)
20+
mocha (0.9.7)
21+
rake
1422
rack (1.2.2)
1523
rack-test (0.5.7)
1624
rack (>= 1.0)
17-
redis (2.1.1)
25+
rake (0.8.7)
26+
redis (2.2.0)
1827
redis-namespace (0.10.0)
1928
redis (< 3.0.0)
20-
sinatra (1.2.1)
29+
sinatra (1.2.6)
2130
rack (~> 1.1)
2231
tilt (>= 1.2.2, < 2.0)
23-
tilt (1.2.2)
32+
tilt (1.3)
2433
vegas (0.1.8)
2534
rack (>= 1.0.0)
2635

2736
PLATFORMS
2837
ruby
2938

3039
DEPENDENCIES
40+
mocha (~> 0.9.7)
3141
rack-test (~> 0.5)
3242
resque!

lib/resque/failure/hoptoad.rb

Lines changed: 16 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
require 'net/https'
2-
require 'builder'
3-
require 'uri'
1+
require 'hoptoad_notifier'
42

53
module Resque
64
module Failure
@@ -10,130 +8,33 @@ module Failure
108
#
119
# require 'resque/failure/hoptoad'
1210
#
13-
# Resque::Failure::Hoptoad.configure do |config|
14-
# config.api_key = 'blah'
15-
# config.secure = true
11+
# Resque::Failure::Multiple.classes = [Resque::Failure::Redis, Resque::Failure::Hoptoad]
12+
# Resque::Failure.backend = Resque::Failure::Multiple
1613
#
17-
# # optional proxy support
18-
# config.proxy_host = 'x.y.z.t'
19-
# config.proxy_port = 8080
20-
#
21-
# # server env support, defaults to RAILS_ENV or RACK_ENV
22-
# config.server_environment = "test"
23-
# end
14+
# Once you've configured resque to use the Hoptoad failure backend,
15+
# you'll want to setup an initializer to configure the Hoptoad.
16+
#
17+
# HoptoadNotifier.configure do |config|
18+
# config.api_key = 'your_key_here'
19+
# end
20+
# For more information see https://github.com/thoughtbot/hoptoad_notifier
2421
class Hoptoad < Base
25-
# From the hoptoad plugin
26-
INPUT_FORMAT = /^([^:]+):(\d+)(?::in `([^']+)')?$/
27-
28-
class << self
29-
attr_accessor :secure, :api_key
30-
attr_accessor :proxy_host, :proxy_port, :proxy_user, :proxy_pass
31-
attr_accessor :server_environment
32-
attr_accessor :host, :port
33-
attr_accessor :http_read_timeout, :http_open_timeout
34-
end
3522

3623
def self.count
3724
# We can't get the total # of errors from Hoptoad so we fake it
3825
# by asking Resque how many errors it has seen.
3926
Stat[:failed]
4027
end
4128

42-
def self.configure
43-
yield self
44-
Resque::Failure.backend = self
45-
end
46-
4729
def save
48-
http = use_ssl? ? :https : :http
49-
host = self.class.host || 'hoptoadapp.com'
50-
port = self.class.port
51-
url = URI.parse("#{http}://#{host}:#{port}/notifier_api/v2/notices/")
52-
53-
request = Net::HTTP::Proxy self.class.proxy_host, self.class.proxy_port,
54-
self.class.proxy_user, self.class.proxy_pass
55-
http = request.new(url.host, url.port)
56-
headers = {
57-
'Content-type' => 'text/xml',
58-
'Accept' => 'text/xml, application/xml'
59-
}
60-
61-
http.read_timeout = self.class.http_read_timeout || 5 # seconds
62-
http.open_timeout = self.class.http_open_timeout || 2 # seconds
63-
64-
http.use_ssl = use_ssl?
65-
66-
begin
67-
response = http.post(url.path, xml, headers)
68-
rescue TimeoutError => e
69-
log "Timeout while contacting the Hoptoad server."
70-
end
71-
72-
case response
73-
when Net::HTTPSuccess then
74-
log "Hoptoad Success: #{response.class}"
75-
else
76-
body = response.body if response.respond_to? :body
77-
log "Hoptoad Failure: #{response.class}\n#{body}"
78-
end
30+
HoptoadNotifier.notify_or_ignore(exception,
31+
:parameters => {
32+
:payload_class => payload['class'].to_s,
33+
:payload_args => payload['args'].inspect
34+
}
35+
)
7936
end
8037

81-
def xml
82-
x = Builder::XmlMarkup.new
83-
x.instruct!
84-
x.notice :version=>"2.0" do
85-
x.tag! "api-key", api_key
86-
x.notifier do
87-
x.name "Resqueue"
88-
x.version "0.1"
89-
x.url "http://github.com/defunkt/resque"
90-
end
91-
x.error do
92-
x.tag! "class", exception.class.name
93-
x.message "#{exception.class.name}: #{exception.message}"
94-
x.backtrace do
95-
fill_in_backtrace_lines(x)
96-
end
97-
end
98-
x.request do
99-
x.url queue.to_s
100-
x.component worker.to_s
101-
x.params do
102-
x.var :key=>"payload_class" do
103-
x.text! payload["class"].to_s
104-
end
105-
x.var :key=>"payload_args" do
106-
x.text! payload["args"].to_s
107-
end
108-
end
109-
end
110-
x.tag!("server-environment") do
111-
x.tag!("project-root", "RAILS_ROOT")
112-
x.tag!("environment-name",server_environment)
113-
end
114-
115-
end
116-
end
117-
118-
def fill_in_backtrace_lines(x)
119-
Array(exception.backtrace).each do |unparsed_line|
120-
_, file, number, method = unparsed_line.match(INPUT_FORMAT).to_a
121-
x.line :file => file,:number => number
122-
end
123-
end
124-
125-
def use_ssl?
126-
self.class.secure
127-
end
128-
129-
def api_key
130-
self.class.api_key
131-
end
132-
133-
def server_environment
134-
return self.class.server_environment if self.class.server_environment
135-
defined?(RAILS_ENV) ? RAILS_ENV : (ENV['RACK_ENV'] || 'development')
136-
end
13738
end
13839
end
13940
end

resque.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Gem::Specification.new do |s|
2525
s.add_dependency "vegas", "~> 0.1.2"
2626
s.add_dependency "sinatra", ">= 0.9.2"
2727
s.add_dependency "json", "~> 1.4.6"
28+
s.add_dependency "hoptoad_notifier","~> 2.4.9"
29+
2830

2931
s.description = <<description
3032
Resque is a Redis-backed Ruby library for creating background jobs,

test/hoptoad_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'test_helper'
2+
require 'resque/failure/hoptoad'
3+
require 'mocha'
4+
require 'hoptoad_notifier'
5+
6+
context "Hoptoad" do
7+
8+
test "should be notified of an error" do
9+
exception = StandardError.new("BOOM")
10+
worker = Resque::Worker.new(:test)
11+
queue = "test"
12+
payload = {'class' => Object, 'args' => 66}
13+
14+
HoptoadNotifier.expects(:notify_or_ignore).with(
15+
exception,
16+
:parameters => {:payload_class => 'Object', :payload_args => '66'})
17+
18+
backend = Resque::Failure::Hoptoad.new(exception, worker, queue, payload)
19+
backend.save
20+
21+
end
22+
23+
end

0 commit comments

Comments
 (0)