mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 01:58:41 +01:00
get from running workers
This commit is contained in:
@@ -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])
|
||||
|
||||
@@ -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<Events>>("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(() => {});
|
||||
}
|
||||
</script>
|
||||
<template>
|
||||
@@ -84,8 +73,8 @@ function run() {
|
||||
variant="outlined"
|
||||
size="small"
|
||||
class="text-primary"
|
||||
:disabled="isTaskRunning"
|
||||
:loading="isTaskRunning"
|
||||
:disabled="false"
|
||||
:loading="false"
|
||||
@click="run"
|
||||
>
|
||||
<v-icon>mdi-play</v-icon>
|
||||
|
||||
@@ -8,7 +8,7 @@ import storeTasks from "@/stores/tasks";
|
||||
import { convertCronExperssion } from "@/utils";
|
||||
|
||||
const tasksStore = storeTasks();
|
||||
const { watcherTasks, scheduledTasks, manualTasks, runningTasks } =
|
||||
const { watcherTasks, scheduledTasks, manualTasks, activeTasks } =
|
||||
storeToRefs(tasksStore);
|
||||
const isLoadingRunningTasks = ref(false);
|
||||
|
||||
@@ -43,10 +43,10 @@ const getManualTaskIcon = (taskName: string) => {
|
||||
};
|
||||
|
||||
// Fetch running tasks
|
||||
const fetchRunningTasks = async () => {
|
||||
const fetchActiveTasks = async () => {
|
||||
isLoadingRunningTasks.value = true;
|
||||
try {
|
||||
await tasksStore.fetchRunningTasks();
|
||||
await tasksStore.fetchActiveTasks();
|
||||
} catch (error) {
|
||||
console.error("Error fetching running tasks:", error);
|
||||
} finally {
|
||||
@@ -58,8 +58,8 @@ const fetchRunningTasks = async () => {
|
||||
let refreshInterval: NodeJS.Timeout | null = null;
|
||||
|
||||
onMounted(() => {
|
||||
fetchRunningTasks();
|
||||
refreshInterval = setInterval(fetchRunningTasks, 5000);
|
||||
fetchActiveTasks();
|
||||
refreshInterval = setInterval(fetchActiveTasks, 5000);
|
||||
});
|
||||
|
||||
// Cleanup interval on unmount
|
||||
@@ -85,7 +85,7 @@ onUnmounted(() => {
|
||||
<v-divider class="border-opacity-25 ma-1" />
|
||||
<v-row no-gutters class="align-center py-1">
|
||||
<v-col
|
||||
v-if="runningTasks.length === 0 && !isLoadingRunningTasks"
|
||||
v-if="activeTasks.length === 0 && !isLoadingRunningTasks"
|
||||
cols="12"
|
||||
>
|
||||
<v-card elevation="0" class="bg-background ma-3">
|
||||
@@ -111,7 +111,7 @@ onUnmounted(() => {
|
||||
</v-col>
|
||||
<v-col
|
||||
v-else
|
||||
v-for="task in runningTasks"
|
||||
v-for="task in activeTasks"
|
||||
:key="task.task_id"
|
||||
cols="12"
|
||||
md="6"
|
||||
|
||||
@@ -27,8 +27,8 @@ async function runTask(
|
||||
return api.post(`/tasks/run/${taskName}`);
|
||||
}
|
||||
|
||||
async function getRunningTasks(): Promise<{ data: TaskStatusResponse[] }> {
|
||||
return api.get("/tasks/running");
|
||||
async function getActiveTasks(): Promise<{ data: TaskStatusResponse[] }> {
|
||||
return api.get("/tasks/active");
|
||||
}
|
||||
|
||||
export default {
|
||||
@@ -36,5 +36,5 @@ export default {
|
||||
getTaskById,
|
||||
runAllTasks,
|
||||
runTask,
|
||||
getRunningTasks,
|
||||
getActiveTasks,
|
||||
};
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
import { defineStore } from "pinia";
|
||||
|
||||
export default defineStore("runningTasks", {
|
||||
state: () => ({
|
||||
runningTasks: [] as string[],
|
||||
}),
|
||||
|
||||
actions: {
|
||||
reset() {
|
||||
this.runningTasks = [];
|
||||
},
|
||||
addTask(taskEndpoint: string) {
|
||||
if (!this.runningTasks.includes(taskEndpoint)) {
|
||||
this.runningTasks.push(taskEndpoint);
|
||||
}
|
||||
},
|
||||
removeTask(taskEndpoint: string) {
|
||||
const index = this.runningTasks.indexOf(taskEndpoint);
|
||||
if (index > -1) {
|
||||
this.runningTasks.splice(index, 1);
|
||||
}
|
||||
},
|
||||
isTaskRunning(taskEndpoint: string): boolean {
|
||||
return this.runningTasks.includes(taskEndpoint);
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -8,7 +8,7 @@ export default defineStore("tasks", {
|
||||
watcherTasks: [] as TaskInfo[],
|
||||
scheduledTasks: [] as TaskInfo[],
|
||||
manualTasks: [] as TaskInfo[],
|
||||
runningTasks: [] as TaskStatusResponse[],
|
||||
activeTasks: [] as TaskStatusResponse[],
|
||||
}),
|
||||
|
||||
actions: {
|
||||
@@ -37,14 +37,14 @@ export default defineStore("tasks", {
|
||||
};
|
||||
}
|
||||
},
|
||||
async fetchRunningTasks(): Promise<TaskStatusResponse[]> {
|
||||
async fetchActiveTasks(): Promise<TaskStatusResponse[]> {
|
||||
try {
|
||||
const response = await tasksApi.getRunningTasks();
|
||||
this.runningTasks = response.data;
|
||||
return this.runningTasks;
|
||||
const response = await tasksApi.getActiveTasks();
|
||||
this.activeTasks = response.data;
|
||||
return this.activeTasks;
|
||||
} catch (error) {
|
||||
console.error("Error fetching running tasks: ", error);
|
||||
this.runningTasks = [];
|
||||
console.error("Error fetching active tasks: ", error);
|
||||
this.activeTasks = [];
|
||||
return [];
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user