Class: Bootsnap::CLI::WorkerPool::Worker
- Inherits:
-
Object
- Object
- Bootsnap::CLI::WorkerPool::Worker
- Defined in:
- lib/bootsnap/cli/worker_pool.rb
Instance Attribute Summary collapse
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#to_io ⇒ Object
readonly
Returns the value of attribute to_io.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(jobs) ⇒ Worker
constructor
A new instance of Worker.
- #spawn ⇒ Object
- #work_loop ⇒ Object
- #write(message, block: true) ⇒ Object
Constructor Details
#initialize(jobs) ⇒ Worker
Returns a new instance of Worker.
38 39 40 41 42 |
# File 'lib/bootsnap/cli/worker_pool.rb', line 38 def initialize(jobs) @jobs = jobs @pipe_out, @to_io = IO.pipe @pid = nil end |
Instance Attribute Details
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
36 37 38 |
# File 'lib/bootsnap/cli/worker_pool.rb', line 36 def pid @pid end |
#to_io ⇒ Object (readonly)
Returns the value of attribute to_io.
36 37 38 |
# File 'lib/bootsnap/cli/worker_pool.rb', line 36 def to_io @to_io end |
Instance Method Details
#close ⇒ Object
54 55 56 |
# File 'lib/bootsnap/cli/worker_pool.rb', line 54 def close to_io.close end |
#spawn ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/bootsnap/cli/worker_pool.rb', line 68 def spawn @pid = Process.fork do to_io.close work_loop exit!(0) end @pipe_out.close true end |
#work_loop ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/bootsnap/cli/worker_pool.rb', line 58 def work_loop loop do job, *args = Marshal.load(@pipe_out) return if job == :exit @jobs.fetch(job).call(*args) end rescue IOError nil end |
#write(message, block: true) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/bootsnap/cli/worker_pool.rb', line 44 def write(, block: true) payload = Marshal.dump() if block to_io.write(payload) true else to_io.write_nonblock(payload, exception: false) != :wait_writable end end |