JIRA: BOTTLENECKS-41 define the documents of vstf in B release
[bottlenecks.git] / vstf / vstf / common / daemon.py
index 1085d36..35933da 100755 (executable)
@@ -20,45 +20,46 @@ class Daemon(object):
     
     Usage: subclass the Daemon class and override the run() method
     """
     
     Usage: subclass the Daemon class and override the run() method
     """
+
     def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
         super(Daemon, self).__init__()
         self.stdin = stdin
         self.stdout = stdout
         self.stderr = stderr
         self.pidfile = pidfile
     def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
         super(Daemon, self).__init__()
         self.stdin = stdin
         self.stdout = stdout
         self.stderr = stderr
         self.pidfile = pidfile
-    
+
     def daemonize(self):
         """
         do the UNIX double-fork magic, see Stevens' "Advanced 
         Programming in the UNIX Environment" for details (ISBN 0201563177)
         http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
         """
     def daemonize(self):
         """
         do the UNIX double-fork magic, see Stevens' "Advanced 
         Programming in the UNIX Environment" for details (ISBN 0201563177)
         http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
         """
-        try: 
-            pid = os.fork() 
+        try:
+            pid = os.fork()
             if pid > 0:
             if pid > 0:
-                sys.exit(0) 
-        except OSError, e: 
+                sys.exit(0)
+        except OSError, e:
             LOG.error("fork #1 failed: %(errno)s, %(strerror)s",
             LOG.error("fork #1 failed: %(errno)s, %(strerror)s",
-                      {'errno':e.errno, 'strerror': e.strerror})
+                      {'errno': e.errno, 'strerror': e.strerror})
             sys.exit(1)
             sys.exit(1)
-    
+
         # decouple from parent environment
         # decouple from parent environment
-        os.chdir("/") 
-        os.setsid() 
-        os.umask(0) 
-    
+        os.chdir("/")
+        os.setsid()
+        os.umask(0)
+
         # do second fork
         # do second fork
-        try: 
-            pid = os.fork() 
+        try:
+            pid = os.fork()
             if pid > 0:
                 # exit from second parent
             if pid > 0:
                 # exit from second parent
-                sys.exit(0) 
-        except OSError, e: 
+                sys.exit(0)
+        except OSError, e:
             LOG.error("fork #1 failed: %(errno)s, %(strerror)s",
             LOG.error("fork #1 failed: %(errno)s, %(strerror)s",
-                      {'errno':e.errno, 'strerror': e.strerror})
-            sys.exit(1) 
-    
-        # redirect standard file descriptors
+                      {'errno': e.errno, 'strerror': e.strerror})
+            sys.exit(1)
+
+            # redirect standard file descriptors
         sys.stdout.flush()
         sys.stderr.flush()
         si = file(self.stdin, 'r')
         sys.stdout.flush()
         sys.stderr.flush()
         si = file(self.stdin, 'r')
@@ -67,12 +68,12 @@ class Daemon(object):
         os.dup2(si.fileno(), sys.stdin.fileno())
         os.dup2(so.fileno(), sys.stdout.fileno())
         os.dup2(se.fileno(), sys.stderr.fileno())
         os.dup2(si.fileno(), sys.stdin.fileno())
         os.dup2(so.fileno(), sys.stdout.fileno())
         os.dup2(se.fileno(), sys.stderr.fileno())
-    
+
         # write pidfile
         atexit.register(self.delpid)
         pid = str(os.getpid())
         # write pidfile
         atexit.register(self.delpid)
         pid = str(os.getpid())
-        file(self.pidfile,'w+').write("%s\n" % pid)
-    
+        file(self.pidfile, 'w+').write("%s\n" % pid)
+
     def delpid(self):
         os.remove(self.pidfile)
 
     def delpid(self):
         os.remove(self.pidfile)
 
@@ -80,15 +81,15 @@ class Daemon(object):
         """
         Start the daemon
         """
         """
         Start the daemon
         """
-        
+
         # Check for a pidfile to see if the daemon already runs
         try:
         # Check for a pidfile to see if the daemon already runs
         try:
-            pf = file(self.pidfile,'r')
+            pf = file(self.pidfile, 'r')
             pid = int(pf.read().strip())
             pf.close()
         except IOError:
             pid = None
             pid = int(pf.read().strip())
             pf.close()
         except IOError:
             pid = None
-    
+
         if pid:
             message = "pidfile %s already exist. Daemon already running?\n"
             sys.stderr.write(message % self.pidfile)
         if pid:
             message = "pidfile %s already exist. Daemon already running?\n"
             sys.stderr.write(message % self.pidfile)
@@ -104,16 +105,16 @@ class Daemon(object):
         """
         # Get the pid from the pidfile
         try:
         """
         # Get the pid from the pidfile
         try:
-            pf = file(self.pidfile,'r')
+            pf = file(self.pidfile, 'r')
             pid = int(pf.read().strip())
             pf.close()
         except IOError:
             pid = None
             pid = int(pf.read().strip())
             pf.close()
         except IOError:
             pid = None
-    
+
         if not pid:
             message = "pidfile %s does not exist. Daemon not running?\n"
             sys.stderr.write(message % self.pidfile)
         if not pid:
             message = "pidfile %s does not exist. Daemon not running?\n"
             sys.stderr.write(message % self.pidfile)
-            return # not an error in a restart
+            return  # not an error in a restart
 
         # Try killing the daemon process    
         try:
 
         # Try killing the daemon process    
         try:
@@ -144,10 +145,10 @@ class Daemon(object):
         
         """
         pass
         
         """
         pass
-        
+
     def daemon_die(self):
     def daemon_die(self):
-        """You should this method when you shutdown daemon
+        """You should override this method when you shutdown daemon
         this func will be call by stop() before kill the process
         
         """
         this func will be call by stop() before kill the process
         
         """
-        pass
\ No newline at end of file
+        pass