Sha256: 721b6fbd3433805abfb0f73f531dcb1f01f1d20a89f9a9d43559941daf76060e

Contents?: true

Size: 1.12 KB

Versions: 5

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Tobox
  class KillError < Interrupt; end

  class Pool
    def initialize(configuration)
      @configuration = configuration
      @logger = @configuration.default_logger
      @num_workers = configuration[:concurrency]
      @workers = Array.new(@num_workers) do |idx|
        Worker.new("tobox-worker-#{idx}", configuration)
      end
      @worker_error_handlers = Array(@configuration.lifecycle_events[:error_worker])
      @running = true
    end

    def stop
      return unless @running

      @workers.each(&:finish!)
      @running = false
    end

    def do_work(wrk)
      wrk.work
    rescue KillError
    # noop
    rescue Exception => e # rubocop:disable Lint/RescueException
      wrk.finish!
      @logger.error do
        "(worker: #{wrk.label}) -> " \
          "crashed with error\n" \
          "#{e.class}: #{e.message}\n" \
          "#{e.backtrace.join("\n")}"
      end
      @worker_error_handlers.each { |hd| hd.call(e) }
    end
  end

  autoload :ThreadedPool, File.join(__dir__, "pool", "threaded_pool")
  autoload :FiberPool, File.join(__dir__, "pool", "fiber_pool")
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tobox-0.6.1 lib/tobox/pool.rb
tobox-0.6.0 lib/tobox/pool.rb
tobox-0.5.2 lib/tobox/pool.rb
tobox-0.5.1 lib/tobox/pool.rb
tobox-0.5.0 lib/tobox/pool.rb