feat: improve backgroundJob tracking

This commit is contained in:
Matthias 2024-11-17 11:37:24 +01:00
parent 07a97501fb
commit c86b47b9e2
2 changed files with 14 additions and 4 deletions

View File

@ -1,7 +1,7 @@
import { BackgroundTaskStatus } from '@/types'; import { BackgroundTaskStatus } from '@/types';
import { AxiosInstance } from 'axios'; import { AxiosInstance } from 'axios';
const jobs = ref<Record<string, { jobType: string; status: string }>>({}); const jobs = ref<Record<string, { jobType: string; taskStatus?: BackgroundTaskStatus }>>({});
export function useBackgroundJob() { export function useBackgroundJob() {
function startBgJob(api: AxiosInstance, showAlert: any, jobId: string, jobType: string) { function startBgJob(api: AxiosInstance, showAlert: any, jobId: string, jobType: string) {
@ -17,7 +17,7 @@ export function useBackgroundJob() {
const evaluating = ref(false); const evaluating = ref(false);
const result = ref<BackgroundTaskStatus | null>(null); const result = ref<BackgroundTaskStatus | null>(null);
jobs.value[jobId] = { jobType, status: 'starting' }; jobs.value[jobId] = { jobType };
const interval = window.setInterval(async () => { const interval = window.setInterval(async () => {
try { try {
@ -25,8 +25,7 @@ export function useBackgroundJob() {
if (!result.value.running) { if (!result.value.running) {
clearJobFromList(); clearJobFromList();
} }
jobs.value[jobId] = { ...jobs.value[jobId], status: result.value.status }; jobs.value[jobId] = { ...jobs.value[jobId], taskStatus: result.value };
console.log('got result', result.value);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
showAlert('Failed to get background job status', 'error'); showAlert('Failed to get background job status', 'error');
@ -48,9 +47,18 @@ export function useBackgroundJob() {
} }
const runningJobs = computed(() => jobs.value); 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 { return {
runningJobs, runningJobs,
startBgJob, startBgJob,
clearJobs,
}; };
} }

View File

@ -8,6 +8,8 @@ export interface BackgroundTaskStatus {
status: string; status: string;
running: boolean; running: boolean;
progress?: number; progress?: number;
// TODO: type this properly
progress_tasks?: Record<string, any>;
} }
export interface BackgroundTaskResult { export interface BackgroundTaskResult {