Class: Tobox::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/tobox/pool.rb

Direct Known Subclasses

FiberPool, ThreadedPool

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Pool

Returns a new instance of Pool.



7
8
9
10
11
12
13
14
15
16
# File 'lib/tobox/pool.rb', line 7

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

Instance Method Details

#do_work(wrk) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tobox/pool.rb', line 25

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

#stopObject



18
19
20
21
22
23
# File 'lib/tobox/pool.rb', line 18

def stop
  return unless @running

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