Class: Npm::Mirror::Pool
- Inherits:
-
Object
- Object
- Npm::Mirror::Pool
- Defined in:
- lib/npm/mirror/pool.rb
Instance Attribute Summary collapse
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
- #enqueue_job(*args, &block) ⇒ Object
-
#initialize(size) ⇒ Pool
constructor
A new instance of Pool.
- #run ⇒ Object
- #run_til_done ⇒ Object
- #running? ⇒ Boolean
- #terminate ⇒ Object
Constructor Details
#initialize(size) ⇒ Pool
Returns a new instance of Pool.
8 9 10 11 12 |
# File 'lib/npm/mirror/pool.rb', line 8 def initialize(size) @size = size @queue = Queue.new @running = false end |
Instance Attribute Details
#size ⇒ Object
Returns the value of attribute size.
6 7 8 |
# File 'lib/npm/mirror/pool.rb', line 6 def size @size end |
Instance Method Details
#enqueue_job(*args, &block) ⇒ Object
60 61 62 |
# File 'lib/npm/mirror/pool.rb', line 60 def enqueue_job(*args, &block) @queue << [block, args] end |
#run ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# 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 ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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? ⇒ Boolean
14 15 16 |
# File 'lib/npm/mirror/pool.rb', line 14 def running? @running end |
#terminate ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# 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 |