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

Chris McDonough chrism@zope.com
Fri, 15 Mar 2002 14:36:33 -0500


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

Modified Files:
      Tag: chrism_logrotate_branch
	z2.py 
Log Message:
Patched up z2.py and some of asyncore to be able to deal with signals.

In this branch, Zope will:

- open and close all of its logfiles upon receving a USR2 signal

- restart "cleanly" upon receiving a HUP signal

- shutdown "cleanly" upon receiving a TERM or INT signal.



=== Zope/z2.py 1.56.2.4 => 1.56.2.5 ===
 
 
-#
-#   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
+#   Install signal handlers.
+#   SIGTERM - cleanly shuts down.
+#   SIGHUP - cleanly restarts.
+#   SIGUSR2 - open and close all log files (for log rotation)
+
+if os.name == 'posix':  # signal.signal() not reliable on Windows
     import signal
+    sig_dict = {}
+    for k,v in signal.__dict__.items():
+        if isinstance(v, type(1)) and k[:3] == 'SIG':
+            sig_dict[v] = k
 
     def closeall():
+        zLOG.LOG('Z2', zLOG.INFO, "Closing all open network connections")
         for socket in asyncore.socket_map.values():
             try:
                 socket.close()
             except:
                 pass
 
+        zLOG.LOG('Z2', zLOG.INFO, "Closing all open ZODB databases")
         import Globals
         for db in Globals.opened:
             try:
@@ -252,36 +259,48 @@
             finally:
                 pass
 
+    def sighandler(signum, frame):
+        signame = sig_dict.get(signum, signum)
+        zLOG.LOG('Z2', zLOG.INFO , "Caught signal %s" % signame)
 
-    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" )
+        if signum in [signal.SIGTERM, signal.SIGINT]:
             closeall()
+            zLOG.LOG('Z2', zLOG.INFO , "Shutting down")
             sys.exit(0)
 
-        if signum == signal.SIGHUP: 
-            zLOG.LOG( 'z2', zLOG.INFO , "Restarting")
+        if signum == signal.SIGHUP:
             closeall()
+            zLOG.LOG('Z2', zLOG.INFO , "Restarting")
             sys.exit(1)
 
         if signum == signal.SIGUSR2:
-            zLOG.LOG( 'z2', zLOG.INFO , "Reopening log files" )
+            zLOG.LOG('Z2', zLOG.INFO , "Reopening log files")
             sys.__lg.reopen()
-            zLOG.LOG('z2', zLOG.BLATHER, "Reopened Z2.log")
+            zLOG.LOG('Z2', zLOG.BLATHER, "Reopened Z2.log")
             if hasattr(sys, '__detailedlog'):
-                zLOG.LOG('z2', zLOG.BLATHER, "Reopened detailed request log")
+                zLOG.LOG('Z2', zLOG.BLATHER, "Reopened detailed request log")
                 sys.__detailedlog.reopen()
-            zLOG._set_log_dest(None)
-            zLOG.LOG('z2', zLOG.BLATHER, "Reopened debug log")
+            if hasattr(zLOG, '_set_stupid_dest'):
+                zLOG._set_stupid_dest(None)
+            else:
+                zLOG._stupid_dest = None
+            ZLogger.stupidFileLogger._stupid_dest = None
+            zLOG.LOG('Z2', zLOG.BLATHER, "Reopened event log")
+            zLOG.LOG('Z2', zLOG.INFO, "Log files reopened successfully")
         
-    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")
-
+    INTERESTING_SIGNALS = {
+        signal.SIGTERM: sighandler,
+        signal.SIGHUP: sighandler,
+        signal.SIGUSR2: sighandler,
+        signal.SIGINT: sighandler,
+        }
+
+    def installsighandlers():
+        # normal kill, restart, and open&close logs respectively
+        for signum, sighandler in INTERESTING_SIGNALS.items():
+            signal.signal(signum, sighandler)
+            signame = sig_dict.get(signum, signum)
+            zLOG.LOG('Z2',zLOG.BLATHER,"Installed sighandler for %s" % signame)
 
 
 ########################################################################
@@ -525,7 +544,8 @@
     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),
+                INTERESTING_SIGNALS.keys())
 
 try:
     # Import logging support
@@ -740,9 +760,9 @@
     except:
         pass
 
+    # we've deferred til now to actuall install signal handlers
     if os.name == 'posix':
-        installhandler()
-
+        installsighandlers()
 
     # if it hasn't failed at this point, create a .pid file.
     if not READ_ONLY: