diff --git a/changedetectionio/changedetection.py b/changedetectionio/changedetection.py index 2adf5ffc..ae6da9e2 100755 --- a/changedetectionio/changedetection.py +++ b/changedetectionio/changedetection.py @@ -4,6 +4,7 @@ import getopt import os +import signal import sys import eventlet @@ -11,7 +12,25 @@ import eventlet.wsgi from . import store, changedetection_app, content_fetcher from . import __version__ +# Only global so we can access it in the signal handler +datastore = None +app = None + +def sigterm_handler(_signo, _stack_frame): + global app + global datastore + + print('Shutdown: got SIGINT, writing DB') + datastore.sync_to_json() + print('sync_to_json() done') + eventlet.is_accepting = False + app.config.exit.set() + + + def main(): + global datastore + global app ssl_mode = False host = '' port = os.environ.get('PORT') or 5000 @@ -74,6 +93,7 @@ def main(): datastore = store.ChangeDetectionStore(datastore_path=app_config['datastore_path'], version_tag=__version__) app = changedetection_app(app_config, datastore) + signal.signal(signal.SIGINT, sigterm_handler) # Go into cleanup mode if do_cleanup: