class Bundler::Worker
常量
- POISON
属性
name[R]
@return [String] 工作器的名称
公共类方法
new(size, name, func) 点击以切换源代码
创建指定大小的工作器池
@param size [Integer] 池的大小 @param name [String] 工作器的名称 @param func [Proc] 在工作器池内部运行的作业
# File bundler/worker.rb, line 22 def initialize(size, name, func) @name = name @request_queue = Thread::Queue.new @response_queue = Thread::Queue.new @func = func @size = size @threads = nil @previous_interrupt_handler = nil end
公共实例方法
deq() 点击以切换源代码
检索在工作器池中执行的作业函数的结果
# File bundler/worker.rb, line 41 def deq result = @response_queue.deq raise result.exception if result.is_a?(WrappedException) result end
enq(obj) 点击以切换源代码
将要在工作器池中执行的请求入队
@param obj [String] 大部分情况下,它是应该下载的规范的名称
# File bundler/worker.rb, line 35 def enq(obj) create_threads unless @threads @request_queue.enq obj end
stop() 点击以切换源代码
# File bundler/worker.rb, line 47 def stop stop_threads end
私有实例方法
abort_threads() 点击以切换源代码
# File bundler/worker.rb, line 80 def abort_threads Bundler.ui.debug("\n#{caller.join("\n")}") @threads.each(&:exit) exit 1 end
add_interrupt_handler() 点击以切换源代码
# File bundler/worker.rb, line 107 def add_interrupt_handler @previous_interrupt_handler = trap("INT") { abort_threads } end
apply_func(obj, i) 点击以切换源代码
# File 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() 点击以切换源代码
# File 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? 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
process_queue(i) 点击以切换源代码
# File 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() 点击以切换源代码
# File bundler/worker.rb, line 111 def remove_interrupt_handler return unless @previous_interrupt_handler trap "INT", @previous_interrupt_handler end
stop_threads() 点击以切换源代码
通过将一个“毒药”对象发送到请求队列来停止工作器线程,以便工作器线程在检索到它后关闭自身
# File 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