1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
# frozen_string_literal: true
module Jaeger
# Executes a given block periodically. The block will be executed only once
# when interval is set to 0.
class RecurringExecutor
def initialize(interval:)
@interval = interval
end
def start(&block)
raise 'Already running' if @thread
@thread = Thread.new do
if @interval <= 0
yield
else
loop do
yield
sleep @interval
end
end
end
end
def running?
@thread&.alive?
end
def stop
@thread.kill
@thread = nil
end
end
end
|