Fix retention query performance and cleanup scheduling

This commit is contained in:
2026-10-06 10:39:32 -04:00
parent 9fd448339d
commit 88a950692a
8 changed files with 104 additions and 8 deletions
-4
View File
@@ -1,8 +1,6 @@
import { pruneHistory } from '../retention.js';
import { db } from '../db/index.js';
import { emit } from './bus.js';
import { requestRestart } from '../provisioning/index.js';
import { pruneHostObservations } from '../hosts/index.js';
interface PendingRow {
deployment_id: string; server: string; restart_state: string;
@@ -20,7 +18,6 @@ export function sweep(): Promise<SweepResult> {
return sweepInFlight;
}
async function runSweep(): Promise<SweepResult> {
pruneHostObservations();
const pending = db().prepare(`SELECT ds.* FROM deployment_servers ds JOIN deployments d ON d.id = ds.deployment_id
WHERE ds.server_deleted_at IS NULL AND ds.restart_state IN ('pending','failed')
AND NOT EXISTS (SELECT 1 FROM retention_controls c WHERE c.category='campaign' AND c.record_id=ds.deployment_id AND c.resolved_at IS NOT NULL) ORDER BY d.started_at, d.id, ds.server`).all() as PendingRow[];
@@ -47,7 +44,6 @@ async function runSweep(): Promise<SweepResult> {
}
db().prepare(`UPDATE deployments SET finished_at = ? WHERE finished_at IS NULL AND NOT EXISTS (
SELECT 1 FROM deployment_servers WHERE deployment_id = deployments.id AND server_deleted_at IS NULL AND restart_state IN ('pending','failed','held'))`).run(nowIso());
pruneHistory('campaign');
emit({ type: 'sweep-tick', frozen: false });
return result;
}
+6
View File
@@ -21,6 +21,7 @@ import { trackActivity } from './activity.js';
import { withOperationSource } from '../hosts/operation-context.js';
const TICK_MS = 15_000;
const RETENTION_INTERVAL_MS = 5 * 60_000;
// Match each agent's two health lanes without making unrelated hosts wait.
const STATUS_CONCURRENCY_PER_HOST = 2;
@@ -31,6 +32,7 @@ let statusTimer: NodeJS.Timeout | null = null;
let tickInFlight: Promise<void> | null = null;
let tickRequested = false;
const requesterFlights = new Map<string, Promise<void>>();
let nextRetentionAt = 0;
function tick(requestAnother = false): Promise<void> {
if (requestAnother) tickRequested = true;
@@ -57,6 +59,10 @@ function reconcileRequesters(): void {
];
for (const [key, label, work] of requesters) {
if (requesterFlights.has(key)) continue;
if (key === 'retention') {
if (Date.now() < nextRetentionAt) continue;
nextRetentionAt = Date.now() + RETENTION_INTERVAL_MS;
}
const flight = trackActivity(key, label, work)
.catch(err => { console.error(`[worker] ${key} error:`, err); })
.then(async () => { if (key !== 'game-files' && key !== 'retention') await tick(true); })