class Workers::Timer

Attributes

delay[R]
repeat[R]

Public Class Methods

new(delay, options = {}, &block) click to toggle source
# File lib/workers/timer.rb, line 8
def initialize(delay, options = {}, &block)
  @logger = Workers::LogProxy.new(options[:logger])
  @delay = delay
  @callback = options[:callback] || block
  @repeat = options[:repeat] || false
  @scheduler = options[:scheduler] || Workers.scheduler
  @mutex = Mutex.new

  reset
  @scheduler.schedule(self)

  nil
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/workers/timer.rb, line 22
def <=>(other)
  sec_remaining <=> other.sec_remaining
end
cancel() click to toggle source
# File lib/workers/timer.rb, line 45
def cancel
  @scheduler.unschedule(self)

  nil
end
fire() click to toggle source
# File lib/workers/timer.rb, line 37
def fire
  @mutex.synchronize do
    @callback.call if @callback
  end

  nil
end
overdue?() click to toggle source
# File lib/workers/timer.rb, line 33
def overdue?
  sec_remaining <= 0
end
reset() click to toggle source
# File lib/workers/timer.rb, line 51
def reset
  @mutex.synchronize do
    @fire_at = Time.now.utc + @delay
  end

  nil
end
sec_remaining() click to toggle source
# File lib/workers/timer.rb, line 26
def sec_remaining
  @mutex.synchronize do
    diff = @fire_at.to_f - Time.now.utc.to_f
    (diff > 0) ? diff : 0
  end
end