Class: TaskJuggler::Daemon

Inherits:
Object show all
Includes:
MessageHandler
Defined in:
lib/taskjuggler/daemon/Daemon.rb

Overview

This class provides the basic functionality to turn the current process into a background process (daemon). To use it, derive you main class from this class and call the start() method.

Direct Known Subclasses

ProjectBroker, WebServer

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MessageHandler

#critical, #debug, #error, #fatal, #info, #warning

Constructor Details

#initializeDaemon

Returns a new instance of Daemon.



27
28
29
30
31
32
33
34
# File 'lib/taskjuggler/daemon/Daemon.rb', line 27

def initialize
  # You can set this flag to false to prevent the program from
  # disconnecting from the current terminal. This is useful for debugging
  # purposes.
  @daemonize = true
  # Save the PID of the running daemon as number into this file.
  @pidFile = nil
end

Instance Attribute Details

#daemonizeObject

Returns the value of attribute daemonize.



25
26
27
# File 'lib/taskjuggler/daemon/Daemon.rb', line 25

def daemonize
  @daemonize
end

#pidFileObject

Returns the value of attribute pidFile.



25
26
27
# File 'lib/taskjuggler/daemon/Daemon.rb', line 25

def pidFile
  @pidFile
end

Instance Method Details

#startObject

Call this method to turn the process into a background process.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/taskjuggler/daemon/Daemon.rb', line 37

def start
  return 0 unless @daemonize

  # Fork and have the parent exit
  if (pid = fork) == -1
    fatal('first_fork_failed', 'First fork failed')
  elsif !pid.nil?
    # This is the parent. We can exit now.
    debug('', "Forked a child process with PID #{pid}")
    exit! 0
  end

  # Create a new session
  Process.setsid
  # Fork again to make sure we lose the controlling terminal
  if (pid = fork) == -1
    fatal('second_fork_failed', 'Second fork failed')
  elsif !pid.nil?
    # This is the parent. We can exit now.
    debug('', "Forked a child process with PID #{pid}")
    exit! 0
  end

  @pid = Process.pid

  writePidFile

  # Change current working directory to the file system root
  Dir.chdir '/'
  # Make sure we can create files with any permission
  File.umask 0

  # We no longer have a controlling terminal, so these are useless.
  $stdin.reopen('/dev/null')
  $stdout.reopen('/dev/null', 'a')
  $stderr.reopen($stdout)

  info('daemon_pid',
       "The process is running as daemon now with PID #{@pid}")

  0
end

#stopObject

This method may provide some cleanup functionality in the future. You better call it before you exit.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/taskjuggler/daemon/Daemon.rb', line 82

def stop
  if @pidFile
    begin
      File.delete(@pidFile)
    rescue
      warning('cannot_delete_pidfile',
              "Cannote delete the PID file (#{@pidFile}): #{$!}")
    end
    info('daemon_deleted_pidfile', "PID file #{@pidFile} deleted")
  end
end