Index lifecycle capacity checks instead of scanning retained jobs

This commit is contained in:
2026-10-06 12:54:10 -04:00
parent 37d380fa45
commit d2108eb604
6 changed files with 95 additions and 8 deletions
+17 -6
View File
@@ -8,18 +8,29 @@ interface Holder { id: string; action: string; host_id: string | null; target_ho
export function lifecycleHolders(): Holder[] {
ensureHostSchema(db());
// Find capacity owners through their active state or outstanding command.
// A correlated OR over d used to walk every retained job and fetch its agent
// receipt on every admission, even when all that history was completed.
return db().prepare(`SELECT d.id,d.action,i.host_id,m.target_host_id,
CASE WHEN m.remove_operation_id IS NOT NULL AND m.removed_at IS NULL THEN 1 ELSE 0 END AS removing
FROM server_deployments d
LEFT JOIN native_provisioning n ON n.deployment_id=d.id
LEFT JOIN native_instances i ON i.id=n.instance_id
LEFT JOIN ec_host_migrations m ON m.deployment_id=d.id
WHERE d.state IN ('running','awaiting-health')
OR d.id IN (SELECT value FROM json_each(?))
OR EXISTS (SELECT 1 FROM restart_rollout r WHERE r.operation_id=d.id AND r.healthy_at IS NULL)
OR (m.remove_operation_id IS NOT NULL AND d.state NOT IN ('done','warning','cancelled'))
OR EXISTS (SELECT 1 FROM ec_host_operations o WHERE o.id IN (n.operation_id,m.remove_operation_id)
AND o.state IN ('pending','accepted','running','unknown'))`)
WHERE d.id IN (
SELECT id FROM server_deployments WHERE state IN ('running','awaiting-health')
UNION SELECT value FROM json_each(?)
UNION SELECT operation_id FROM restart_rollout WHERE healthy_at IS NULL
UNION SELECT deployment_id FROM ec_host_migrations
WHERE remove_operation_id IS NOT NULL AND deployment_id IN (
SELECT id FROM server_deployments WHERE state NOT IN ('done','warning','cancelled'))
UNION SELECT n.deployment_id FROM ec_host_operations o
JOIN native_provisioning n ON n.operation_id=o.id
WHERE o.state IN ('pending','accepted','running','unknown')
UNION SELECT m.deployment_id FROM ec_host_operations o
JOIN ec_host_migrations m ON m.remove_operation_id=o.id
WHERE o.state IN ('pending','accepted','running','unknown')
)`)
.all(JSON.stringify([...preparingJobs])) as Holder[];
}