Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Static Public Attributes
MoinMoin.util.daemon.Daemon Class Reference
Inheritance diagram for MoinMoin.util.daemon.Daemon:
Inheritance graph
[legend]
Collaboration diagram for MoinMoin.util.daemon.Daemon:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def do_start
def do_stop
def do_kill
def do_restart
def status
def readPID
def daemonize
def writePID
def removePID
def warn
def log

Public Attributes

 name
 function
 args
 kw
 pidFile

Static Public Attributes

string commandPrefix = 'do_'

Detailed Description

A background process

Represent a background process, which may be running or not. The
process can be started, stopped, restarted or killed.

Definition at line 57 of file daemon.py.


Constructor & Destructor Documentation

def MoinMoin.util.daemon.Daemon.__init__ (   self,
  name,
  pidfile,
  function,
  args,
  kw 
)
Create a daemon

@param name: name of the process
@param pidfile: pid filename
@param function: the server main function, will block until the
    server is done.
@param args: arguments to pass to function
@param kw: keyword arguments to pass to function

Definition at line 65 of file daemon.py.

00065 
00066     def __init__(self, name, pidfile, function, *args, **kw):
00067         """ Create a daemon
00068 
00069         @param name: name of the process
00070         @param pidfile: pid filename
00071         @param function: the server main function, will block until the
00072             server is done.
00073         @param args: arguments to pass to function
00074         @param kw: keyword arguments to pass to function
00075         """
00076         self.name = name
00077         self.function = function
00078         self.args = args
00079         self.kw = kw
00080         self.pidFile = os.path.abspath(pidfile)


Member Function Documentation

Make the current process a daemon

See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16

Definition at line 183 of file daemon.py.

00183 
00184     def daemonize(self):
00185         """ Make the current process a daemon
00186 
00187         See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16
00188         """
00189         if os.fork():   # launch child and...
00190             os._exit(0) # kill off parent
00191         os.setsid()
00192         if os.fork():   # launch child and...
00193             os._exit(0) # kill off parent again.
00194         os.umask(0077)
00195         null = os.open('/dev/null', os.O_RDWR)
00196         for i in range(3):
00197             try:
00198                 os.dup2(null, i)
00199             except OSError, e:
00200                 if e.errno != errno.EBADF:
00201                     raise
00202         os.close(null)

Here is the caller graph for this function:

Kill the daemon process

Kill or raise an error which we can't handle here. Clean the
pid file for the killed process.

Definition at line 112 of file daemon.py.

00112 
00113     def do_kill(self):
00114         """ Kill the daemon process
00115 
00116         Kill or raise an error which we can't handle here. Clean the
00117         pid file for the killed process.
00118         """
00119         running, pid = self.status()
00120         if not running:
00121             return self.log("%s is not running" % self.name)
00122         os.kill(pid, signal.SIGKILL)
00123         self.removePID()

Here is the call graph for this function:

stop, wait until pid file gone and start again 

Definition at line 124 of file daemon.py.

00124 
00125     def do_restart(self):
00126         """ stop, wait until pid file gone and start again """
00127         running, pid = self.status()
00128         if not running:
00129             self.log("%s is not running, trying to start" % self.name)
00130         else:
00131             self.do_stop()
00132         timeoutSeconds = 2.0
00133         start = time.time()
00134         while time.time() - start < timeoutSeconds:
00135             running, pid = self.status()
00136             if not running:
00137                 break
00138             time.sleep(0.1)
00139         else:
00140             raise Error("could not start after %s seconds" % timeoutSeconds)
00141         self.do_start()

Here is the call graph for this function:

Start the daemon process

Start will daemonize then block until the server is killed and
then cleanup the pid file on the way out.

Definition at line 84 of file daemon.py.

00084 
00085     def do_start(self):
00086         """ Start the daemon process
00087 
00088         Start will daemonize then block until the server is killed and
00089         then cleanup the pid file on the way out.
00090         """
00091         running, pid = self.status()
00092         if running:
00093             raise Error("another application is running with pid %s "
00094                         "(try restart)" % pid)
00095         self.daemonize()
00096         self.writePID()
00097         try:
00098             self.function(*self.args, **self.kw)
00099         finally:
00100             self.removePID()

Here is the call graph for this function:

Here is the caller graph for this function:

Stop the daemon process

Terminate or raise an error we can't handle here. On success,
the pid file will be cleaned by the terminated process.

Definition at line 101 of file daemon.py.

00101 
00102     def do_stop(self):
00103         """ Stop the daemon process
00104 
00105         Terminate or raise an error we can't handle here. On success,
00106         the pid file will be cleaned by the terminated process.
00107         """
00108         running, pid = self.status()
00109         if not running:
00110             return self.log("%s is not running" % self.name)
00111         os.kill(pid, signal.SIGINT)

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.util.daemon.Daemon.log (   self,
  message 
)
TODO: does it work after daemonize? 

Definition at line 217 of file daemon.py.

00217 
00218     def log(self, message):
00219         """ TODO: does it work after daemonize? """
00220         sys.stderr.write(message + '\n')
00221 

Here is the caller graph for this function:

Return the pid from the pid file

If there is no pid file, return None. If pid file is corrupted,
remove it. If its not readable, raise.

Definition at line 166 of file daemon.py.

00166 
00167     def readPID(self):
00168         """ Return the pid from the pid file
00169 
00170         If there is no pid file, return None. If pid file is corrupted,
00171         remove it. If its not readable, raise.
00172         """
00173         pid = None
00174         try:
00175             pid = int(file(self.pidFile).read())
00176         except IOError, err:
00177             if err.errno != errno.ENOENT:
00178                 raise
00179         except ValueError:
00180             self.warn("removing corrupted pid file: %s" % self.pidFile)
00181             self.removePID()
00182         return pid

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 207 of file daemon.py.

00207 
00208     def removePID(self):
00209         try:
00210             os.remove(self.pidFile)
00211         except OSError, err:
00212             if err.errno != errno.ENOENT:
00213                 raise

Here is the caller graph for this function:

Return status tuple (running, pid)

See http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC18

Definition at line 145 of file daemon.py.

00145 
00146     def status(self):
00147         """ Return status tuple (running, pid)
00148 
00149         See http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC18
00150         """
00151         running = False
00152         pid = self.readPID()
00153         if pid is not None:
00154             try:
00155                 os.kill(pid, 0)
00156                 running = True
00157             except OSError, err:
00158                 if err.errno == errno.ESRCH:
00159                     # No such process or security enhancements are causing
00160                     # the system to deny its existence.
00161                     self.log("removing stale pid file: %s" % self.pidFile)
00162                     self.removePID()
00163                 else:
00164                     raise
00165         return running, pid

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.util.daemon.Daemon.warn (   self,
  message 
)

Definition at line 214 of file daemon.py.

00214 
00215     def warn(self, message):
00216         self.log('warning: %s' % message)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 203 of file daemon.py.

00203 
00204     def writePID(self):
00205         pid = str(os.getpid())
00206         open(self.pidFile, 'wb').write(pid)

Here is the caller graph for this function:


Member Data Documentation

Definition at line 77 of file daemon.py.

Definition at line 63 of file daemon.py.

Definition at line 76 of file daemon.py.

Definition at line 78 of file daemon.py.

Definition at line 75 of file daemon.py.

Definition at line 79 of file daemon.py.


The documentation for this class was generated from the following file: