Compare commits

...

9 Commits

Author SHA1 Message Date
dgtlmoon
627fd368fb Return exit status 2022-08-02 09:06:52 +02:00
dgtlmoon
f2f8469891 WIP 2022-08-01 21:02:07 +02:00
dgtlmoon
27117b324d Add comment 2022-08-01 19:30:04 +02:00
dgtlmoon
3cc7b8e489 Nope ignore it :) 2022-08-01 19:25:45 +02:00
dgtlmoon
307ce59b38 Handle SIGSTOP for suspend mode also 2022-08-01 19:23:38 +02:00
dgtlmoon
d1bd5b442b whitespace cleanup 2022-08-01 19:18:13 +02:00
dgtlmoon
a3f492bf17 use multiprocessing to wrap flask and use SIGTERM to save DB 2022-08-01 19:13:57 +02:00
dgtlmoon
58b0166330 Merge branch 'master' into sig-handler 2022-08-01 16:48:10 +02:00
dgtlmoon
fd080e9e4b Handle SIGINT somewhat 2022-07-04 20:52:15 +02:00
3 changed files with 47 additions and 3 deletions

View File

@@ -6,6 +6,34 @@
# Read more https://github.com/dgtlmoon/changedetection.io/wiki
from changedetectionio import changedetection
import multiprocessing
import signal
import os
def sigterm_handler(_signo, _stack_frame):
import sys
print('Shutdown: Got SIGCHLD')
# https://stackoverflow.com/questions/40453496/python-multiprocessing-capturing-signals-to-restart-child-processes-or-shut-do
pid, status = os.waitpid(-1, os.WNOHANG | os.WUNTRACED | os.WCONTINUED)
print('Sub-process: pid %d status %d' % (pid, status))
if status != 0:
sys.exit(1)
raise SystemExit
if __name__ == '__main__':
changedetection.main()
signal.signal(signal.SIGCHLD, sigterm_handler)
# The only way I could find to get Flask to shutdown, is to wrap it and then rely on the subsystem issuing SIGTERM/SIGKILL
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")

View File

@@ -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)

View File

@@ -4,6 +4,7 @@
import getopt
import os
import signal
import sys
import eventlet
@@ -11,7 +12,22 @@ 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
app.config.exit.set()
datastore.sync_to_json()
print('Shutdown: Got SIGTERM, DB saved to disk')
raise SystemExit
def main():
global datastore
global app
ssl_mode = False
host = ''
port = os.environ.get('PORT') or 5000
@@ -72,8 +88,10 @@ def main():
"Or use the -C parameter to create the directory.".format(app_config['datastore_path']), file=sys.stderr)
sys.exit(2)
datastore = store.ChangeDetectionStore(datastore_path=app_config['datastore_path'], version_tag=__version__)
app = changedetection_app(app_config, datastore)
signal.signal(signal.SIGTERM, sigterm_handler)
# Go into cleanup mode
if do_cleanup:
@@ -111,4 +129,3 @@ def main():
else:
eventlet.wsgi.server(eventlet.listen((host, int(port))), app)