Skip to content

Instantly share code, notes, and snippets.

@assaf
Created February 1, 2010 00:04
Show Gist options
  • Select an option

  • Save assaf/291329 to your computer and use it in GitHub Desktop.

Select an option

Save assaf/291329 to your computer and use it in GitHub Desktop.
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
@ealdent
Copy link
Copy Markdown

ealdent commented Apr 20, 2010

When calling super, doesn't that reference Base#save which is an empty method? This code is not storing in Redis for me.

@assaf
Copy link
Copy Markdown
Author

assaf commented Apr 20, 2010

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.

@barmstrong
Copy link
Copy Markdown

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.

@barmstrong
Copy link
Copy Markdown

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