class Bundler::Worker

Constants

POISON

Public Class Methods

new(size, func) click to toggle source

Creates a worker pool of specified size

@param size [Integer] Size of pool @param func [Proc] job to run in inside the worker pool

# File lib/bundler/worker.rb, line 18
def initialize(size, func)
  @request_queue = Queue.new
  @response_queue = Queue.new
  @func = func
  @threads = size.times.map { |i| Thread.start { process_queue(i) } }
  trap("INT") { abort_threads }
end

Public Instance Methods

deq() click to toggle source

Retrieves results of job function being executed in worker pool

# File lib/bundler/worker.rb, line 34
def deq
  result = @response_queue.deq
  raise result.exception if result.is_a?(WrappedException)
  result
end
enq(obj) click to toggle source

Enqueue a request to be executed in the worker pool

@param obj [String] mostly it is name of spec that should be downloaded

# File lib/bundler/worker.rb, line 29
def enq(obj)
  @request_queue.enq obj
end
stop() click to toggle source
# File lib/bundler/worker.rb, line 40
def stop
  stop_threads
end

Private Instance Methods

abort_threads() click to toggle source
# File lib/bundler/worker.rb, line 67
def abort_threads
  @threads.each {|i| i.exit }
  exit 1
end
apply_func(obj, i) click to toggle source
# File lib/bundler/worker.rb, line 54
def apply_func(obj, i)
  @func.call(obj, i)
rescue Exception => e
  WrappedException.new(e)
end
process_queue(i) click to toggle source
# File lib/bundler/worker.rb, line 46
def process_queue(i)
  loop do
    obj = @request_queue.deq
    break if obj.equal? POISON
    @response_queue.enq apply_func(obj, i)
  end
end
stop_threads() click to toggle source

Stop the worker threads by sending a poison object down the request queue so as worker threads after retrieving it, shut themselves down

# File lib/bundler/worker.rb, line 62
def stop_threads
  @threads.each { @request_queue.enq POISON }
  @threads.each { |thread| thread.join }
end