Symphony::Metronome::

Scheduler class

Manage the delta queue of events and associated actions.

Constants

SIGNALS

Signals the daemon responds to.

Attributes

queue R

The sorted set of ScheduledEvent objects.

Public Class Methods

run( &block )

Create and start an instanced daemon.

# File lib/symphony/metronome/scheduler.rb, line 34
def self::run( &block )
        return new( block )
end

Protected Instance Methods

handle_signal( sig )

Dispatch incoming signals to appropriate handlers.

# File lib/symphony/metronome/scheduler.rb, line 106
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_events()

Process all events that have reached their runtime.

# File lib/symphony/metronome/scheduler.rb, line 124
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 )

                # Reschedule the event and place it back on the queue.
                #
                if ev.event.recurring
                        ev.reset_runtime
                        events_to_add << ev unless rv.nil?

                # It was a single run event, torch it!
                #
                else
                        ev.delete

                end
        end

        events_to_delete.map{|e| self.queue.delete( e ) }
        events_to_add.map{|e| self.queue.add( e ) }
end
start()

Main daemon sleep loop.

# File lib/symphony/metronome/scheduler.rb, line 83
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