Created
August 16, 2012 13:21
-
-
Save dblock/3370052 to your computer and use it in GitHub Desktop.
A mongoid delayed job observer, used in test environment to kick off any delayed jobs as they are queued.
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
class DelayedJobObserver < Mongoid::Observer | |
observe Delayed::Job | |
class << self | |
attr_accessor :total_processed | |
attr_accessor :total_errors | |
attr_accessor :enabled | |
def enabled? | |
!! DelayedJobObserver.enabled | |
end | |
def enable! | |
DelayedJobObserver.enabled = true | |
end | |
def disable! | |
DelayedJobObserver.enabled = false | |
end | |
def reset! | |
DelayedJobObserver.total_processed = 0 | |
DelayedJobObserver.total_errors = 0 | |
DelayedJobObserver.enable! | |
end | |
end | |
def after_create(delayed_job) | |
begin | |
delayed_job.invoke_job if DelayedJobObserver.enabled? | |
rescue | |
DelayedJobObserver.total_errors += 1 | |
end | |
DelayedJobObserver.total_processed += 1 | |
end | |
end | |
DelayedJobObserver.reset! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that this is now obsolete.
In 4.1.0 they allowed
Delayed::Worker.delay_jobs
to be a proc, see collectiveidea/delayed_job@ce88693.Now, whenever a job is being delayed, they do this:
Our version now selects what to do in specs based on when the job is to run.