Class: Deimos::Utils::Executor

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

Overview

Mostly copied from Phobos::Executor. We should DRY this up by putting in a PR to make it more generic. Might even make sense to move to a separate gem.

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



18
19
20
21
22
23
# File 'lib/deimos/utils/executor.rb', line 18

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:



11
12
13
# File 'lib/deimos/utils/executor.rb', line 11

def runners
  @runners
end

Instance Method Details

#startObject

Start the executor.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/deimos/utils/executor.rb', line 26

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.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/deimos/utils/executor.rb', line 45

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