fix(scheduler): start a tick's scans together; the slots cap them (#61)
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Agent integration (pull_request) Successful in 4m25s
CI / Check (pull_request) Successful in 17m11s

The scan tick awaited every tenant and every target one after another,
so one slow repository held up all the others and SCAN_CONCURRENCY never
applied to scheduled scans. The tick now starts them all; each scan
waits for one of the SCAN_CONCURRENCY slots (ScanGate), and the existing
start jitter of up to 30 s spreads them out.

Closes #61.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Sharang ParnerkarandClaude Opus 5.5 committed 2026-11-04 09:00:12 +01:00
1 parent 3300bb1851
commit 6f8a2f72da
1 file changed
+25 -15
+25 -15
View File
@@ -31,9 +31,15 @@ pub async fn start_scheduler(agent: &ComplianceAgent) -> Result<(), AgentError>
tenant_count = tenants.len(),
"Scheduled scan: tenants resolved"
);
for tenant_id in tenants {
scan_all_repos(&agent, &tenant_id).await;
}
// Every tenant and target at once: each scan waits for one of the
// `SCAN_CONCURRENCY` slots, so a slow repository holds one slot,
// not the whole tick (#61).
futures_util::future::join_all(
tenants
.iter()
.map(|tenant_id| scan_all_repos(&agent, tenant_id)),
)
.await;
})
})
.map_err(|e| AgentError::Scheduler(format!("Failed to create scan job: {e}")))?;
@@ -232,18 +238,22 @@ async fn scan_all_repos(agent: &ComplianceAgent, tenant_id: &str) {
let targets: Vec<_> = cursor.filter_map(|r| async { r.ok() }).collect().await;
for target in targets {
let target_id = target.id.map(|id| id.to_hex()).unwrap_or_default();
if let Err(e) = agent
.run_target_scan(tenant_id, &target_id, ScanTrigger::Scheduled)
.await
{
tracing::error!(
"Scheduled scan failed for {} (tenant '{tenant_id}'): {e}",
target.name
);
}
}
// Started together; the scan slots (`SCAN_CONCURRENCY`) decide how many
// run, and each scan's start jitter spreads them out.
futures_util::stream::iter(targets)
.for_each_concurrent(None, |target| async move {
let target_id = target.id.map(|id| id.to_hex()).unwrap_or_default();
if let Err(e) = agent
.run_target_scan(tenant_id, &target_id, ScanTrigger::Scheduled)
.await
{
tracing::error!(
"Scheduled scan failed for {} (tenant '{tenant_id}'): {e}",
target.name
);
}
})
.await;
}
async fn monitor_cves(agent: &ComplianceAgent, tenant_id: &str) {