diff options
Diffstat (limited to 'lib/bundler/worker.rb')
-rw-r--r-- | lib/bundler/worker.rb | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/lib/bundler/worker.rb b/lib/bundler/worker.rb index 3ef5addcee..62d93e285e 100644 --- a/lib/bundler/worker.rb +++ b/lib/bundler/worker.rb @@ -25,11 +25,8 @@ module Bundler @request_queue = Queue.new @response_queue = Queue.new @func = func - @threads = Array.new(size) do |i| - Thread.start { process_queue(i) }.tap do |thread| - thread.name = "#{name} Worker ##{i}" if thread.respond_to?(:name=) - end - end + @size = size + @threads = nil trap("INT") { abort_threads } end @@ -37,6 +34,7 @@ module Bundler # # @param obj [String] mostly it is name of spec that should be downloaded def enq(obj) + create_threads unless @threads @request_queue.enq obj end @@ -70,13 +68,37 @@ module Bundler # Stop the worker threads by sending a poison object down the request queue # so as worker threads after retrieving it, shut themselves down def stop_threads + return unless @threads @threads.each { @request_queue.enq POISON } @threads.each(&:join) + @threads = nil end def abort_threads + return unless @threads @threads.each(&:exit) exit 1 end + + def create_threads + creation_errors = [] + + @threads = Array.new(@size) do |i| + begin + Thread.start { process_queue(i) }.tap do |thread| + thread.name = "#{name} Worker ##{i}" if thread.respond_to?(:name=) + end + rescue ThreadError => e + creation_errors << e + nil + end + end.compact + + return if creation_errors.empty? + + message = "Failed to create threads for the #{name} worker: #{creation_errors.map(&:to_s).uniq.join(", ")}" + raise ThreadCreationError, message if @threads.empty? + Bundler.ui.info message + end end end |