Class: Bundler::Worker
Relationships & Source Files | |
Namespace Children | |
Exceptions:
| |
Inherits: | Object |
Defined in: | lib/bundler/worker.rb |
Constant Summary
-
POISON =
# File 'lib/bundler/worker.rb', line 5Object.new
Class Method Summary
-
.new(size, name, func) ⇒ Worker
constructor
Creates a worker pool of specified size.
Instance Attribute Summary
- #name ⇒ String readonly
Instance Method Summary
-
#deq
Retrieves results of job function being executed in worker pool.
-
#enq(obj)
Enqueue a request to be executed in the worker pool.
- #stop
- #abort_threads private
- #add_interrupt_handler private
- #apply_func(obj, i) private
- #create_threads private
- #process_queue(i) private
- #remove_interrupt_handler private
-
#stop_threads
private
Stop the worker threads by sending a poison object down the request queue so as worker threads after retrieving it, shut themselves down.
Constructor Details
.new(size, name, func) ⇒ Worker
Creates a worker pool of specified size
Instance Attribute Details
#name ⇒ String
(readonly)
# File 'lib/bundler/worker.rb', line 15
attr_reader :name
Instance Method Details
#abort_threads (private)
[ GitHub ]#add_interrupt_handler (private)
[ GitHub ]# File 'lib/bundler/worker.rb', line 107
def add_interrupt_handler @previous_interrupt_handler = trap("INT") { abort_threads } end
#apply_func(obj, i) (private)
[ GitHub ]# File 'lib/bundler/worker.rb', line 61
def apply_func(obj, i) @func.call(obj, i) rescue Exception => e # rubocop:disable Lint/RescueException WrappedException.new(e) end
#create_threads (private)
# File 'lib/bundler/worker.rb', line 86
def create_threads creation_errors = [] @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 rescue ThreadError => e creation_errors << e nil end.compact add_interrupt_handler unless @threads.empty? return if creation_errors.empty? = "Failed to create threads for the #{name} worker: #{creation_errors.map(&:to_s).uniq.join(", ")}" raise ThreadCreationError, if @threads.empty? Bundler.ui.info end
#deq
Retrieves results of job function being executed in worker pool
# File 'lib/bundler/worker.rb', line 41
def deq result = @response_queue.deq raise result.exception if result.is_a?(WrappedException) result end
#enq(obj)
Enqueue a request to be executed in the worker pool
# File 'lib/bundler/worker.rb', line 35
def enq(obj) create_threads unless @threads @request_queue.enq obj end
#process_queue(i) (private)
[ GitHub ]# File 'lib/bundler/worker.rb', line 53
def process_queue(i) loop do obj = @request_queue.deq break if obj.equal? POISON @response_queue.enq apply_func(obj, i) end end
#remove_interrupt_handler (private)
[ GitHub ]# File 'lib/bundler/worker.rb', line 111
def remove_interrupt_handler return unless @previous_interrupt_handler trap "INT", @previous_interrupt_handler end
#stop
[ GitHub ]# File 'lib/bundler/worker.rb', line 47
def stop stop_threads end
#stop_threads (private)
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 69
def stop_threads return unless @threads @threads.each { @request_queue.enq POISON } @threads.each(&:join) remove_interrupt_handler @threads = nil end