diff --git a/monitor/main.py b/monitor/main.py index 674cd82..a10ac4c 100644 --- a/monitor/main.py +++ b/monitor/main.py @@ -12,6 +12,9 @@ from dotenv import load_dotenv # Load environment variables from .env file load_dotenv() +# {task_type}_{task_id}: status_dict +TASK_STATUS_CACHE = {} + app = Flask(__name__) # Load configuration from environment variables @@ -157,6 +160,13 @@ def get_task_status_brief(task_type, task_id): """ Get brief status info for a task, without detailed step data, for fast homepage loading. """ + # Generate cache key based on task type and ID + cache_key = f"{task_type}_{task_id}" + + # Check if the status is already cached + if cache_key in TASK_STATUS_CACHE: + return TASK_STATUS_CACHE[cache_key] + result_dir = os.path.join(RESULTS_BASE_PATH, task_type, task_id) if not os.path.exists(result_dir): @@ -259,13 +269,19 @@ def get_task_status_brief(task_type, task_id): except: result_content = "Result file not found" - return { + status_dict = { "status": status, "progress": step_count, "max_steps": MAX_STEPS, "last_update": last_update, "result": result_content } + + # Cache the status if it is done or error + if status.startswith("Done") or status == "Error": + TASK_STATUS_CACHE[cache_key] = status_dict + + return status_dict def get_all_tasks_status(): task_list = load_task_list()