Class: Bootsnap::CLI::WorkerPool
- Inherits:
-
Object
- Object
- Bootsnap::CLI::WorkerPool
- Defined in:
- lib/bootsnap/cli/worker_pool.rb
Defined Under Namespace
Class Method Summary collapse
Instance Method Summary collapse
- #dispatch_loop ⇒ Object
- #free_worker ⇒ Object
-
#initialize(size:, jobs: {}) ⇒ WorkerPool
constructor
A new instance of WorkerPool.
- #push(*args) ⇒ Object
- #shutdown ⇒ Object
- #spawn ⇒ Object
Constructor Details
#initialize(size:, jobs: {}) ⇒ WorkerPool
Returns a new instance of WorkerPool.
79 80 81 82 83 84 |
# File 'lib/bootsnap/cli/worker_pool.rb', line 79 def initialize(size:, jobs: {}) @size = size @jobs = jobs @queue = Queue.new @pids = [] end |
Class Method Details
Instance Method Details
#dispatch_loop ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/bootsnap/cli/worker_pool.rb', line 94 def dispatch_loop loop do case job = @queue.pop when nil @workers.each do |worker| worker.write([:exit]) worker.close end return true else unless @workers.sample.write(job, block: false) free_worker.write(job) end end end end |
#free_worker ⇒ Object
111 112 113 |
# File 'lib/bootsnap/cli/worker_pool.rb', line 111 def free_worker IO.select(nil, @workers)[1].sample end |
#push(*args) ⇒ Object
115 116 117 118 |
# File 'lib/bootsnap/cli/worker_pool.rb', line 115 def push(*args) @queue.push(args) nil end |
#shutdown ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/bootsnap/cli/worker_pool.rb', line 120 def shutdown @queue.close @dispatcher_thread.join @workers.each do |worker| _pid, status = Process.wait2(worker.pid) return status.exitstatus unless status.success? end nil end |