class Resqued::Daemon

Public Class Methods

new(master) click to toggle source
# File lib/resqued/daemon.rb, line 3
def initialize(master)
  @master = master
end

Public Instance Methods

run() click to toggle source

Public: daemonize and run the master process.

# File lib/resqued/daemon.rb, line 8
def run
  rd, wr = IO.pipe
  if fork
    # grandparent
    wr.close
    begin
      master_pid = rd.readpartial(16).to_i
      puts "Started master: #{master_pid}" if ENV["DEBUG"]
      exit
    rescue EOFError
      puts "Master process failed to start!"
      exit! 1
    end
  elsif fork
    # parent
    Process.setsid
    exit
  else
    # master
    STDIN.reopen "/dev/null"
    STDOUT.reopen "/dev/null", "a"
    STDERR.reopen "/dev/null", "a"
    rd.close
    @master.run(wr)
  end
end