Class: Triglav::Agent::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/triglav/agent/timer.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initializeTimer

Returns a new instance of Timer.



25
26
27
28
# File 'lib/triglav/agent/timer.rb', line 25

def initialize
  @r, @w = IO.pipe
  start
end

Instance Method Details

#startObject



44
45
46
# File 'lib/triglav/agent/timer.rb', line 44

def start
  @stop = false
end

#stopObject



48
49
50
51
# File 'lib/triglav/agent/timer.rb', line 48

def stop
  @stop = true
  signal
end

#wait(sec, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# 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