Class: Bundler::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/worker.rb

Defined Under Namespace

Classes: WrappedException

Constant Summary collapse

POISON =
Object.new

Instance Method Summary collapse

Constructor Details

#initialize(size, func) ⇒ Worker

Creates a worker pool of specified size

Parameters:

  • size (Integer)

    Size of pool

  • func (Proc)

    job to run in inside the worker pool


18
19
20
21
22
23
24
# 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

Instance Method Details

#deqObject

Retrieves results of job function being executed in worker pool


34
35
36
37
38
# 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) ⇒ Object

Enqueue a request to be executed in the worker pool

Parameters:

  • obj (String)

    mostly it is name of spec that should be downloaded


29
30
31
# File 'lib/bundler/worker.rb', line 29

def enq(obj)
  @request_queue.enq obj
end

#stopObject


40
41
42
# File 'lib/bundler/worker.rb', line 40

def stop
  stop_threads
end