feat: add backgroundJob composable

This commit is contained in:
Matthias 2024-11-03 08:50:38 +01:00
parent 34189d28ab
commit 377d038dc2

View File

@ -0,0 +1,56 @@
import { BackgroundTaskStatus } from '@/types';
import { AxiosInstance } from 'axios';
const jobs = ref<Record<string, { jobType: string; status: string }>>({});
export function useBackgroundJob() {
function start(api: AxiosInstance, showAlert: any, jobId: string, jobType: string) {
async function getBackgroundJobStatus(jobId: string) {
try {
const { data } = await api.get<BackgroundTaskStatus>(`/background/${jobId}`);
return Promise.resolve(data);
} catch (error) {
console.error(error);
return Promise.reject(error);
}
}
const evaluating = ref(false);
const result = ref<BackgroundTaskStatus | null>(null);
jobs.value[jobId] = { jobType, status: 'starting' };
const interval = window.setInterval(async () => {
try {
result.value = await getBackgroundJobStatus(jobId);
if (!result.value.running) {
clearJobFromList();
}
jobs.value[jobId] = { ...jobs.value[jobId], status: result.value.status };
console.log('got result', result.value);
} catch (error) {
console.error(error);
showAlert('Failed to get background job status', 'error');
clearJobFromList();
}
}, 500);
function clearJobFromList() {
if (interval) {
clearInterval(interval);
}
evaluating.value = false;
}
return {
evaluating,
result,
};
}
const runningJobs = computed(() => jobs.value);
return {
runningJobs,
start,
};
}