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 |
Older versions of Resque stored to Redis form Failure::Base.
Newer versions introduce Failure::Redis and you need to use Failure::Multiple to compose. Let me find an update and post it.
Does this use your ActionMailer SMTP settings? It seems like the rails environment hasn't loaded by this point if you put it in an initializer, but I could be wrong.
It would be nice if there was a way to have this reuse whatever mail settings your app is already using to dry it up.
Ending up using this instead: https://github.com/anandagrawal84/resque_failed_job_mailer
Seemed to work with existing SMTP settings.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When calling super, doesn't that reference Base#save which is an empty method? This code is not storing in Redis for me.