Class: Metrics::Rails::Worker
- Inherits:
-
Object
- Object
- Metrics::Rails::Worker
- Defined in:
- lib/metrics/rails/worker.rb
Overview
This class manages the background thread which submits all data to the Librato Metrics service.
Instance Method Summary collapse
-
#execute(obj) ⇒ Object
do the assigned work, catching some special cases.
-
#initialize ⇒ Worker
constructor
A new instance of Worker.
- #logger ⇒ Object
-
#run_periodically(period, &block) ⇒ Object
run the given block every <period> seconds, looping infinitely unless @interrupt becomes true.
-
#start_time(period) ⇒ Object
Give some structure to worker start times so when possible they will be in sync.
Constructor Details
#initialize ⇒ Worker
Returns a new instance of Worker.
8 9 10 |
# File 'lib/metrics/rails/worker.rb', line 8 def initialize @interrupt = false end |
Instance Method Details
#execute(obj) ⇒ Object
do the assigned work, catching some special cases
14 15 16 |
# File 'lib/metrics/rails/worker.rb', line 14 def execute(obj) obj.call end |
#logger ⇒ Object
18 19 20 |
# File 'lib/metrics/rails/worker.rb', line 18 def logger Metrics::Rails.logger end |
#run_periodically(period, &block) ⇒ Object
run the given block every <period> seconds, looping infinitely unless @interrupt becomes true.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/metrics/rails/worker.rb', line 25 def run_periodically(period, &block) next_run = start_time(period) until @interrupt do now = Time.now if now >= next_run execute(block) # runs given block while next_run <= now next_run += period end else sleep (next_run - now) end end end |
#start_time(period) ⇒ Object
Give some structure to worker start times so when possible they will be in sync.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/metrics/rails/worker.rb', line 42 def start_time(period) earliest = Time.now + period # already on a whole minute return earliest if earliest.sec == 0 if period > 30 # bump to whole minute earliest + (60-earliest.sec) else # ensure sync to whole minute if minute is evenly divisible earliest + (period-(earliest.sec%period)) end end |