Skip to content

Instantly share code, notes, and snippets.

@cmhobbs
Created August 16, 2011 21:22
Show Gist options
  • Save cmhobbs/1150207 to your computer and use it in GitHub Desktop.
Save cmhobbs/1150207 to your computer and use it in GitHub Desktop.
Resque multiple failure notifier via postmark
# this goes in lib/resque/failure/notifier.rb
require 'resque/failure/multiple'
require 'resque/failure/redis'
require 'postmark'
require 'mail'
module Resque
module Failure
class Notifier < Base
class << self
attr_accessor :smtp, :sender, :recipients
end
def self.configure
yield self
Resque::Failure.backend = self unless Resque::Failure.backend == Resque::Failure::Multiple
end
def save
message = Mail.new
# FIXME stash the api key elsewhere
message.delivery_method(Mail::Postmark, :api_key => "YOUR-SUPER-SECRET-POSTMARK-KEY-HERE")
message.from = self.class.sender
message.to = self.class.recipients
message.subject = "Resque: #{exception}"
message.content_type = "text/html"
# FIXME do something other than a here doc, this is ugly
message.body = <<EOT
Queue: #{queue}
Worker: #{worker}
#{payload.inspect}
#{exception}
#{exception.backtrace.join("\n")}
EOT
# pull the trigger
message.deliver
end
end
end
end
# this goes in lib/tasks/resque.rake
require 'resque/failure/notifier'
require 'resque/failure/multiple'
require 'resque/failure/redis'
ENV['JOBS_PER_FORK'] ||= '1'
namespace :resque do
task :setup => :environment do
Resque.schedule = YAML.load_file("#{Rails.root}/config/resque_schedule.yml")
Resque.after_fork do |job|
ActiveRecord::Base.establish_connection
end
Resque::Failure::Notifier.configure do |config|
config.sender = "notifier@example.com"
config.recipients = %w{user0@example.com user1@example.com user2@example.com}
end
Resque::Failure::Multiple.classes = [Resque::Failure::Redis, Resque::Failure::Notifier]
Resque::Failure.backend = Resque::Failure::Multiple
end
end
@bsodmike
Copy link

Hi Chris, curious as to what's in resque_schedule.yml - thanks!

@cmhobbs
Copy link
Author

cmhobbs commented May 28, 2012

This was for a proprietary project, so I can't copy and paste the exact file, but there was no voodoo in it. It was basically several definitions in the following format:

refresh_network_statistics:                                                                                                                 
   cron: "0 1 * * *"
   class: NetworkRefreshDispatcher
   queue: 'network_refresh_dispatch'
   args: 'refresh_stats'
   description: "This job refreshes statistics for all enabled networks"

I'm not sure how your question is pertinent to the blog post referencing this gist. Were you trying to figure out how to use multiple failure backends?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment