lib/tobox/pool.rb in tobox-0.6.1 vs lib/tobox/pool.rb in tobox-0.7.0
- old
+ new
@@ -7,11 +7,11 @@
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)
+ @configuration.worker_class.new("tobox-worker-#{idx}", configuration)
end
@worker_error_handlers = Array(@configuration.lifecycle_events[:error_worker])
@running = true
end
@@ -20,22 +20,31 @@
@workers.each(&:finish!)
@running = false
end
+ private
+
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")}"
+ handle_exception(wrk, e)
+ end
+
+ def handle_exception(wrk, exc)
+ case exc
+ when KillError
+ # noop
+ when Exception
+ wrk.finish!
+ @logger.error do
+ "(worker: #{wrk.label}) -> " \
+ "crashed with error\n" \
+ "#{exc.class}: #{exc.message}\n" \
+ "#{exc.backtrace.join("\n")}"
+ end
+ @worker_error_handlers.each { |hd| hd.call(exc) }
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")