Class: Resqued::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/resqued/daemon.rb

Instance Method Summary collapse

Constructor Details

#initialize(master) ⇒ Daemon

Returns a new instance of Daemon.



3
4
5
# File 'lib/resqued/daemon.rb', line 3

def initialize(master)
  @master = master
end

Instance Method Details

#runObject

Public: daemonize and run the master process.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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
      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