From 6f8a2f72dab2442478440d7de1a035c466ea6461 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Wed, 4 Nov 2026 09:00:12 +0100 Subject: [PATCH] fix(scheduler): start a tick's scans together; the slots cap them (#61) 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 --- compliance-agent/src/scheduler.rs | 40 +++++++++++++++++++------------ 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/compliance-agent/src/scheduler.rs b/compliance-agent/src/scheduler.rs index 6100863..b437bdb 100644 --- a/compliance-agent/src/scheduler.rs +++ b/compliance-agent/src/scheduler.rs @@ -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) { -- 2.54.1