Last active
December 15, 2015 07:49
-
-
Save eric/5226286 to your computer and use it in GitHub Desktop.
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 Scheduler < Rufus::Scheduler::PlainScheduler | |
def initialize(zk, *args) | |
super(*args) | |
@zk_locker = zk.exclusive_locker('scheduler') | |
end | |
def run | |
@zk_locker.with_lock do | |
configure | |
start | |
join | |
end | |
end | |
def configure | |
every '1h' do | |
Resque.enqueue(Jobs::Hourly) | |
end | |
every '1m' do | |
Resque.enqueue(Jobs::Minute) | |
end | |
# ... | |
end | |
protected | |
def log_exception(e) | |
case e | |
when Interrupt, SignalException, SystemExit | |
# nothing | |
raise | |
else | |
ExceptionNotifier.notify(e) | |
end | |
end | |
# If we want to do something before | |
def before_job(job) | |
begin | |
@zk_locker.assert! | |
rescue ZK::Exceptions::LockAssertionFailedError => e | |
ExceptionNotifier.notify(e) | |
# Tell the world to stop | |
stop | |
end | |
# Verify ActiveRecord connections | |
ActiveRecord::Base.connection_handler.verify_active_connections! | |
end | |
def after_job(job) | |
# Let go of the ActiveRecord connection | |
ActiveRecord::Base.clear_active_connections! | |
end | |
end |
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
require 'rufus/scheduler' | |
class Rufus::Scheduler::Job | |
def trigger_block_with_hooks | |
trigger_scheduler_hook(:before_job) | |
trigger_block_without_hooks | |
ensure | |
trigger_scheduler_hook(:after_job) | |
end | |
alias_method_chain :trigger_block, :hooks | |
def trigger_scheduler_hook(method) | |
if @scheduler.respond_to?(method) | |
@scheduler.method(method).arity == 1 ? @scheduler.send(method, self) : @scheduler.send(method) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment