[Zope-Checkins] CVS: Zope - z2.py:1.56.2.3

Andreas Jung andreas@digicool.com
Thu, 24 Jan 2002 11:27:33 -0500


Update of /cvs-repository/Zope
In directory cvs.zope.org:/tmp/cvs-serv23011

Modified Files:
      Tag: chrism_logrotate_branch
	z2.py 
Log Message:
rewrote signal handling code
- SIGUSR2 works to rotate logs
- SIGHUP|TERM work but code to close all open stuff does not work properly


=== Zope/z2.py 1.56.2.2 => 1.56.2.3 ===
 Zpid=''
 
+
+#
+#   Install signal handlers for SIGHUP and SIGTERM, to permit
+#   clean shutdown/restart from the command line.
+#
+if os.name == 'posix':  # signal.signal() not reliable on Windos
+    import signal
+
+    def closeall():
+
+        import Globals
+
+        for socket in asyncore.socket_map.values():
+            socket.close()
+
+        for db in Globals.opened:
+            try:
+                db.close()
+            finally:
+                pass
+
+
+    def handler( signum, frame, die=signal.SIGTERM ):
+
+        zLOG.LOG( 'z2', zLOG.INFO , "Caught signal  (%s)" % signum )
+
+        if signum==die: 
+            zLOG.LOG( 'z2', zLOG.INFO , "Shutting down" )
+            closeall()
+            sys.exit(0)
+
+        # reopen log files
+
+        sys.__lg.reopen()
+        DebugLogger.reopen()
+        zLOG.LOG( 'z2', zLOG.INFO , "Logfiles reopened" )
+
+        if signum == signal.SIGUSR2: return
+        
+        if signum == signal.SIGHUP: 
+            zLOG.LOG( 'z2', zLOG.INFO , "Restarting")
+            closeall()
+            sys.exit(1)
+
+        return 
+
+
+
+    def installhandler():
+        signal.signal( signal.SIGHUP, handler )
+        signal.signal( signal.SIGTERM, handler )
+        signal.signal( signal.SIGUSR2, handler )
+        zLOG.LOG( 'z2', zLOG.INFO , "Signal handlers installed")
+
+
+
 ########################################################################
 # Configuration section
 
@@ -470,8 +526,8 @@
 if Zpid and not READ_ONLY:
     import zdaemon, App.FindHomes, posix
     sys.ZMANAGED=1
-    
-    zdaemon.run(sys.argv, os.path.join(CLIENT_HOME, Zpid))
+
+#    zdaemon.run(sys.argv, os.path.join(CLIENT_HOME, Zpid))
 
 try:
     # Import logging support
@@ -489,7 +545,9 @@
     if DETAILED_LOG_FILE:
         from ZServer import DebugLogger
         logfile=os.path.join(CLIENT_HOME, DETAILED_LOG_FILE)
-        DebugLogger.log=DebugLogger.DebugLogger(logfile).log
+        DL=DebugLogger.DebugLogger(logfile)
+        DebugLogger.log=DL.log
+        DebugLogger.reopen=DL.reopen
 
     # Import Zope (or Main)
     exec "import "+MODULE in {}
@@ -544,6 +602,7 @@
         lg = logger.syslog_logger((addr, int(port)))
     else:
         lg = logger.file_logger(LOG_PATH)
+    sys.__lg = lg
 
     # HTTP Server
     if HTTP_PORT:
@@ -681,38 +740,8 @@
     except:
         pass
 
-    #
-    #   Install signal handlers for SIGHUP and SIGTERM, to permit
-    #   clean shutdown/restart from the command line.
-    #
-    if os.name == 'posix':  # signal.signal() not reliable on Windos
-        try:
-            import Globals, signal
-            def handler( signum, frame, die=signal.SIGTERM ):
-                for socket in asyncore.socket_map.values():
-                    socket.close()
-
-                for db in Globals.opened:
-                    try:
-                        db.close()
-                    finally:
-                        pass
-                try:
-                    zLOG.LOG( 'z2', zLOG.INFO
-                            , "Shutting down (signal %s)" % signum )
-                except:
-                    pass
-
-                if signum==die:
-                    sys.exit(0)
-                else:
-                    sys.exit(1)
-
-            signal.signal( signal.SIGTERM, handler )
-            signal.signal( signal.SIGHUP, handler )
-
-        finally:
-            pass
+    if os.name == 'posix':
+        installhandler()
 
 
     # if it hasn't failed at this point, create a .pid file.