Class: ZMQMachine::Timer
- Inherits:
-
Object
- Object
- ZMQMachine::Timer
- Includes:
- Comparable
- Defined in:
- lib/zm/timers.rb
Overview
Used to track the specific expiration time and execution code for each timer.
This should never be instantiated directly by user code. A timer must be known to the #Reactor in which it is running, so use the #Reactor#oneshot_timer and #Reactor#periodical_timer convenience methods. It ensures that new timers are installed in the correct #Reactor.
Instance Attribute Summary collapse
-
#fire_time ⇒ Object
readonly
Returns the value of attribute fire_time.
-
#timer_proc ⇒ Object
readonly
Returns the value of attribute timer_proc.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#cancel ⇒ Object
Cancels this timer from firing.
-
#expired?(time) ⇒ Boolean
True when the timer should be fired; false otherwise.
-
#fire ⇒ Object
Executes the callback.
-
#initialize(opts) ⇒ Timer
constructor
delay
is in milliseconds. - #inspect ⇒ Object
-
#periodical? ⇒ Boolean
True when this is a periodical timer; false otherwise.
- #reschedule ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(opts) ⇒ Timer
delay
is in milliseconds
278 279 280 281 282 283 284 285 |
# File 'lib/zm/timers.rb', line 278 def initialize opts @timers = opts[:timers] @delay = opts[:delay].to_i @periodical = opts[:periodical] @timer_proc = opts[:timer_proc] @exact_time = opts[:exact_time] schedule_firing_time end |
Instance Attribute Details
#fire_time ⇒ Object (readonly)
Returns the value of attribute fire_time.
274 275 276 |
# File 'lib/zm/timers.rb', line 274 def fire_time @fire_time end |
#timer_proc ⇒ Object (readonly)
Returns the value of attribute timer_proc.
274 275 276 |
# File 'lib/zm/timers.rb', line 274 def timer_proc @timer_proc end |
Instance Method Details
#<=>(other) ⇒ Object
304 305 306 |
# File 'lib/zm/timers.rb', line 304 def <=>(other) @fire_time <=> other.fire_time end |
#==(other) ⇒ Object
308 309 310 311 312 313 314 |
# File 'lib/zm/timers.rb', line 308 def ==(other) # need a more specific equivalence test since multiple timers could be # scheduled to go off at exactly the same time @fire_time == other.fire_time && @timer_proc == other.timer_proc && periodical? == other.periodical? end |
#cancel ⇒ Object
Cancels this timer from firing.
300 301 302 |
# File 'lib/zm/timers.rb', line 300 def cancel @timers.cancel self end |
#expired?(time) ⇒ Boolean
True when the timer should be fired; false otherwise.
318 319 320 321 |
# File 'lib/zm/timers.rb', line 318 def expired? time time ||= Timers.now time >= @fire_time end |
#fire ⇒ Object
Executes the callback.
Returns true
when the timer is a one-shot; Returns false
when the timer is periodical and has rescheduled itself.
293 294 295 296 |
# File 'lib/zm/timers.rb', line 293 def fire schedule_firing_time if @periodical @timer_proc.call end |
#inspect ⇒ Object
341 |
# File 'lib/zm/timers.rb', line 341 def inspect; to_s; end |
#periodical? ⇒ Boolean
True when this is a periodical timer; false otherwise.
325 326 327 |
# File 'lib/zm/timers.rb', line 325 def periodical? @periodical end |
#reschedule ⇒ Object
329 330 331 |
# File 'lib/zm/timers.rb', line 329 def reschedule schedule_firing_time end |
#to_s ⇒ Object
333 334 335 336 337 338 339 |
# File 'lib/zm/timers.rb', line 333 def to_s ftime = Time.at(@fire_time / 1000) fdelay = @fire_time - Timers.now name = @timer_proc.respond_to?(:name) ? @timer_proc.name : @timer_proc.to_s "[delay [#{@delay}], periodical? [#{@periodical}], fire_time [#{ftime}] fire_delay_ms [#{fdelay}]] proc [#{name}]" end |