From c86b47b9e20226d7ee7290f5305fd6a0b8f7e698 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 17 Nov 2024 11:37:24 +0100 Subject: [PATCH] feat: improve backgroundJob tracking --- src/composables/backgroundJob.ts | 16 ++++++++++++---- src/types/backgroundtasks.ts | 2 ++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/composables/backgroundJob.ts b/src/composables/backgroundJob.ts index eaf67048..ef3f405a 100644 --- a/src/composables/backgroundJob.ts +++ b/src/composables/backgroundJob.ts @@ -1,7 +1,7 @@ import { BackgroundTaskStatus } from '@/types'; import { AxiosInstance } from 'axios'; -const jobs = ref>({}); +const jobs = ref>({}); export function useBackgroundJob() { function startBgJob(api: AxiosInstance, showAlert: any, jobId: string, jobType: string) { @@ -17,7 +17,7 @@ export function useBackgroundJob() { const evaluating = ref(false); const result = ref(null); - jobs.value[jobId] = { jobType, status: 'starting' }; + jobs.value[jobId] = { jobType }; const interval = window.setInterval(async () => { try { @@ -25,8 +25,7 @@ export function useBackgroundJob() { if (!result.value.running) { clearJobFromList(); } - jobs.value[jobId] = { ...jobs.value[jobId], status: result.value.status }; - console.log('got result', result.value); + jobs.value[jobId] = { ...jobs.value[jobId], taskStatus: result.value }; } catch (error) { console.error(error); showAlert('Failed to get background job status', 'error'); @@ -48,9 +47,18 @@ export function useBackgroundJob() { } const runningJobs = computed(() => jobs.value); + function clearJobs() { + // Clear all jobs that are not running + for (const [jobId, job] of Object.entries(jobs.value)) { + if (job.taskStatus?.status !== 'running') { + delete jobs.value[jobId]; + } + } + } return { runningJobs, startBgJob, + clearJobs, }; } diff --git a/src/types/backgroundtasks.ts b/src/types/backgroundtasks.ts index d9c1f425..b45b4f82 100644 --- a/src/types/backgroundtasks.ts +++ b/src/types/backgroundtasks.ts @@ -8,6 +8,8 @@ export interface BackgroundTaskStatus { status: string; running: boolean; progress?: number; + // TODO: type this properly + progress_tasks?: Record; } export interface BackgroundTaskResult {