Created
February 1, 2010 00:04
-
-
Save assaf/291329 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "resque" | |
require "resque/failure/multiple" | |
require "resque/failure/redis" | |
# Configure Resque connection from config/redis.yml. This file should look | |
# something like: | |
# development: localhost:6379 | |
# test: localhost:6379:15 | |
# production: localhost:6379 | |
Resque.redis = YAML.load_file(Rails.root + 'config/redis.yml')[Rails.env] | |
module Resque | |
module Failure | |
# Logs failure messages. | |
class Logger < Base | |
def save | |
Rails.logger.error detailed | |
end | |
def detailed | |
<<-EOF | |
#{worker} failed processing #{queue}: | |
Payload: | |
#{payload.inspect.split("\n").map { |l| " " + l }.join("\n")} | |
Exception: | |
#{exception} | |
#{exception.backtrace.map { |l| " " + l }.join("\n")} | |
EOF | |
end | |
end | |
# Emails failure messages. | |
# Note: uses Mail (default in Rails 3.0) not TMail (Rails 2.x). | |
class Notifier < Logger | |
def save | |
text, subject = detailed, "[Error] #{queue}: #{exception}" | |
Mail.deliver do | |
from "error@ ... domain ..." | |
to "... that would be you ..." | |
subject subject | |
text_part do | |
body text | |
end | |
end | |
rescue | |
puts $! | |
end | |
end | |
end | |
end | |
Resque::Failure::Multiple.configure do |multi| | |
# Always stores failure in Redis and writes to log | |
multi.classes = Resque::Failure::Redis, Resque::Failure::Logger | |
# Production/staging only: also email us a notification | |
multi.classes << Resque::Failure::Notifier if Rails.env.production? || Rails.env.staging? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ending up using this instead: https://github.com/anandagrawal84/resque_failed_job_mailer
Seemed to work with existing SMTP settings.