Class: Jaeger::RecurringExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/jaeger/recurring_executor.rb

Overview

Executes a given block periodically. The block will be executed only once when interval is set to 0.

Instance Method Summary collapse

Constructor Details

#initialize(interval:) ⇒ RecurringExecutor

Returns a new instance of RecurringExecutor.



7
8
9
# File 'lib/jaeger/recurring_executor.rb', line 7

def initialize(interval:)
  @interval = interval
end

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/jaeger/recurring_executor.rb', line 26

def running?
  @thread&.alive?
end

#start(&block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jaeger/recurring_executor.rb', line 11

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

#stopObject



30
31
32
33
# File 'lib/jaeger/recurring_executor.rb', line 30

def stop
  @thread.kill
  @thread = nil
end