Class: OneApm::Support::EventLoop::Timer
- Inherits:
-
Object
- Object
- OneApm::Support::EventLoop::Timer
- Defined in:
- lib/one_apm/support/event/timer.rb
Instance Attribute Summary collapse
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#last_fired_at ⇒ Object
readonly
Returns the value of attribute last_fired_at.
-
#next_fire_time ⇒ Object
readonly
Returns the value of attribute next_fire_time.
Instance Method Summary collapse
- #advance(amount) ⇒ Object
- #calculate_next_fire_time ⇒ Object
- #due?(now = Time.now) ⇒ Boolean
- #finished? ⇒ Boolean
-
#initialize(interval, event, repeat = false) ⇒ Timer
constructor
A new instance of Timer.
- #last_interval_start ⇒ Object
- #reschedule ⇒ Object
- #set_fired_time ⇒ Object
Constructor Details
#initialize(interval, event, repeat = false) ⇒ Timer
Returns a new instance of Timer.
10 11 12 13 14 15 16 17 |
# File 'lib/one_apm/support/event/timer.rb', line 10 def initialize(interval, event, repeat=false) @interval = interval @event = event @repeat = repeat @started_at = Time.now @last_fired_at = nil reschedule end |
Instance Attribute Details
#event ⇒ Object (readonly)
Returns the value of attribute event.
8 9 10 |
# File 'lib/one_apm/support/event/timer.rb', line 8 def event @event end |
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
8 9 10 |
# File 'lib/one_apm/support/event/timer.rb', line 8 def interval @interval end |
#last_fired_at ⇒ Object (readonly)
Returns the value of attribute last_fired_at.
8 9 10 |
# File 'lib/one_apm/support/event/timer.rb', line 8 def last_fired_at @last_fired_at end |
#next_fire_time ⇒ Object (readonly)
Returns the value of attribute next_fire_time.
8 9 10 |
# File 'lib/one_apm/support/event/timer.rb', line 8 def next_fire_time @next_fire_time end |
Instance Method Details
#advance(amount) ⇒ Object
23 24 25 |
# File 'lib/one_apm/support/event/timer.rb', line 23 def advance(amount) @next_fire_time -= amount end |
#calculate_next_fire_time ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/one_apm/support/event/timer.rb', line 31 def calculate_next_fire_time now = Time.now return now if @interval == 0 fire_time = @last_fired_at || now while fire_time <= now fire_time += @interval end fire_time end |
#due?(now = Time.now) ⇒ Boolean
45 46 47 |
# File 'lib/one_apm/support/event/timer.rb', line 45 def due?(now=Time.now) now >= @next_fire_time end |
#finished? ⇒ Boolean
49 50 51 |
# File 'lib/one_apm/support/event/timer.rb', line 49 def finished? !@repeat && @last_fired_at end |
#last_interval_start ⇒ Object
27 28 29 |
# File 'lib/one_apm/support/event/timer.rb', line 27 def last_interval_start @last_fired_at || @started_at end |
#reschedule ⇒ Object
19 20 21 |
# File 'lib/one_apm/support/event/timer.rb', line 19 def reschedule @next_fire_time = calculate_next_fire_time end |
#set_fired_time ⇒ Object
41 42 43 |
# File 'lib/one_apm/support/event/timer.rb', line 41 def set_fired_time @last_fired_at = Time.now end |