class MainLoop::Dispatcher
Attributes
bus[R]
handlers[R]
logger[R]
Public Class Methods
new(bus, timeout: 5, logger: nil)
click to toggle source
Calls superclass method
# File lib/main_loop/dispatcher.rb, line 11 def initialize(bus, timeout: 5, logger: nil) super() @bus = bus @timeout = timeout @handlers = [] @logger = logger || Logger.new(nil) @exit_code = 0 end
Public Instance Methods
add_handler(handler)
click to toggle source
# File lib/main_loop/dispatcher.rb, line 37 def add_handler(handler) synchronize do handler.term if terminating? handlers << handler end end
crash()
click to toggle source
# File lib/main_loop/dispatcher.rb, line 61 def crash @exit_code = 3 term unless terminating? end
log_status()
click to toggle source
:nocov:
# File lib/main_loop/dispatcher.rb, line 101 def log_status total = handlers.size running = handlers.count(&:running?) finihsed = handlers.count(&:finished?) term_text = terminating? ? 'TERM' : '' logger.debug("Total:#{total} Running:#{running} Finihsed:#{finihsed}. #{term_text}".strip) end
need_force_kill?()
click to toggle source
# File lib/main_loop/dispatcher.rb, line 79 def need_force_kill? @terminating_at && (Time.now - @terminating_at) >= @timeout end
pids()
click to toggle source
# File lib/main_loop/dispatcher.rb, line 83 def pids handlers.map{|h| h.pid rescue nil }.compact end
reap(statuses)
click to toggle source
# File lib/main_loop/dispatcher.rb, line 20 def reap(statuses) statuses.each do |(pid, status)| reap_by_id(pid, status) end end
reap_by_id(id, status)
click to toggle source
# File lib/main_loop/dispatcher.rb, line 26 def reap_by_id(id, status) synchronize do if (handler = handlers.find {|h| h.id == id }) logger.info("Reap handler #{handler.name.inspect}. Status: #{status.inspect}") handler.reap(status) else logger.debug("Reap unknown handler. Status: #{status.inspect}. Skipped") end end end
term()
click to toggle source
# File lib/main_loop/dispatcher.rb, line 48 def term synchronize do if terminating? logger.info('Terminate FORCE all handlers') handlers.each(&:kill) else @terminating_at ||= Time.now logger.info('Terminate all handlers') handlers.each(&:term) end end end
terminating?()
click to toggle source
# File lib/main_loop/dispatcher.rb, line 44 def terminating? @terminating_at end
tick()
click to toggle source
# File lib/main_loop/dispatcher.rb, line 66 def tick log_status if logger.debug? return unless terminating? try_exit! return if @killed || !need_force_kill? @killed = true logger.info('Killing all handlers by timeout') handlers.each(&:kill) end
try_exit!()
click to toggle source
:nocov:
# File lib/main_loop/dispatcher.rb, line 88 def try_exit! synchronize do return unless handlers.all?(&:finished?) logger.info('All handlers finished exiting...') status = handlers.all?(&:success?) ? @exit_code : 1 logger.info("Exit: #{status}") exit status end end