From 4885316714bd199a2a18c818860aa692cf17536e Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Wed, 1 Oct 2025 10:59:49 -0400 Subject: [PATCH] also fetch completed tasks --- backend/endpoints/tasks.py | 85 +++++++++++++++++-- .../Settings/Administration/Tasks.vue | 20 ++--- frontend/src/services/api/task.ts | 6 +- frontend/src/stores/tasks.ts | 14 +-- 4 files changed, 96 insertions(+), 29 deletions(-) diff --git a/backend/endpoints/tasks.py b/backend/endpoints/tasks.py index 2c2f522d7..608434d96 100644 --- a/backend/endpoints/tasks.py +++ b/backend/endpoints/tasks.py @@ -3,6 +3,7 @@ from datetime import datetime, timezone from fastapi import HTTPException, Request from rq import Worker from rq.job import Job +from rq.registry import FailedJobRegistry, FinishedJobRegistry from config import ( ENABLE_RESCAN_ON_FILESYSTEM_CHANGE, @@ -93,17 +94,18 @@ async def list_tasks(request: Request) -> GroupedTasksDict: return grouped_tasks -@protected_route(router.get, "/active", [Scope.TASKS_RUN]) -async def get_active_tasks(request: Request) -> list[TaskStatusResponse]: - """Get all currently active tasks. +@protected_route(router.get, "/status", [Scope.TASKS_RUN]) +async def get_task_status(request: Request) -> list[TaskStatusResponse]: + """Get all task statuses including active, queued, completed, and failed tasks. Args: request (Request): FastAPI Request object Returns: - list[TaskStatusResponse]: List of currently active tasks + list[TaskStatusResponse]: List of all tasks with their current status """ - active_tasks = [] + all_tasks = [] + # Get currently running jobs from workers workers = Worker.all(connection=redis_client) for worker in workers: current_job = worker.get_current_job() @@ -125,7 +127,7 @@ async def get_active_tasks(request: Request) -> list[TaskStatusResponse]: current_job.ended_at.isoformat() if current_job.ended_at else None ) - active_tasks.append( + all_tasks.append( TaskStatusResponse( task_name=str(task_name), task_id=current_job.get_id(), @@ -136,7 +138,7 @@ async def get_active_tasks(request: Request) -> list[TaskStatusResponse]: ) ) - # Get all jobs from the queue + # Get all jobs from the queues (including completed ones) low_prio_jobs = low_prio_queue.get_jobs() default_prio_jobs = default_queue.get_jobs() high_prio_jobs = high_prio_queue.get_jobs() @@ -153,7 +155,7 @@ async def get_active_tasks(request: Request) -> list[TaskStatusResponse]: job.meta.get("task_name") or job.func_name if job.meta else job.func_name ) - active_tasks.append( + all_tasks.append( TaskStatusResponse( task_name=str(task_name), task_id=job.get_id(), @@ -164,7 +166,72 @@ async def get_active_tasks(request: Request) -> list[TaskStatusResponse]: ) ) - return active_tasks + # Get finished jobs from all queues + finished_registries = [ + FinishedJobRegistry(queue=low_prio_queue), + FinishedJobRegistry(queue=default_queue), + FinishedJobRegistry(queue=high_prio_queue), + ] + + failed_registries = [ + FailedJobRegistry(queue=low_prio_queue), + FailedJobRegistry(queue=default_queue), + FailedJobRegistry(queue=high_prio_queue), + ] + + # Process finished jobs + for registry in finished_registries: + for job_id in registry.get_job_ids(): + job = Job.fetch(job_id, connection=redis_client) + task_name = ( + job.meta.get("task_name") or job.func_name + if job.meta + else job.func_name + ) + + # Convert datetime objects to ISO format strings + queued_at = job.created_at.isoformat() if job.created_at else None + started_at = job.started_at.isoformat() if job.started_at else None + ended_at = job.ended_at.isoformat() if job.ended_at else None + + all_tasks.append( + TaskStatusResponse( + task_name=str(task_name), + task_id=job_id, + status=job.get_status(), + queued_at=queued_at or "", + started_at=started_at, + ended_at=ended_at, + ) + ) + + # Process failed jobs + for registry in failed_registries: + for job_id in registry.get_job_ids(): + job = Job.fetch(job_id, connection=redis_client) + task_name = ( + job.meta.get("task_name") or job.func_name + if job.meta + else job.func_name + ) + + # Convert datetime objects to ISO format strings + queued_at = job.created_at.isoformat() if job.created_at else None + started_at = job.started_at.isoformat() if job.started_at else None + ended_at = job.ended_at.isoformat() if job.ended_at else None + + all_tasks.append( + TaskStatusResponse( + task_name=str(task_name), + task_id=job_id, + status=job.get_status(), + queued_at=queued_at or "", + started_at=started_at, + ended_at=ended_at, + ) + ) + + return all_tasks @protected_route(router.get, "/{task_id}", [Scope.TASKS_RUN]) diff --git a/frontend/src/components/Settings/Administration/Tasks.vue b/frontend/src/components/Settings/Administration/Tasks.vue index a0cd5ba12..1c46ae372 100644 --- a/frontend/src/components/Settings/Administration/Tasks.vue +++ b/frontend/src/components/Settings/Administration/Tasks.vue @@ -8,7 +8,7 @@ import storeTasks from "@/stores/tasks"; import { convertCronExperssion } from "@/utils"; const tasksStore = storeTasks(); -const { watcherTasks, scheduledTasks, manualTasks, activeTasks } = +const { watcherTasks, scheduledTasks, manualTasks, taskStatuses } = storeToRefs(tasksStore); const isLoadingRunningTasks = ref(false); @@ -42,24 +42,24 @@ const getManualTaskIcon = (taskName: string) => { return iconMap[taskName] || "mdi-play"; }; -// Fetch running tasks -const fetchActiveTasks = async () => { +// Fetch task status +const fetchTaskStatus = async () => { isLoadingRunningTasks.value = true; try { - await tasksStore.fetchActiveTasks(); + await tasksStore.fetchTaskStatus(); } catch (error) { - console.error("Error fetching running tasks:", error); + console.error("Error fetching task status:", error); } finally { isLoadingRunningTasks.value = false; } }; -// Auto-refresh running tasks every 5 seconds +// Auto-refresh task status every 5 seconds let refreshInterval: NodeJS.Timeout | null = null; onMounted(() => { - fetchActiveTasks(); - refreshInterval = setInterval(fetchActiveTasks, 5000); + fetchTaskStatus(); + refreshInterval = setInterval(fetchTaskStatus, 5000); }); // Cleanup interval on unmount @@ -85,7 +85,7 @@ onUnmounted(() => { @@ -111,7 +111,7 @@ onUnmounted(() => { { - return api.get("/tasks/active"); +async function getTaskStatus(): Promise<{ data: TaskStatusResponse[] }> { + return api.get("/tasks/status"); } export default { @@ -36,5 +36,5 @@ export default { getTaskById, runAllTasks, runTask, - getActiveTasks, + getTaskStatus, }; diff --git a/frontend/src/stores/tasks.ts b/frontend/src/stores/tasks.ts index 754a3f406..c7c6caca7 100644 --- a/frontend/src/stores/tasks.ts +++ b/frontend/src/stores/tasks.ts @@ -8,7 +8,7 @@ export default defineStore("tasks", { watcherTasks: [] as TaskInfo[], scheduledTasks: [] as TaskInfo[], manualTasks: [] as TaskInfo[], - activeTasks: [] as TaskStatusResponse[], + taskStatuses: [] as TaskStatusResponse[], }), actions: { @@ -37,14 +37,14 @@ export default defineStore("tasks", { }; } }, - async fetchActiveTasks(): Promise { + async fetchTaskStatus(): Promise { try { - const response = await tasksApi.getActiveTasks(); - this.activeTasks = response.data; - return this.activeTasks; + const response = await tasksApi.getTaskStatus(); + this.taskStatuses = response.data; + return this.taskStatuses; } catch (error) { - console.error("Error fetching active tasks: ", error); - this.activeTasks = []; + console.error("Error fetching task status: ", error); + this.taskStatuses = []; return []; } },