mirror of
				https://github.com/dgtlmoon/changedetection.io.git
				synced 2025-11-04 08:34:57 +00:00 
			
		
		
		
	Compare commits
	
		
			9 Commits
		
	
	
		
			0.39.20.4
			...
			sig-handle
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					627fd368fb | ||
| 
						 | 
					f2f8469891 | ||
| 
						 | 
					27117b324d | ||
| 
						 | 
					3cc7b8e489 | ||
| 
						 | 
					307ce59b38 | ||
| 
						 | 
					d1bd5b442b | ||
| 
						 | 
					a3f492bf17 | ||
| 
						 | 
					58b0166330 | ||
| 
						 | 
					fd080e9e4b | 
@@ -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")
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user