From 401ebcf68559999beb97da7179704a5bc9e613ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 25 Jun 2015 15:18:33 +0200 Subject: [PATCH] Fix: Cleanly exit on SIGTERM --- CHANGELOG.md | 7 +++++++ src/octoprint/server/__init__.py | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe57f3e8..d754f68e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # OctoPrint Changelog +## 1.2.1 (unreleased) + +### Bug Fixes + +* [IRC] - OctoPrint will now exit cleanly on `SIGTERM`, calling the shutdown functions provided by plugins. + Thanks @Salandora for the heads-up. + ## 1.2.0 (2015-06-25) ### Note for Upgraders diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 8426628f..35586d1d 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -20,6 +20,7 @@ import os import logging import logging.config import atexit +import signal SUCCESS = {} NO_CONTENT = ("", 204) @@ -440,16 +441,27 @@ class Server(): # prepare our shutdown function def on_shutdown(): - self._logger.info("Goodbye!") + # will be called on clean system exit and shutdown the watchdog observer and call the on_shutdown methods + # on all registered ShutdownPlugins + self._logger.info("Shutting down...") observer.stop() observer.join() octoprint.plugin.call_plugin(octoprint.plugin.ShutdownPlugin, "on_shutdown") + self._logger.info("Goodbye!") atexit.register(on_shutdown) + def sigterm_handler(*args, **kwargs): + # will stop tornado on SIGTERM, making the program exit cleanly + def shutdown_tornado(): + ioloop.stop() + ioloop.add_callback_from_signal(shutdown_tornado) + signal.signal(signal.SIGTERM, sigterm_handler) + try: + # this is the main loop - as long as tornado is running, OctoPrint is running ioloop.start() - except KeyboardInterrupt: + except (KeyboardInterrupt, SystemExit): pass except: self._logger.fatal("Now that is embarrassing... Something really really went wrong here. Please report this including the stacktrace below in OctoPrint's bugtracker. Thanks!")