Skip to content

Instantly share code, notes, and snippets.

@tosch

tosch/scheduler.rb

Created Feb 9, 2011
Embed
What would you like to do?
class Scheduler
def initialize
end
def start
@scheduler = Rufus::Scheduler.start_new
['TERM', 'INT'].each do |signal|
Signal.trap(signal) { stop }
end
Signal.trap('HUP') { reload_schedules }
load_schedules
@scheduler.join
end
def stop
@scheduler.stop if @scheduler
puts "Exiting scheduler" if Rails.env == 'development'
Kernel.exit
end
def load_schedules
return false unless @scheduler
puts "Loading schedules" if Rails.env == 'development'
BackupSettings.schedule_backups(@scheduler)
puts "Finished loading schedules" if Rails.env == 'development'
true
end
def unload_schedules
return false unless @scheduler
@scheduler.all_jobs.each {|job| @scheduler.unschedule(job) }
end
def reload_schedules
unload_schedules
load_schedules
end
class << self
#
# sends SIGHUP to a running instance of the scheduler
#
# expects the pid file in log/scheduler.rb.pid
#
def reload_schedules_in_running_instances
pid_file = File.join(Rails.root, 'log', 'schedule.rb.pid')
return unless File.exists?(pid_file) && File.readable?(pid_file)
pid = File.open(pid_file)
begin
pid = File.open(pid_file) {|f|
f.gets.to_i
}
rescue ::Exception
return
end
Process.kill('HUP', pid)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.