Symphony::Metronome::
Scheduler
class
Manage the delta queue of events and associated actions.
- SIGNALS
Signals the daemon responds to.
- queue R
The sorted set of ScheduledEvent objects.
Create and start an instanced daemon.
def self::run( &block )
return new( block )
end
Protected Instance Methods
Dispatch incoming signals to appropriate handlers.
def handle_signal( sig )
case sig
when :TERM, :INT
@running = false
Process.kill( sig.to_s, @child ) if self.class.listen
when :HUP
@queue = Symphony::Metronome::ScheduledEvent.load
self.queue.each{|ev| ev.fire(&@proc) if ev.event.recurring }
else
self.log.debug "Unhandled signal: %s" % [ sig ]
end
end
Process all events that have reached their runtime.
def process_events
now = Time.now
events_to_delete = []
events_to_add = []
self.queue.each do |ev|
next unless now >= ev.runtime
events_to_delete << ev
rv = ev.fire( &@proc )
if ev.event.recurring
ev.reset_runtime
events_to_add << ev unless rv.nil?
else
ev.delete
end
end
events_to_delete.map{|e| self.queue.delete( e ) }
events_to_add.map{|e| self.queue.add( e ) }
end
Main daemon sleep loop.
def start
$0 = "Metronome%s" % [ self.class.listen ? ' (executor)' : '' ]
@running = true
loop do
wait = nil
if ev = self.queue.first
wait = ev.runtime - Time.now
wait = 0 if wait < 0
self.log.info "Next event in %0.3f second(s) (id: %d)..." % [ wait, ev.id ]
else
self.log.warn "No events scheduled. Waiting indefinitely..."
end
self.process_events unless self.wait_for_signals( wait )
break unless @running
end
end