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
31 @timer = Thread.new do
33 # get the current time
36 # iterate over each event and trigger any that are due
37 @events.each do |event|
46 # sleep until next check
52 # Create and register a new TimerEvent with the given parameters
53 def schedule(condition, interval = condition.interval)
54 @events << TimerEvent.new(condition, interval)
55 @events.sort! { |x, y| x.at <=> y.at }
58 # Remove any TimerEvents for the given condition
59 def unschedule(condition)
60 @events.reject! { |x| x.condition == condition }
64 Hub.trigger(event.condition)
67 # Join the timer thread