Sha256: 473c366529055a94716daa4b98e8c89f311cf83e2c7685b426078f91d9758c22

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
remon-0.1.0 lib/remon/check_runner.rb