Skip to content

Instantly share code, notes, and snippets.

@dbalatero
Created October 26, 2011 07:17
Show Gist options
  • Save dbalatero/1315670 to your computer and use it in GitHub Desktop.
Save dbalatero/1315670 to your computer and use it in GitHub Desktop.
Provides basic failover for Resque -> MySQL
module Resque
# Provides a Failover to MySQL.
module Failover
def self.extended(base)
base.class_eval do
class << self
alias_method_chain :enqueue, :active_record_failover
alias_method_chain :enqueue_at, :active_record_failover
end
end
end
def enqueue_with_active_record_failover(klass, *args)
begin
enqueue_without_active_record_failover(klass, *args)
rescue *Errno::ALL_EXCEPTIONS
recover_enqueue_failure(klass, nil, *args)
end
end
def enqueue_at_with_active_record_failover(enqueue_at, klass, *args)
begin
enqueue_at_without_active_record_failover(enqueue_at, klass, *args)
rescue *Errno::ALL_EXCEPTIONS
recover_enqueue_failure(klass, enqueue_at, *args)
end
end
private
def recover_enqueue_failure(klass, enqueue_at, *args)
ResqueJob.create!(:klass_name => klass.to_s,
:enqueue_at => enqueue_at,
:args_json => args.to_json)
::Mailer.notify_admins_of_resque_failure(ResqueJob.count).deliver
end
end
end
@dbalatero
Copy link
Author

Usage: Resque.extend(Resque::Failover)

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