From 3acef66a89107493a8e39e0ceb830bed2a110995 Mon Sep 17 00:00:00 2001 From: tyrin Date: Thu, 12 Sep 2024 14:30:59 +0300 Subject: [PATCH] monitor.py to restart ingester periodically hopefully avoiding memory leaks. --- management/commands/ingester.py | 4 ++-- monitor.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 monitor.py diff --git a/management/commands/ingester.py b/management/commands/ingester.py index b9cc56c..c88c07e 100644 --- a/management/commands/ingester.py +++ b/management/commands/ingester.py @@ -42,7 +42,7 @@ class Command(BaseCommand): #fetching the file list - directory = input_with_timeout(f"[{current_time()}] Please enter the path to the directory containing the csv files [{previous_choice}]: ", 30) + directory = input_with_timeout(f"[{current_time()}] Please enter the path to the directory containing the csv files, timeout 60 seconds [{previous_choice}]: ", 60) if not directory: directory = previous_choice @@ -170,6 +170,6 @@ class Command(BaseCommand): # Create a completion flag file with open("ingester_done.flag", "w") as f: f.write("done") - print("Ingester done") + print(f"[{current_time()}] Ingester done.") asyncio.run(ingest_files()) diff --git a/monitor.py b/monitor.py new file mode 100644 index 0000000..9f07e8f --- /dev/null +++ b/monitor.py @@ -0,0 +1,24 @@ +import subprocess +import time +import os + +def restart_script(): + + while True: + process = subprocess.Popen(["python", "manage.py", "ingester"]) + time.sleep(7200) + if os.path.exists("ingester_done.flag"): + # Remove the flag file + os.remove("ingester_done.flag") + print("Ingester done, exiting monitor script.") + break + + process.terminate() + process.wait() + print("Restarting ingester...") + + +if __name__ == "__main__": + restart_script() + +