класс Bundler::Worker
Константы
- POISON
Атрибуты
name[R]
@return [String] имя работника
Общедоступные методы класса
# File lib/bundler/worker.rb, line 22
def initialize(size, name, func)
@name = name
@request_queue = Queue.new
@response_queue = Queue.new
@func = func
@size = size
@threads = nil
SharedHelpers.trap("INT") { abort_threads }
end Создаёт пул работников заданного размера
@param size [Целое число] Размер пула @param name [Строка] имя работника @param func [Proc] задача для выполнения в пуле работников
Общедоступные методы экземпляра
# File lib/bundler/worker.rb, line 41 def deq result = @response_queue.deq raise result.exception if result.is_a?(WrappedException) result end
Извлекает результаты выполнения задачи в пуле работников
# File lib/bundler/worker.rb, line 35 def enq(obj) create_threads unless @threads @request_queue.enq obj end
Добавляет запрос в очередь для выполнения в пуле работников
@param obj [Строка] в основном, это имя спецификации, которая должна быть загружена
# File lib/bundler/worker.rb, line 47 def stop stop_threads end
Приватные методы экземпляра
# File lib/bundler/worker.rb, line 76
def abort_threads
return unless @threads
Bundler.ui.debug("\n#{caller.join("\n")}")
@threads.each(&:exit)
exit 1
end # 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
# File lib/bundler/worker.rb, line 83
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 # 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 # File lib/bundler/worker.rb, line 69
def stop_threads
return unless @threads
@threads.each { @request_queue.enq POISON }
@threads.each(&:join)
@threads = nil
end Останавливает потоки работников, отправляя объект яда в очередь запросов, так как потоки работников после его получения завершат свою работу
Ruby Core © 1993–2017 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.