Module: Djinn::Base::Tonic

Included in:
Djinn::Base
Defined in:
lib/djinn/base/tonic.rb

Overview

This play on words kills me everytime. I’m that lame.

Instance Method Summary collapse

Instance Method Details

#daemonize(logfile, pidfile, &block) ⇒ Object

Send your Djinn off to frolic in the ether



7
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/djinn/base/tonic.rb', line 7

def daemonize(logfile, pidfile, &block)
  pidfile.ensure_empty
  puts "Djinn is leaving process #{$$}"

  srand # split rand streams between spawning and daemonized process

  fork do
    puts "Daemonizing on process #{$$}" 
    # puts system("ps aux | grep #{$$}")
    
    #Dir.chdir "/" # release old working directory
    File.umask 0000 # ensure sensible umask
    
    # Making sure all file descriptors are closed
    ObjectSpace.each_object(IO) do |io|
      unless [STDIN, STDOUT, STDERR].include?(io)
        begin
          io.close unless io.closed?
        rescue ::Exception
        end
      end
    end
  
    pidfile.create # write PID file
  
    # detach from controlling terminal
    unless sess_id = Process.setsid
      raise 'Cannot detach from controlling terminal'
    end
    
    # redirect IO
    STDIN.reopen('/dev/null')
    STDOUT.reopen(logfile, 'a')
    STDERR.reopen(STDOUT)
    
    yield
    
  end
    
end