diff --git a/lib/cuetip/worker_group.rb b/lib/cuetip/worker_group.rb index fe188a6..1c92c65 100644 --- a/lib/cuetip/worker_group.rb +++ b/lib/cuetip/worker_group.rb @@ -6,6 +6,7 @@ module Cuetip class WorkerGroup include ActiveSupport::Callbacks + define_callbacks :run_worker_group define_callbacks :run_worker attr_reader :quantity @@ -34,19 +35,21 @@ def start trap('INT', &exit_trap) trap('TERM', &exit_trap) - @quantity.times do |i| - @workers[i] = Worker.new(self, i, @queues) - Klogger.tagged worker_index: i do - Cuetip.logger.info "Starting worker" - @threads[i] = Thread.new(@workers[i]) do |worker| - run_callbacks :run_worker do - worker.run + run_callbacks :run_worker_group do + @quantity.times do |i| + @workers[i] = Worker.new(self, i, @queues) + Klogger.tagged worker_index: i do + Cuetip.logger.info "Starting worker" + @threads[i] = Thread.new(@workers[i]) do |worker| + run_callbacks :run_worker do + worker.run + end end end + @threads[i].abort_on_exception = true end - @threads[i].abort_on_exception = true + @threads.values.each(&:join) end - @threads.values.each(&:join) end end