Class: MaintenanceTasks::Ticker Private
- Inherits:
-
Object
- Object
- MaintenanceTasks::Ticker
- Defined in:
- app/models/maintenance_tasks/ticker.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
This class encapsulates the logic behind updating the tick counter.
It’s initialized with a duration for the throttle, and a block to persist the number of ticks to increment.
When tick
is called, the block will be called with the increment, provided the duration since the last update (or initialization) has been long enough.
To not lose any increments, persist
should be used, which may call the block with any leftover ticks.
Instance Method Summary collapse
-
#initialize(throttle_duration) {|ticks| ... } ⇒ Ticker
constructor
private
Creates a Ticker that will call the block each time
tick
is called, unless the tick is being throttled. -
#persist ⇒ Object
private
Persists the tick increments by calling the block passed to the initializer.
-
#tick ⇒ Object
private
Increments the tick count by one, and may persist the new value if the threshold duration has passed since initialization or the tick count was last persisted.
Constructor Details
#initialize(throttle_duration) {|ticks| ... } ⇒ Ticker
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a Ticker that will call the block each time tick
is called, unless the tick is being throttled.
24 25 26 27 28 29 |
# File 'app/models/maintenance_tasks/ticker.rb', line 24 def initialize(throttle_duration, &persist) @throttle_duration = throttle_duration @persist = persist @last_persisted = Time.now @ticks_recorded = 0 end |
Instance Method Details
#persist ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Persists the tick increments by calling the block passed to the initializer. This is idempotent in the sense that calling it twice in a row will call the block at most once (if it had been throttled).
42 43 44 45 46 47 48 49 50 |
# File 'app/models/maintenance_tasks/ticker.rb', line 42 def persist return if @ticks_recorded == 0 now = Time.now duration = now - @last_persisted @last_persisted = now @persist.call(@ticks_recorded, duration) @ticks_recorded = 0 end |
#tick ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Increments the tick count by one, and may persist the new value if the threshold duration has passed since initialization or the tick count was last persisted.
34 35 36 37 |
# File 'app/models/maintenance_tasks/ticker.rb', line 34 def tick @ticks_recorded += 1 persist if persist? end |