Skip to content

Instantly share code, notes, and snippets.

@tosch
Created February 9, 2011 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tosch/818100 to your computer and use it in GitHub Desktop.
Save tosch/818100 to your computer and use it in GitHub Desktop.
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