Skip to content

Commit 451a930

Browse files
committed
Fixing/updating the hoptoad backend
The previous version of the hoptoad back end no longer worked. This is due to Hoptoad changing their API somewhere along the way. I've updated the code here to simply use the hoptoad_notifier gem that folks are likely already using in their apps. This puts the maintenance and configuration responsibility into that gem rather than here in resque.
1 parent 491d16d commit 451a930

File tree

1 file changed

+16
-115
lines changed

1 file changed

+16
-115
lines changed

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(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

0 commit comments

Comments
 (0)