Class: Sigurd::Executor
- Inherits:
-
Object
- Object
- Sigurd::Executor
- 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
-
#initialize(runners, sleep_seconds: nil, logger: Logger.new(STDOUT)) ⇒ Executor
constructor
started or stopped.
-
#start ⇒ Object
Start the executor.
-
#stop ⇒ Object
Stop the executor.
Constructor Details
#initialize(runners, sleep_seconds: nil, logger: Logger.new(STDOUT)) ⇒ Executor
started or stopped. failed runs instead of using an exponential backoff.
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
Instance Method Details
#start ⇒ Object
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 |
#stop ⇒ Object
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 |