Skip to content

Commit

Permalink
Merge pull request resque#277 from raykrueger/hoptoad_notifier
Browse files Browse the repository at this point in the history
Fixing/updating the hoptoad backend by using hoptoad_notifier
  • Loading branch information
defunkt committed May 16, 2011
2 parents 8f8449e + c41c9ca commit 084e4df
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 119 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -4,4 +4,5 @@ gemspec

group :test do
gem "rack-test", "~> 0.5"
gem "mocha", "~> 0.9.7"
end
18 changes: 14 additions & 4 deletions Gemfile.lock
@@ -1,7 +1,8 @@
PATH
remote: .
specs:
resque (1.13.0)
resque (1.15.0)
hoptoad_notifier (~> 2.4.9)
json (~> 1.4.6)
redis-namespace (>= 0.10.0)
sinatra (>= 0.9.2)
Expand All @@ -10,23 +11,32 @@ PATH
GEM
remote: http://rubygems.org/
specs:
activesupport (3.0.7)
builder (3.0.0)
hoptoad_notifier (2.4.9)
activesupport
builder
json (1.4.6)
mocha (0.9.7)
rake
rack (1.2.2)
rack-test (0.5.7)
rack (>= 1.0)
redis (2.1.1)
rake (0.8.7)
redis (2.2.0)
redis-namespace (0.10.0)
redis (< 3.0.0)
sinatra (1.2.1)
sinatra (1.2.6)
rack (~> 1.1)
tilt (>= 1.2.2, < 2.0)
tilt (1.2.2)
tilt (1.3)
vegas (0.1.8)
rack (>= 1.0.0)

PLATFORMS
ruby

DEPENDENCIES
mocha (~> 0.9.7)
rack-test (~> 0.5)
resque!
131 changes: 16 additions & 115 deletions lib/resque/failure/hoptoad.rb
@@ -1,6 +1,4 @@
require 'net/https'
require 'builder'
require 'uri'
require 'hoptoad_notifier'

module Resque
module Failure
Expand All @@ -10,130 +8,33 @@ module Failure
#
# require 'resque/failure/hoptoad'
#
# Resque::Failure::Hoptoad.configure do |config|
# config.api_key = 'blah'
# config.secure = true
# Resque::Failure::Multiple.classes = [Resque::Failure::Redis, Resque::Failure::Hoptoad]
# Resque::Failure.backend = Resque::Failure::Multiple
#
# # optional proxy support
# config.proxy_host = 'x.y.z.t'
# config.proxy_port = 8080
#
# # server env support, defaults to RAILS_ENV or RACK_ENV
# config.server_environment = "test"
# end
# Once you've configured resque to use the Hoptoad failure backend,
# you'll want to setup an initializer to configure the Hoptoad.
#
# HoptoadNotifier.configure do |config|
# config.api_key = 'your_key_here'
# end
# For more information see https://github.com/thoughtbot/hoptoad_notifier
class Hoptoad < Base
# From the hoptoad plugin
INPUT_FORMAT = /^([^:]+):(\d+)(?::in `([^']+)')?$/

class << self
attr_accessor :secure, :api_key
attr_accessor :proxy_host, :proxy_port, :proxy_user, :proxy_pass
attr_accessor :server_environment
attr_accessor :host, :port
attr_accessor :http_read_timeout, :http_open_timeout
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 self.configure
yield self
Resque::Failure.backend = self
end

def save
http = use_ssl? ? :https : :http
host = self.class.host || 'hoptoadapp.com'
port = self.class.port
url = URI.parse("#{http}://#{host}:#{port}/notifier_api/v2/notices/")

request = Net::HTTP::Proxy self.class.proxy_host, self.class.proxy_port,
self.class.proxy_user, self.class.proxy_pass
http = request.new(url.host, url.port)
headers = {
'Content-type' => 'text/xml',
'Accept' => 'text/xml, application/xml'
}

http.read_timeout = self.class.http_read_timeout || 5 # seconds
http.open_timeout = self.class.http_open_timeout || 2 # seconds

http.use_ssl = use_ssl?

begin
response = http.post(url.path, xml, headers)
rescue TimeoutError => e
log "Timeout while contacting the Hoptoad server."
end

case response
when Net::HTTPSuccess then
log "Hoptoad Success: #{response.class}"
else
body = response.body if response.respond_to? :body
log "Hoptoad Failure: #{response.class}\n#{body}"
end
HoptoadNotifier.notify_or_ignore(exception,
:parameters => {
:payload_class => payload['class'].to_s,
:payload_args => payload['args'].inspect
}
)
end

def xml
x = Builder::XmlMarkup.new
x.instruct!
x.notice :version=>"2.0" do
x.tag! "api-key", api_key
x.notifier do
x.name "Resqueue"
x.version "0.1"
x.url "http://github.com/defunkt/resque"
end
x.error do
x.tag! "class", exception.class.name
x.message "#{exception.class.name}: #{exception.message}"
x.backtrace do
fill_in_backtrace_lines(x)
end
end
x.request do
x.url queue.to_s
x.component worker.to_s
x.params do
x.var :key=>"payload_class" do
x.text! payload["class"].to_s
end
x.var :key=>"payload_args" do
x.text! payload["args"].to_s
end
end
end
x.tag!("server-environment") do
x.tag!("project-root", "RAILS_ROOT")
x.tag!("environment-name",server_environment)
end

end
end

def fill_in_backtrace_lines(x)
Array(exception.backtrace).each do |unparsed_line|
_, file, number, method = unparsed_line.match(INPUT_FORMAT).to_a
x.line :file => file,:number => number
end
end

def use_ssl?
self.class.secure
end

def api_key
self.class.api_key
end

def server_environment
return self.class.server_environment if self.class.server_environment
defined?(RAILS_ENV) ? RAILS_ENV : (ENV['RACK_ENV'] || 'development')
end
end
end
end
2 changes: 2 additions & 0 deletions resque.gemspec
Expand Up @@ -25,6 +25,8 @@ Gem::Specification.new do |s|
s.add_dependency "vegas", "~> 0.1.2"
s.add_dependency "sinatra", ">= 0.9.2"
s.add_dependency "json", "~> 1.4.6"
s.add_dependency "hoptoad_notifier","~> 2.4.9"


s.description = <<description
Resque is a Redis-backed Ruby library for creating background jobs,
Expand Down
23 changes: 23 additions & 0 deletions test/hoptoad_test.rb
@@ -0,0 +1,23 @@
require 'test_helper'
require 'resque/failure/hoptoad'
require 'mocha'
require 'hoptoad_notifier'

context "Hoptoad" 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}

HoptoadNotifier.expects(:notify_or_ignore).with(
exception,
:parameters => {:payload_class => 'Object', :payload_args => '66'})

backend = Resque::Failure::Hoptoad.new(exception, worker, queue, payload)
backend.save

end

end

0 comments on commit 084e4df

Please sign in to comment.