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 sys
|
||||||
import logging
|
import logging
|
||||||
|
import signal
|
||||||
|
import importlib
|
||||||
|
|
||||||
import gevent
|
import gevent
|
||||||
import gevent.hub
|
import gevent.hub
|
||||||
|
|
||||||
from Config import config
|
from Config import config
|
||||||
import importlib
|
from . import Debug
|
||||||
|
|
||||||
last_error = None
|
last_error = None
|
||||||
|
|
||||||
def shutdown():
|
def shutdown(reason="Unknown"):
|
||||||
print("Shutting down...")
|
logging.info("Shutting down (reason: %s)..." % reason)
|
||||||
if "file_server" in dir(sys.modules["main"]) and sys.modules["main"].file_server.running:
|
if "file_server" in dir(sys.modules["main"]) and sys.modules["main"].file_server.running:
|
||||||
try:
|
try:
|
||||||
if "file_server" in dir(sys.modules["main"]):
|
if "file_server" in dir(sys.modules["main"]):
|
||||||
|
@ -24,7 +26,7 @@ def shutdown():
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# Store last error, ignore notify, allow manual error logging
|
# Store last error, ignore notify, allow manual error logging
|
||||||
def handleError(*args):
|
def handleError(*args, **kwargs):
|
||||||
global last_error
|
global last_error
|
||||||
if not args: # Manual called
|
if not args: # Manual called
|
||||||
args = sys.exc_info()
|
args = sys.exc_info()
|
||||||
|
@ -33,22 +35,23 @@ def handleError(*args):
|
||||||
silent = False
|
silent = False
|
||||||
if args[0].__name__ != "Notify":
|
if args[0].__name__ != "Notify":
|
||||||
last_error = args
|
last_error = args
|
||||||
|
|
||||||
if args[0].__name__ == "KeyboardInterrupt":
|
if args[0].__name__ == "KeyboardInterrupt":
|
||||||
shutdown()
|
shutdown("Keyboard interrupt")
|
||||||
return
|
elif not silent and args[0].__name__ != "Notify":
|
||||||
if not silent and args[0].__name__ != "Notify":
|
|
||||||
logging.exception("Unhandled exception")
|
logging.exception("Unhandled exception")
|
||||||
if "greenlet.py" not in args[2].tb_frame.f_code.co_filename: # Don't display error twice
|
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
|
# Ignore notify errors
|
||||||
def handleErrorNotify(*args):
|
def handleErrorNotify(*args, **kwargs):
|
||||||
if args[0].__name__ == "KeyboardInterrupt":
|
err = args[0]
|
||||||
shutdown()
|
if err.__name__ == "KeyboardInterrupt":
|
||||||
if args[0].__name__ != "Notify":
|
shutdown("Keyboard interrupt")
|
||||||
logging.exception("Unhandled exception")
|
elif err.__name__ != "Notify":
|
||||||
sys.__excepthook__(*args)
|
logging.error("Unhandled exception: %s" % [args])
|
||||||
|
sys.__excepthook__(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
if config.debug: # Keep last error for /Debug
|
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
|
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__":
|
if __name__ == "__main__":
|
||||||
import time
|
import time
|
||||||
from gevent import monkey
|
from gevent import monkey
|
||||||
|
|
|
@ -15,12 +15,6 @@ def main():
|
||||||
|
|
||||||
main = None
|
main = None
|
||||||
try:
|
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__))
|
app_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
os.chdir(app_dir) # Change working dir to zeronet.py dir
|
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
|
sys.path.insert(0, os.path.join(app_dir, "src/lib")) # External liblary directory
|
||||||
|
|
Loading…
Reference in a new issue