[Zope-Checkins] CVS: Zope/lib/python/Signals - Signals.py:1.1 __init__.py:1.1

Chris McDonough chrism@zope.com
Tue, 6 Aug 2002 11:37:57 -0400


Update of /cvs-repository/Zope/lib/python/Signals
In directory cvs.zope.org:/tmp/cvs-serv3769/Signals

Added Files:
	Signals.py __init__.py 
Log Message:
Changed signal handler code.

SignalHandler module now lives in zdaemon package, so it may be shared
between ZEO and Zope.

The SignalHandler module in lib/python was removed in favor of the
module in zdaemon.

The zdaemon SignalHandler doesn't register any default signals.

There is a new package in lib/python named Signals.  This package
contains signal handlers and registry functions used by z2.py
to register Zope-specific signals with the SignalHandler registry.



=== Added File Zope/lib/python/Signals/Signals.py ===
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
# 
##############################################################################
"""
Zope signal handlers for clean shutdown, restart and log rotation.

$Id: Signals.py,v 1.1 2002/08/06 15:37:56 chrism Exp $
"""
__version__='$Revision: 1.1 $'[11:-2]

from zdaemon.SignalHandler import SignalHandler
import zLOG
import sys
import ZLogger

def shutdownHandler():
    """Shutdown cleanly on SIGTERM, SIGINT. This is registered first,
       so it should be called after all other handlers."""
    closeall()
    zLOG.LOG('Z2', zLOG.INFO , "Shutting down")
    sys.exit(0)

def restartHandler():
    """Restart cleanly on SIGHUP. This is registered first, so it
       should be called after all other SIGHUP handlers."""
    closeall()
    zLOG.LOG('Z2', zLOG.INFO , "Restarting")
    sys.exit(1)

def logfileReopenHandler():
    """Reopen log files on SIGUSR2. This is registered first, so it
       should be called after all other SIGUSR2 handlers."""
    zLOG.LOG('Z2', zLOG.INFO , "Reopening log files")
    reopen = getattr(getattr(sys, '__lg', None), 'reopen', None)
    if reopen is not None:
        reopen()
        zLOG.LOG('Z2', zLOG.BLATHER, "Reopened access log")
    reopen = getattr(getattr(sys, '__detailedlog', None), 'reopen', None)
    if reopen is not None:
        reopen()
        zLOG.LOG('Z2', zLOG.BLATHER,"Reopened detailed request 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 closeall():
    """Helper method to close network and database connections."""
    import Globals
    import asyncore
    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")
    for db in Globals.opened:
        try: db.close()
        finally: pass

def registerZopeSignals():
    import signal
    SignalHandler.registerHandler(signal.SIGTERM, shutdownHandler)
    SignalHandler.registerHandler(signal.SIGINT, shutdownHandler)
    SignalHandler.registerHandler(signal.SIGHUP, restartHandler)
    SignalHandler.registerHandler(signal.SIGUSR2, logfileReopenHandler)


=== Added File Zope/lib/python/Signals/__init__.py ===
"""
Signals package __init__.py
"""