require_relative 'logger' module Remon class CheckRunner include Logger def initialize(task_queue:, num_workers:, result_queue:) @task_queue = task_queue @num_workers = num_workers @result_queue = result_queue @workers = [] end def start logger.debug "starting runners" @num_workers.times { @workers << new_worker } end def stop @workers.each { |t| Thread.kill t if (t && t.alive?)} end def new_worker Thread.new do loop { process_job } end end def process_job j = @task_queue.pop logger.debug "running #{j}" if logger.debug? result = run_job j case result when Array result.each { |r| @result_queue << r if r} else @result_queue << result if result end rescue => e logger.error "error while running job #{e.message}" end def run_job(j) if @num_workers > 1 j.run_mutex else j.run end end end end