fix(background-jobs): poll job list continuously so newly started jobs appear immediately in the floating widget

This commit is contained in:
Geert Rademakes 2025-08-08 10:20:30 +02:00
parent 7618c40a77
commit 9c8bf11986

View File

@ -92,25 +92,31 @@ export const BackgroundJobProgress: React.FC<BackgroundJobProgressProps> = ({
} }
}; };
// Start polling for all active jobs // Start polling for jobs and update progress
const startPolling = () => { const startPolling = () => {
if (intervalRef.current) { if (intervalRef.current) {
clearInterval(intervalRef.current); clearInterval(intervalRef.current);
} }
intervalRef.current = setInterval(() => { intervalRef.current = setInterval(async () => {
// Update all active jobs try {
const activeJobs = jobs.filter(job => job.status === 'running'); // Always reload job list to detect newly started jobs
const activeJobIds = activeJobs.map(job => job.jobId); const jobsData = await api.getAllJobs();
activeJobIds.forEach(jobId => { setJobs(jobsData);
updateJobProgress(jobId);
});
// Also update specific jobId if provided // Update progress for active jobs
if (jobId) { const activeJobIds = jobsData.filter((j: JobProgress) => j.status === 'running').map((j: JobProgress) => j.jobId);
updateJobProgress(jobId); for (const id of activeJobIds) {
await updateJobProgress(id);
} }
}, 2000); // Poll every 2 seconds for less frequent updates
if (jobId) {
await updateJobProgress(jobId);
}
} catch (err) {
// ignore transient polling errors
}
}, 2000);
}; };
// Stop polling // Stop polling
@ -121,22 +127,12 @@ export const BackgroundJobProgress: React.FC<BackgroundJobProgressProps> = ({
} }
}; };
// Load jobs on mount // Start polling on mount and stop on unmount
useEffect(() => { useEffect(() => {
loadJobs(); loadJobs();
}, []);
// Start polling for active jobs
useEffect(() => {
const activeJobs = jobs.filter(job => job.status === 'running');
if (activeJobs.length > 0 || jobId) {
startPolling(); startPolling();
} return () => stopPolling();
}, []);
return () => {
stopPolling();
};
}, [jobs, jobId]);
// Cleanup on unmount // Cleanup on unmount
useEffect(() => { useEffect(() => {