Skip to content

Commit

Permalink
Merge pull request #2218 from RTXteam/issue-2217
Browse files Browse the repository at this point in the history
Issue 2217
  • Loading branch information
saramsey authored Nov 30, 2023
2 parents 42035d0 + 558954b commit a59d6de
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
10 changes: 8 additions & 2 deletions code/ARAX/ARAXQuery/ARAX_background_tasker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -159,7 +165,7 @@ def run_tasks(self):


def main():
background_tasker = ARAXBackgroundTasker()
background_tasker = ARAXBackgroundTasker(os.getpid())
background_tasker.run_tasks()


Expand Down
16 changes: 10 additions & 6 deletions code/UI/OpenAPI/python-flask-server/KG2/openapi_server/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():

Expand All @@ -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
Expand All @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions code/UI/OpenAPI/python-flask-server/openapi_server/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():

Expand All @@ -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
Expand All @@ -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())
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a59d6de

Please sign in to comment.