Class: Sigurd::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/sigurd/executor.rb

Overview

Class that takes a list of “runners” and runs them on a loop until told to stop. Each runner is given its own thread. Runners need to define a “start” and “stop” method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runners, sleep_seconds: nil, logger: Logger.new(STDOUT)) ⇒ Executor

started or stopped. failed runs instead of using an exponential backoff.

Parameters:

  • runners (Array<#start, #stop, #id>)

    A list of objects that can be

  • logger (Logger) (defaults to: Logger.new(STDOUT))
  • sleep_seconds (Integer) (defaults to: nil)

    Use a fixed time to sleep between



20
21
22
23
24
25
# File 'lib/sigurd/executor.rb', line 20

def initialize(runners, sleep_seconds: nil, logger: Logger.new(STDOUT))
  @threads = Concurrent::Array.new
  @runners = runners
  @logger = logger
  @sleep_seconds = sleep_seconds
end

Instance Attribute Details

#runnersArray<#start, #stop, #id>

Returns:



13
14
15
# File 'lib/sigurd/executor.rb', line 13

def runners
  @runners
end

Instance Method Details

#startObject

Start the executor.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sigurd/executor.rb', line 28

def start
  @logger.info('Starting executor')
  @signal_to_stop = false
  @threads.clear
  @thread_pool = Concurrent::FixedThreadPool.new(@runners.size)

  @runners.each do |runner|
    @thread_pool.post do
      thread = Thread.current
      thread.abort_on_exception = true
      @threads << thread
      run_object(runner)
    end
  end

  true
end

#stopObject

Stop the executor.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sigurd/executor.rb', line 47

def stop
  return if @signal_to_stop

  @logger.info('Stopping executor')
  @signal_to_stop = true
  @runners.each(&:stop)
  @threads.select(&:alive?).each do |thread|
    begin
      thread.wakeup
    rescue StandardError
      nil
    end
  end
  @thread_pool&.shutdown
  @thread_pool&.wait_for_termination
  @logger.info('Executor stopped')
end