Class: Bootsnap::CLI::WorkerPool::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/bootsnap/cli/worker_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#pidObject (readonly)

Returns the value of attribute pid.



36
37
38
# File 'lib/bootsnap/cli/worker_pool.rb', line 36

def pid
  @pid
end

#to_ioObject (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

#closeObject



54
55
56
# File 'lib/bootsnap/cli/worker_pool.rb', line 54

def close
  to_io.close
end

#spawnObject



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_loopObject



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(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