Proper handle of sigterm signal, log reason of shutdown
This commit is contained in:
parent
16f36824e6
commit
faba28dd94
2 changed files with 23 additions and 20 deletions
|
@ -1,16 +1,18 @@
|
|||
import sys
|
||||
import logging
|
||||
import signal
|
||||
import importlib
|
||||
|
||||
import gevent
|
||||
import gevent.hub
|
||||
|
||||
from Config import config
|
||||
import importlib
|
||||
from . import Debug
|
||||
|
||||
last_error = None
|
||||
|
||||
def shutdown():
|
||||
print("Shutting down...")
|
||||
def shutdown(reason="Unknown"):
|
||||
logging.info("Shutting down (reason: %s)..." % reason)
|
||||
if "file_server" in dir(sys.modules["main"]) and sys.modules["main"].file_server.running:
|
||||
try:
|
||||
if "file_server" in dir(sys.modules["main"]):
|
||||
|
@ -24,7 +26,7 @@ def shutdown():
|
|||
sys.exit(0)
|
||||
|
||||
# Store last error, ignore notify, allow manual error logging
|
||||
def handleError(*args):
|
||||
def handleError(*args, **kwargs):
|
||||
global last_error
|
||||
if not args: # Manual called
|
||||
args = sys.exc_info()
|
||||
|
@ -33,22 +35,23 @@ def handleError(*args):
|
|||
silent = False
|
||||
if args[0].__name__ != "Notify":
|
||||
last_error = args
|
||||
|
||||
if args[0].__name__ == "KeyboardInterrupt":
|
||||
shutdown()
|
||||
return
|
||||
if not silent and args[0].__name__ != "Notify":
|
||||
shutdown("Keyboard interrupt")
|
||||
elif not silent and args[0].__name__ != "Notify":
|
||||
logging.exception("Unhandled exception")
|
||||
if "greenlet.py" not in args[2].tb_frame.f_code.co_filename: # Don't display error twice
|
||||
sys.__excepthook__(*args)
|
||||
sys.__excepthook__(*args, **kwargs)
|
||||
|
||||
|
||||
# Ignore notify errors
|
||||
def handleErrorNotify(*args):
|
||||
if args[0].__name__ == "KeyboardInterrupt":
|
||||
shutdown()
|
||||
if args[0].__name__ != "Notify":
|
||||
logging.exception("Unhandled exception")
|
||||
sys.__excepthook__(*args)
|
||||
def handleErrorNotify(*args, **kwargs):
|
||||
err = args[0]
|
||||
if err.__name__ == "KeyboardInterrupt":
|
||||
shutdown("Keyboard interrupt")
|
||||
elif err.__name__ != "Notify":
|
||||
logging.error("Unhandled exception: %s" % [args])
|
||||
sys.__excepthook__(*args, **kwargs)
|
||||
|
||||
|
||||
if config.debug: # Keep last error for /Debug
|
||||
|
@ -80,6 +83,12 @@ def handleGreenletError(self, context, type, value, tb):
|
|||
|
||||
gevent.hub.Hub.handle_error = handleGreenletError
|
||||
|
||||
try:
|
||||
signal.signal(signal.SIGTERM, lambda signum, stack_frame: shutdown("SIGTERM"))
|
||||
except Exception as err:
|
||||
logging.debug("Error setting up SIGTERM watcher: %s" % err)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import time
|
||||
from gevent import monkey
|
||||
|
|
|
@ -15,12 +15,6 @@ def main():
|
|||
|
||||
main = None
|
||||
try:
|
||||
import signal
|
||||
try:
|
||||
signal.signal(signal.SIGTERM, lambda signum, stack_frame: sys.exit(0))
|
||||
except Exception as err:
|
||||
print("Error setting up SIGTERM watcher: %s" % err)
|
||||
|
||||
app_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
os.chdir(app_dir) # Change working dir to zeronet.py dir
|
||||
sys.path.insert(0, os.path.join(app_dir, "src/lib")) # External liblary directory
|
||||
|
|
Loading…
Reference in a new issue