Commit c6ed1908 authored by Hanno Schlichting's avatar Hanno Schlichting

Make the dependency between Signals and Lifetime optional.

parent 966f7dfa
import asyncore
import logging
import sys
import time
from Signals.threads import dump_threads
logger = logging.getLogger("Z2")
_shutdown_phase = 0
_shutdown_timeout = 30 # seconds per phase
......@@ -84,3 +90,30 @@ def graceful_shutdown_loop():
# No vetos? That is one step closer to shutting down
_shutdown_phase += 1
timestamp = time.time()
def shutdownFastHandler():
"""Shutdown cleanly on SIGTERM. This is registered first,
so it should be called after all other handlers."""
logger.info("Shutting down fast")
shutdown(0, fast=1)
def shutdownHandler():
"""Shutdown cleanly on SIGINT. This is registered first,
so it should be called after all other handlers."""
logger.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."""
logger.info("Restarting")
shutdown(1)
def showStacks():
"""Dump a stracktrace of all threads on the console."""
print(dump_threads())
sys.stdout.flush()
......@@ -16,11 +16,17 @@ Zope signal handlers for clean shutdown, restart and log rotation.
import logging
import os
import sys
import Lifetime
from .threads import dump_threads
LIFETIME = True
try:
from Lifetime import (
shutdownFastHandler,
shutdownHandler,
restartHandler,
showStacks,
)
except ImportError:
LIFETIME = False
logger = logging.getLogger("Z2")
......@@ -37,33 +43,6 @@ else:
from SignalHandler import SignalHandler
def shutdownFastHandler():
"""Shutdown cleanly on SIGTERM. This is registered first,
so it should be called after all other handlers."""
logger.info("Shutting down fast")
Lifetime.shutdown(0, fast=1)
def shutdownHandler():
"""Shutdown cleanly on SIGINT. This is registered first,
so it should be called after all other handlers."""
logger.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."""
logger.info("Restarting")
Lifetime.shutdown(1)
def showStacks():
"""Dump a stracktrace of all threads on the console."""
print(dump_threads())
sys.stdout.flush()
class LogfileReopenHandler(object):
"""Reopen log files on SIGUSR2.
......@@ -120,11 +99,11 @@ def registerZopeSignals(loggers):
except ImportError:
mod_wsgi = False
if not mod_wsgi:
if not mod_wsgi and LIFETIME:
SignalHandler.registerHandler(SIGTERM, shutdownFastHandler)
SignalHandler.registerHandler(SIGINT, shutdownHandler)
if os.name != 'nt':
if not mod_wsgi:
if not mod_wsgi and LIFETIME:
SignalHandler.registerHandler(SIGHUP, restartHandler)
SignalHandler.registerHandler(SIGUSR1, showStacks)
SignalHandler.registerHandler(SIGUSR2, LogfileReopenHandler(loggers))
......
......@@ -41,13 +41,13 @@
# to add yet another asyncore handler - the thread in this module could
# then "post" the request to the main thread via this asyncore handler.
import asyncore
import atexit
import logging
import os
import sys
import signal
import threading
import asyncore
import atexit
import Lifetime
# As at pywin32-204, we must ensure pywintypes is the first win32 module
# imported in our process, otherwise we can end up with 2 pywintypesxx.dll
......@@ -62,7 +62,12 @@ import win32con
import win32event
import ntsecuritycon
import logging
LIFETIME = True
try:
from Lifetime import shutdown
except ImportError:
LIFETIME = False
logger = logging.getLogger("WinSignalHandler")
# We simulate signals via win32 named events. This is the event name
......@@ -233,9 +238,12 @@ class SignalHandler(object):
# SystemExit does the right thing. On Windows, we are on
# our own thread, so throwing SystemExit there isn't a great
# idea. Just shutdown the main loop.
logger.debug(
"Trapped SystemExit(%s) - doing Lifetime shutdown" % rc)
Lifetime.shutdown(rc)
if LIFETIME:
logger.debug("Trapped SystemExit(%s) - "
"doing Lifetime shutdown" % rc)
shutdown(rc)
else:
raise
except:
logger.exception("A handler for %s failed!'" % signame)
wakeSelect() # trigger a walk around the Lifetime loop.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment