Class: Triglav::Agent::Timer
- Inherits:
-
Object
- Object
- Triglav::Agent::Timer
- 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
-
#initialize ⇒ Timer
constructor
A new instance of Timer.
- #start ⇒ Object
- #stop ⇒ Object
- #wait(sec, &block) ⇒ Object
Constructor Details
#initialize ⇒ Timer
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
#start ⇒ Object
44 45 46 |
# File 'lib/triglav/agent/timer.rb', line 44 def start @stop = false end |
#stop ⇒ Object
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 |