class Bootsnap::CLI::WorkerPool::Worker
def close
def close to_io.close end
def initialize(jobs)
def initialize(jobs) @jobs = jobs @pipe_out, @to_io = IO.pipe(binmode: true) # Set the writer encoding to binary since IO.pipe only sets it for the reader. # https://github.com/rails/rails/issues/16514#issuecomment-52313290 @to_io.set_encoding(Encoding::BINARY) @pid = nil end
def spawn
def spawn @pid = Process.fork do to_io.close work_loop exit!(0) end @pipe_out.close true end
def work_loop
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
def write(message, block: true)
def write(message, block: true) payload = Marshal.dump(message) if block to_io.write(payload) true else to_io.write_nonblock(payload, exception: false) != :wait_writable end end