Class: Daemon

Inherits:
Object show all
Defined in:
lib/carat-dev/daemon/daemon.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug = $DEBUG) ⇒ Daemon

Returns a new instance of Daemon.



22
23
24
# File 'lib/carat-dev/daemon/daemon.rb', line 22

def initialize( debug = $DEBUG )
  @debug = $DEBUG
end

Class Method Details

.exec(port, dir, debug = $DEBUG) ⇒ Object



18
19
20
# File 'lib/carat-dev/daemon/daemon.rb', line 18

def self.exec( port, dir, debug = $DEBUG )
  new(debug).exec port, dir
end

Instance Method Details

#exec(port, dir) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/carat-dev/daemon/daemon.rb', line 26

def exec( port, dir )
  if @debug then
    Dir.chdir dir
    wait 
    return
  end

  fork do
    Process.setsid
    fork do
      Dir.chdir dir
      $stdin.close
      $stdout.close
      $stderr.close

      wait
    end

    exit!
  end
  exit!
end

#waitObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/carat-dev/daemon/daemon.rb', line 49

def wait
  s = TCPServer.new( 10070 )

  unless @debug then
    trap( 'SIGINT' ) { exit! }
    trap( 'SIGHUP' ) { exit! }
    trap( 'SIGTERM' ) { exit! }
  end

  while true do
    @sock = s.accept
    begin
      session
    ensure
      @sock.close
    end
  end
end