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
264 265 266 267 268 269 270 271 |
# File 'lib/zm/timers.rb', line 264 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.
260 261 262 |
# File 'lib/zm/timers.rb', line 260 def fire_time @fire_time end |
#timer_proc ⇒ Object (readonly)
Returns the value of attribute timer_proc.
260 261 262 |
# File 'lib/zm/timers.rb', line 260 def timer_proc @timer_proc end |
Instance Method Details
#<=>(other) ⇒ Object
291 292 293 |
# File 'lib/zm/timers.rb', line 291 def <=>(other) @fire_time <=> other.fire_time end |
#==(other) ⇒ Object
295 296 297 298 299 300 301 |
# File 'lib/zm/timers.rb', line 295 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.
287 288 289 |
# File 'lib/zm/timers.rb', line 287 def cancel @timers.cancel self end |
#expired?(time) ⇒ Boolean
True when the timer should be fired; false otherwise.
305 306 307 308 |
# File 'lib/zm/timers.rb', line 305 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.
279 280 281 282 283 |
# File 'lib/zm/timers.rb', line 279 def fire @timer_proc.call schedule_firing_time if @periodical end |
#inspect ⇒ Object
328 |
# File 'lib/zm/timers.rb', line 328 def inspect; to_s; end |
#periodical? ⇒ Boolean
True when this is a periodical timer; false otherwise.
312 313 314 |
# File 'lib/zm/timers.rb', line 312 def periodical? @periodical end |
#reschedule ⇒ Object
316 317 318 |
# File 'lib/zm/timers.rb', line 316 def reschedule schedule_firing_time end |
#to_s ⇒ Object
320 321 322 323 324 325 326 |
# File 'lib/zm/timers.rb', line 320 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 |