diff --git a/backend/src/tasks/task-service.ts b/backend/src/tasks/task-service.ts index 55b2148c..f1f23b4e 100644 --- a/backend/src/tasks/task-service.ts +++ b/backend/src/tasks/task-service.ts @@ -615,15 +615,25 @@ class TaskService { return counts; } + // Get counts by status, with stale calculated from running tasks with old heartbeats const result = await pool.query( - `SELECT status, COUNT(*) as count + `SELECT + CASE + WHEN status IN ('running', 'claimed') + AND last_heartbeat_at < NOW() - INTERVAL '2 minutes' + THEN 'stale' + ELSE status + END as effective_status, + COUNT(*) as count FROM worker_tasks - GROUP BY status` + GROUP BY effective_status` ); for (const row of result.rows) { - const typedRow = row as { status: TaskStatus; count: string }; - counts[typedRow.status] = parseInt(typedRow.count, 10); + const typedRow = row as { effective_status: TaskStatus; count: string }; + if (typedRow.effective_status in counts) { + counts[typedRow.effective_status] = parseInt(typedRow.count, 10); + } } return counts;