diff --git a/code/ARAX/ARAXQuery/ARAX_background_tasker.py b/code/ARAX/ARAXQuery/ARAX_background_tasker.py index d43c2ac34..4b3d7cbbb 100644 --- a/code/ARAX/ARAXQuery/ARAX_background_tasker.py +++ b/code/ARAX/ARAXQuery/ARAX_background_tasker.py @@ -22,8 +22,10 @@ def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) class ARAXBackgroundTasker: - def __init__(self, run_kp_info_cacher=True): + def __init__(self, parent_pid: int, + run_kp_info_cacher: bool = True): self.run_kp_info_cacher = run_kp_info_cacher + self.parent_pid = parent_pid timestamp = str(datetime.datetime.now().isoformat()) eprint(f"{timestamp}: INFO: ARAXBackgroundTasker created") @@ -116,6 +118,10 @@ def run_tasks(self): # Loop forever doing various things my_pid = os.getpid() while True: + if not psutil.pid_exists(self.parent_pid): + eprint("INFO: ARAXBackgroundTasker: parent process " + f"{self.parent_pid} has gone away; exiting") + sys.exit(0) # Run the KP Info Cacher less frequently if self.run_kp_info_cacher: @@ -159,7 +165,7 @@ def run_tasks(self): def main(): - background_tasker = ARAXBackgroundTasker() + background_tasker = ARAXBackgroundTasker(os.getpid()) background_tasker.run_tasks() diff --git a/code/UI/OpenAPI/python-flask-server/KG2/openapi_server/__main__.py b/code/UI/OpenAPI/python-flask-server/KG2/openapi_server/__main__.py index a020b7be3..362696622 100644 --- a/code/UI/OpenAPI/python-flask-server/KG2/openapi_server/__main__.py +++ b/code/UI/OpenAPI/python-flask-server/KG2/openapi_server/__main__.py @@ -19,9 +19,13 @@ def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) FLASK_DEFAULT_TCP_PORT = 5008 +global child_pid child_pid = None +global parent_pid parent_pid = None +CONFIG_FILE = 'openapi_server/flask_config.json' + def main(): @@ -42,13 +46,15 @@ def main(): # Read any load configuration details for this instance try: - with open('openapi_server/flask_config.json') as infile: + with open(CONFIG_FILE, 'r') as infile: local_config = json.load(infile) except Exception: - eprint(f"Error loading config file: {infile}") + eprint(f"Error loading config file: {CONFIG_FILE}") local_config = {"port": FLASK_DEFAULT_TCP_PORT} tcp_port = local_config['port'] + parent_pid = os.getpid() + pid = os.fork() if pid == 0: # I am the child process from ARAX_background_tasker import ARAXBackgroundTasker @@ -58,7 +64,8 @@ def main(): f"::run_tasks [port={tcp_port}]") eprint("Starting background tasker in a child process") try: - ARAXBackgroundTasker(run_kp_info_cacher=False).run_tasks() + ARAXBackgroundTasker(parent_pid, + run_kp_info_cacher=False).run_tasks() except Exception as e: eprint("Error in ARAXBackgroundTasker.run_tasks()") eprint(traceback.format_exc()) @@ -113,10 +120,7 @@ def receive_sigpipe(signal_number, frame): # Start the service eprint(f"Background tasker is running in child process {pid}") - global child_pid child_pid = pid - global parent_pid - parent_pid = os.getpid() signal.signal(signal.SIGCHLD, receive_sigchld) signal.signal(signal.SIGPIPE, receive_sigpipe) signal.signal(signal.SIGTERM, receive_sigterm) diff --git a/code/UI/OpenAPI/python-flask-server/openapi_server/__main__.py b/code/UI/OpenAPI/python-flask-server/openapi_server/__main__.py index 66e2f7db5..a06be9b02 100644 --- a/code/UI/OpenAPI/python-flask-server/openapi_server/__main__.py +++ b/code/UI/OpenAPI/python-flask-server/openapi_server/__main__.py @@ -19,9 +19,13 @@ def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) FLASK_DEFAULT_TCP_PORT = 5000 +global child_pid child_pid = None +global parent_pid parent_pid = None +CONFIG_FILE = 'openapi_server/flask_config.json' + def main(): @@ -42,13 +46,15 @@ def main(): # Read any load configuration details for this instance try: - with open('openapi_server/flask_config.json') as infile: + with open(CONFIG_FILE, 'r') as infile: local_config = json.load(infile) except Exception: - eprint(f"Error loading config file: {infile}") + eprint(f"Error loading config file: {CONFIG_FILE}") local_config = {"port": FLASK_DEFAULT_TCP_PORT} tcp_port = local_config['port'] + parent_pid = os.getpid() + pid = os.fork() if pid == 0: # I am the child process from ARAX_background_tasker import ARAXBackgroundTasker @@ -58,7 +64,7 @@ def main(): f"::run_tasks [port={tcp_port}]") eprint("Starting background tasker in a child process") try: - ARAXBackgroundTasker().run_tasks() + ARAXBackgroundTasker(parent_pid).run_tasks() except Exception as e: eprint("Error in ARAXBackgroundTasker.run_tasks()") eprint(traceback.format_exc()) @@ -113,10 +119,7 @@ def receive_sigpipe(signal_number, frame): # Start the service eprint(f"Background tasker is running in child process {pid}") - global child_pid child_pid = pid - global parent_pid - parent_pid = os.getpid() signal.signal(signal.SIGCHLD, receive_sigchld) signal.signal(signal.SIGPIPE, receive_sigpipe) signal.signal(signal.SIGTERM, receive_sigterm)