diff --git a/backend/endpoints/tasks.py b/backend/endpoints/tasks.py index 19f1e807e..2c2f522d7 100644 --- a/backend/endpoints/tasks.py +++ b/backend/endpoints/tasks.py @@ -1,6 +1,7 @@ from datetime import datetime, timezone from fastapi import HTTPException, Request +from rq import Worker from rq.job import Job from config import ( @@ -12,7 +13,12 @@ from decorators.auth import protected_route from endpoints.responses import TaskExecutionResponse, TaskStatusResponse from endpoints.responses.tasks import GroupedTasksDict, TaskInfo from handler.auth.constants import Scope -from handler.redis_handler import default_queue, high_prio_queue, low_prio_queue +from handler.redis_handler import ( + default_queue, + high_prio_queue, + low_prio_queue, + redis_client, +) from tasks.manual.cleanup_orphaned_resources import cleanup_orphaned_resources_task from tasks.scheduled.convert_images_to_webp import convert_images_to_webp_task from tasks.scheduled.scan_library import scan_library_task @@ -87,16 +93,48 @@ async def list_tasks(request: Request) -> GroupedTasksDict: return grouped_tasks -@protected_route(router.get, "/running", [Scope.TASKS_RUN]) -async def get_running_tasks(request: Request) -> list[TaskStatusResponse]: - """Get all currently running tasks. +@protected_route(router.get, "/active", [Scope.TASKS_RUN]) +async def get_active_tasks(request: Request) -> list[TaskStatusResponse]: + """Get all currently active tasks. Args: request (Request): FastAPI Request object Returns: - list[TaskStatusResponse]: List of currently running tasks + list[TaskStatusResponse]: List of currently active tasks """ - running_tasks = [] + active_tasks = [] + + workers = Worker.all(connection=redis_client) + for worker in workers: + current_job = worker.get_current_job() + if current_job: + task_name = ( + current_job.meta.get("task_name") or current_job.func_name + if current_job.meta + else current_job.func_name + ) + + # Convert datetime objects to ISO format strings + queued_at = ( + current_job.created_at.isoformat() if current_job.created_at else None + ) + started_at = ( + current_job.started_at.isoformat() if current_job.started_at else None + ) + ended_at = ( + current_job.ended_at.isoformat() if current_job.ended_at else None + ) + + active_tasks.append( + TaskStatusResponse( + task_name=str(task_name), + task_id=current_job.get_id(), + status=current_job.get_status(), + queued_at=queued_at or "", + started_at=started_at, + ended_at=ended_at, + ) + ) # Get all jobs from the queue low_prio_jobs = low_prio_queue.get_jobs() @@ -104,7 +142,6 @@ async def get_running_tasks(request: Request) -> list[TaskStatusResponse]: high_prio_jobs = high_prio_queue.get_jobs() for job in low_prio_jobs + default_prio_jobs + high_prio_jobs: - # Only include jobs that are currently running or queued status = job.get_status() # Convert datetime objects to ISO format strings @@ -112,12 +149,11 @@ async def get_running_tasks(request: Request) -> list[TaskStatusResponse]: started_at = job.started_at.isoformat() if job.started_at else None ended_at = job.ended_at.isoformat() if job.ended_at else None - # Get task name from job metadata or function name task_name = ( job.meta.get("task_name") or job.func_name if job.meta else job.func_name ) - running_tasks.append( + active_tasks.append( TaskStatusResponse( task_name=str(task_name), task_id=job.get_id(), @@ -128,7 +164,7 @@ async def get_running_tasks(request: Request) -> list[TaskStatusResponse]: ) ) - return running_tasks + return active_tasks @protected_route(router.get, "/{task_id}", [Scope.TASKS_RUN]) diff --git a/frontend/src/components/Settings/Administration/TaskOption.vue b/frontend/src/components/Settings/Administration/TaskOption.vue index 8e18fda51..2a558e26b 100644 --- a/frontend/src/components/Settings/Administration/TaskOption.vue +++ b/frontend/src/components/Settings/Administration/TaskOption.vue @@ -2,7 +2,7 @@ import type { Emitter } from "mitt"; import { inject, computed } from "vue"; import taskApi from "@/services/api/task"; -import storeRunningTasks from "@/stores/runningTasks"; +import storeTasks from "@/stores/tasks"; import type { Events } from "@/types/emitter"; const props = withDefaults( @@ -26,19 +26,11 @@ const props = withDefaults( }, ); const emitter = inject>("emitter"); -const runningTasksStore = storeRunningTasks(); - -// Computed properties -const isTaskRunning = computed(() => - props.name ? runningTasksStore.isTaskRunning(props.name) : false, -); +const tasksStore = storeTasks(); function run() { if (!props.name) return; - // Add task to running tasks - runningTasksStore.addTask(props.name); - taskApi .runTask(props.name) .then(() => { @@ -56,10 +48,7 @@ function run() { color: "red", }); }) - .finally(() => { - // Remove task from running tasks - runningTasksStore.removeTask(props.name); - }); + .finally(() => {}); }