reload functionality progress using SIGUSR1

pull/183/head
Anthony Leung 2024-06-04 15:40:39 +00:00
rodzic 9ef604d71d
commit 2d5443933c
2 zmienionych plików z 40 dodań i 24 usunięć

Wyświetl plik

@ -111,23 +111,26 @@ def load_module(file, reload=False):
def execute(args): def execute(args):
print('execute') print('cli.execute')
try: try:
_execute(args) _execute(args)
except SystemExit: except SystemExit as se:
print('caught exit') if 'reload' in str(se) and '--reload' in args:
if '--reload' in args:
logging.info('RELOADING...') logging.info('RELOADING...')
import sys import sys
import os import os
print(args) #print(args)
print(sys.argv) #print(sys.argv)
#os.execv(sys.executable, ['-m socketify'] + args[1:]) #os.execv(sys.executable, ['-m socketify'] + args[1:])
print(sys.executable, [sys.executable, '-m', 'socketify'] + args[1:]) #print(sys.executable, [sys.executable, '-m', 'socketify'] + args[1:])
# The app.run has already caught SIGUSR1 which closes the loop then raises SystemExit.
# Now we respawn the process with the original arguments
os.execv(sys.executable, [sys.executable, '-m', 'socketify'] + args[1:]) os.execv(sys.executable, [sys.executable, '-m', 'socketify'] + args[1:])
os.kill(os.getpid(), signal.SIGINT) #or os.popen("wmic process where processid='{}' call terminate".format(os.getpid())) #os.kill(os.getpid(), signal.SIGINT) <-- this done in the file probe
#or os.popen("wmic process where processid='{}' call terminate".format(os.getpid()))
def _execute(args): def _execute(args):
arguments_length = len(args) arguments_length = len(args)
@ -332,26 +335,38 @@ def _execute(args):
new_files = get_files() new_files = get_files()
if prev_files is not None and new_files != prev_files: if prev_files is not None and new_files != prev_files:
# Call exit, the wrapper will restart the process
print('Reload') print('Reload')
logging.info("Reloading files...") logging.info("Reloading files...")
print('running sigint')
import os, signal
os.kill(os.getpid(),signal.SIGUSR1) # call sigusr1 back on main thread which is caught by App.run()
# os.kill(os.getpid(),signal.SIGINT) # call sigint back on main thread which is caught by App.run()
"""print('running sysexit')
import sys import sys
sys.exit(0) sys.exit(0)
print('ran sysexit')
"""
return new_files, thread return new_files, thread
import asyncio import asyncio
print('rnt 0') print('rnt 0')
def poll_check():
thread = None thread = None
thread = threading.Thread(target=run_method, kwargs={'from_main_thread': False}, daemon=True)
thread.start()
files = None
poll_frequency = 0.5 poll_frequency = 0.5
files = None
while True: while True:
import time import time
time.sleep(poll_frequency) time.sleep(poll_frequency)
files, thread = do_check(files, thread) files, thread = do_check(files, thread)
#await asyncio.wait_for(thread, poll_frequency) #await asyncio.wait_for(thread, poll_frequency)
thread = None
# thread = threading.Thread(target=run_method, kwargs={from_main_thread': False}, daemon=True)
thread = threading.Thread(target=poll_check, kwargs={}, daemon=True)
thread.start()
run_method()
""" """
async def launch_with_file_probe(run_method, user_module_function, loop, poll_frequency=0.5): async def launch_with_file_probe(run_method, user_module_function, loop, poll_frequency=0.5):
import asyncio import asyncio
@ -519,17 +534,11 @@ def _execute(args):
fork_app.listen(AppListenOptions(port=port, host=host), listen_log) fork_app.listen(AppListenOptions(port=port, host=host), listen_log)
if auto_reload: if auto_reload:
# there's watchfiles but socketify currently has no external dependencies... # there's watchfiles module but socketify currently has no external dependencies so
# we'll roll our own for now...
# from watchfiles import arun_process # from watchfiles import arun_process
# w/o external dependencies
#import asyncio
#fork_app.loop.run_async(launch_with_file_probe(fork_app.run, module, fork_app.loop))
logging.info(' LAUNCHING WITH RELOAD ') logging.info(' LAUNCHING WITH RELOAD ')
launch_with_file_probe(fork_app.run, module, fork_app.loop) launch_with_file_probe(fork_app.run, module, fork_app.loop)
#asyncio.run(launch_with_file_probe(fork_app.run, module))
#thread = threading.Thread(target=launch_with_file_probe, args=[fork_app.run, module])
#thread.start()
#thread.join()
else: # run normally else: # run normally
fork_app.run() fork_app.run()

Wyświetl plik

@ -3378,6 +3378,13 @@ class App:
exit(0) exit(0)
signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGINT, signal_handler)
def reload_signal_handler(sig, frame):
self.close()
raise SystemExit('reload')
signal.signal(signal.SIGUSR1, reload_signal_handler) # used by --reload in cli.py to reload process
self.loop.run() self.loop.run()
if self.lifespan: if self.lifespan: