4 attr_accessor :condition, :at
6 # Instantiate a new TimerEvent that will be triggered after the specified delay
7 # +condition+ is the Condition
8 # +delay+ is the number of seconds from now at which to trigger
11 def initialize(condition, delay)
12 self.condition = condition
13 self.at = Time.now.to_i + delay
20 attr_reader :events, :timer
24 # Get the singleton Timer
31 # Reset the singleton Timer so the next call to Timer.get will
39 # Instantiate a new Timer and start the scheduler loop to handle events
46 @timer = Thread.new do
49 # get the current time
52 # iterate over each event and trigger any that are due
56 @events.each do |event|
58 # trigger the event and mark it for removal
62 # events are ordered, so we can bail on first miss
67 # remove all triggered events
68 triggered.each do |event|
73 message = format("Unhandled exception (%s): %s\n%s",
74 e.class, e.message, e.backtrace.join("\n"))
75 applog(nil, :fatal, message)
77 # sleep until next check
84 # Create and register a new TimerEvent
85 # +condition+ is the Condition
86 # +delay+ is the number of seconds to delay (default: interval defined in condition)
89 def schedule(condition, delay = condition.interval)
91 @events << TimerEvent.new(condition, delay)
92 @events.sort! { |x, y| x.at <=> y.at }
96 # Remove any TimerEvents for the given condition
97 # +condition+ is the Condition
100 def unschedule(condition)
101 @mutex.synchronize do
102 @events.reject! { |x| x.condition == condition }
106 # Trigger the event's condition to be evaluated
107 # +event+ is the TimerEvent to trigger
111 Hub.trigger(event.condition)
114 # Join the timer thread