Module: Creeper::Launcher

Extended by:
Launcher
Included in:
Launcher
Defined in:
lib/creeper/launcher.rb

Instance Method Summary collapse

Instance Method Details

#launch!(options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/creeper/launcher.rb', line 13

def launch!(options)
  $stdin.reopen("/dev/null")

  # grandparent - reads pipe, exits when master is ready
  #  \_ parent  - exits immediately ASAP
  #      \_ creeper master - writes to pipe when ready

  rd, wr = IO.pipe
  grandparent = $$
  if fork
    wr.close # grandparent does not write
  else
    rd.close # creeper master does not read
    Process.setsid
    exit if fork # parent dies now
  end

  if grandparent == $$
    # this will block until Creeper.join runs (or it dies)
    creeper_pid = (rd.readpartial(16) rescue nil).to_i
    unless creeper_pid > 1
      warn "creeper failed to start, check stderr log for details"
      exit!(1)
    end
    exit 0
  else # creeper master process
    options[:ready_pipe] = wr
  end
end