class Npm::Mirror::Pool
Attributes
size[RW]
Public Class Methods
new(size)
click to toggle source
# File lib/npm/mirror/pool.rb, line 8 def initialize(size) @size = size @queue = Queue.new @running = false end
Public Instance Methods
enqueue_job(*args, &block)
click to toggle source
# File lib/npm/mirror/pool.rb, line 60 def enqueue_job(*args, &block) @queue << [block, args] end
run()
click to toggle source
# File lib/npm/mirror/pool.rb, line 18 def run return if running? @running = true @threads = Array.new(@size) do |i| Thread.new do Thread.current[:id] = i catch(:exit) do loop do job, args = @queue.pop job.call(*args) end end end end end
run_til_done()
click to toggle source
# File lib/npm/mirror/pool.rb, line 34 def run_til_done run until @queue.empty? && @queue.num_waiting == @size @threads.each { |t| t.join 0.2 } l = @size.to_s.size master = 'Master'.ljust(%w(Thread- Master).map(&:size).max + l) puts "[#{master}] Queue size: #{@queue.size}, Waiting thread: " \ "#{@queue.num_waiting}" end terminate end
running?()
click to toggle source
# File lib/npm/mirror/pool.rb, line 14 def running? @running end
terminate()
click to toggle source
# File lib/npm/mirror/pool.rb, line 48 def terminate return unless running? @running = false @size.times do enqueue_job { throw :exit } end @threads.each { |t| t.join 0.2 } @threads.each { |t| t.kill } end