From a3f492bf1739e074ba63b848b738877a088dbffd Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Mon, 1 Aug 2022 19:13:57 +0200 Subject: [PATCH] use multiprocessing to wrap flask and use SIGTERM to save DB --- changedetection.py | 15 ++++++++++++++- changedetectionio/__init__.py | 1 - changedetectionio/changedetection.py | 9 ++++----- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/changedetection.py b/changedetection.py index 9e76cc8c..10afd764 100755 --- a/changedetection.py +++ b/changedetection.py @@ -6,6 +6,19 @@ # Read more https://github.com/dgtlmoon/changedetection.io/wiki from changedetectionio import changedetection +import multiprocessing if __name__ == '__main__': - changedetection.main() + + parse_process = multiprocessing.Process(target=changedetection.main) + parse_process.daemon = True + parse_process.start() + import time + + try: + while True: + time.sleep(1) + + except KeyboardInterrupt: + #parse_process.terminate() not needed, because this process will issue it to the sub-process anyway + print ("Exited - CTRL+C") diff --git a/changedetectionio/__init__.py b/changedetectionio/__init__.py index acb301b5..72bb1dd5 100644 --- a/changedetectionio/__init__.py +++ b/changedetectionio/__init__.py @@ -1263,7 +1263,6 @@ def notification_runner(): global notification_debug_log from datetime import datetime import json - while not app.config.exit.is_set(): try: # At the moment only one thread runs (single runner) diff --git a/changedetectionio/changedetection.py b/changedetectionio/changedetection.py index ae6da9e2..6056c1e4 100755 --- a/changedetectionio/changedetection.py +++ b/changedetectionio/changedetection.py @@ -20,11 +20,10 @@ 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() + datastore.sync_to_json() + print('Shutdown: Got SIGTERM, DB saved to disk') + raise SystemExit @@ -93,7 +92,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) + signal.signal(signal.SIGTERM, sigterm_handler) # Go into cleanup mode if do_cleanup: