class Triglav::Agent::Timer

A timer utility to run serverengine worker in a time interval

module Triglav::Agent
  module Worker
    def initialize
      @interval = 60.0 # sec
    end

    def run
      @timer = Timer.new
      @stop = false
      until @stop
        @timer.wait(@interval) { process }
      end
    end

    def stop
      @stop = true
      @timer.stop
    end
  end
end

Public Class Methods

new() click to toggle source
# File lib/triglav/agent/timer.rb, line 25
def initialize
  @r, @w = IO.pipe
  start
end

Public Instance Methods

start() click to toggle source
# File lib/triglav/agent/timer.rb, line 44
def start
  @stop = false
end
stop() click to toggle source
# File lib/triglav/agent/timer.rb, line 48
def stop
  @stop = true
  signal
end
wait(sec) { || ... } click to toggle source
# File lib/triglav/agent/timer.rb, line 30
def wait(sec, &block)
  return if @stop
  started = Time.now
  yield
  elapsed = Time.now - started
  if (timeout = (sec - elapsed).to_f) > 0
    begin
      IO.select([@r], [], [], timeout)
    rescue IOError, Errno::EBADF
      # these error may occur if @r is closed during IO.select. Ignore it
    end
  end
end

Private Instance Methods

signal() click to toggle source
# File lib/triglav/agent/timer.rb, line 55
def signal
  @w.puts ' '
end