Created
October 26, 2011 07:17
-
-
Save dbalatero/1315670 to your computer and use it in GitHub Desktop.
Provides basic failover for Resque -> MySQL
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Resque.extend(Resque::Failover)