4 attr_accessor :condition, :at
6 def initialize(condition, interval)
7 self.condition = condition
8 self.at = Time.now.to_i + interval
15 attr_reader :events, :timer
27 # Start the scheduler loop to handle events
32 @timer = Thread.new do
35 # get the current time
38 # iterate over each event and trigger any that are due
42 @events.each do |event|
44 # trigger the event and mark it for removal
48 # events are ordered, so we can bail on first miss
53 # remove all triggered events
54 triggered.each do |event|
59 message = format("Unhandled exception (%s): %s\n%s",
60 e.class, e.message, e.backtrace.join("\n"))
61 applog(nil, :fatal, message)
63 # sleep until next check
70 # Create and register a new TimerEvent with the given parameters
71 def schedule(condition, interval = condition.interval)
73 @events << TimerEvent.new(condition, interval)
74 @events.sort! { |x, y| x.at <=> y.at }
78 # Remove any TimerEvents for the given condition
79 def unschedule(condition)
81 @events.reject! { |x| x.condition == condition }
86 Hub.trigger(event.condition)
89 # Join the timer thread