class InstJobsStatsd::Stats::Periodic::Timer

Public Class Methods

new(min_interval) click to toggle source
# File lib/inst_jobs_statsd/stats/periodic.rb, line 65
def initialize(min_interval)
  @min_interval = min_interval * 1.0
  @start_time = Delayed::Job.db_time_now
  update_next_run
end

Public Instance Methods

tick() { || ... } click to toggle source

This is called as often as possible, based on the lifecycle callbacks. When the required interval of time has passed, execute the given block

# File lib/inst_jobs_statsd/stats/periodic.rb, line 73
def tick
  return unless Delayed::Job.db_time_now >= @next_run
  update_next_run
  yield
end

Private Instance Methods

update_next_run() click to toggle source

Target the next run time to based on the original start time, instead of just adding the run interval, to prevent drift from the target interval as much as possible

# File lib/inst_jobs_statsd/stats/periodic.rb, line 84
def update_next_run
  ticks = ((Delayed::Job.db_time_now - @start_time) / @min_interval).floor
  @next_run = @start_time + (ticks + 1) * @min_interval
end