use multiprocessing to wrap flask and use SIGTERM to save DB

sig-handler
dgtlmoon 2 years ago
parent 58b0166330
commit a3f492bf17

@ -6,6 +6,19 @@
# Read more https://github.com/dgtlmoon/changedetection.io/wiki # Read more https://github.com/dgtlmoon/changedetection.io/wiki
from changedetectionio import changedetection from changedetectionio import changedetection
import multiprocessing
if __name__ == '__main__': 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")

@ -1263,7 +1263,6 @@ def notification_runner():
global notification_debug_log global notification_debug_log
from datetime import datetime from datetime import datetime
import json import json
while not app.config.exit.is_set(): while not app.config.exit.is_set():
try: try:
# At the moment only one thread runs (single runner) # At the moment only one thread runs (single runner)

@ -20,11 +20,10 @@ def sigterm_handler(_signo, _stack_frame):
global app global app
global datastore 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() 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__) datastore = store.ChangeDetectionStore(datastore_path=app_config['datastore_path'], version_tag=__version__)
app = changedetection_app(app_config, datastore) app = changedetection_app(app_config, datastore)
signal.signal(signal.SIGINT, sigterm_handler) signal.signal(signal.SIGTERM, sigterm_handler)
# Go into cleanup mode # Go into cleanup mode
if do_cleanup: if do_cleanup:

Loading…
Cancel
Save