feat: per-region due scheduling + run_id to group pings across regions

This commit is contained in:
M1
2026-03-18 16:36:35 +04:00
parent e057a65535
commit f7ab3b96b2
6 changed files with 20 additions and 5 deletions
+9 -2
View File
@@ -33,6 +33,9 @@ pub async fn fetch_and_run(
let n = monitors.len();
if n == 0 { return Ok(0); }
// One run_id per fetch batch — groups all checks from this cycle across regions
let run_id = uuid::Uuid::new_v4().to_string();
// Spawn all checks — fire and forget, skip if already in-flight
let mut spawned = 0usize;
for monitor in monitors {
@@ -49,12 +52,13 @@ pub async fn fetch_and_run(
let coordinator_url = coordinator_url.to_string();
let token = token.to_string();
let region_owned = region.to_string();
let run_id_owned = run_id.clone();
let in_flight = in_flight.clone();
tokio::spawn(async move {
let timeout_ms = monitor.timeout_ms.unwrap_or(30000);
// Hard deadline: timeout + 5s buffer, so hung checks always resolve
let deadline = std::time::Duration::from_millis(timeout_ms + 5000);
let result = match tokio::time::timeout(deadline, run_check(&client, &monitor, monitor.scheduled_at.clone(), &region_owned)).await {
let result = match tokio::time::timeout(deadline, run_check(&client, &monitor, monitor.scheduled_at.clone(), &region_owned, &run_id_owned)).await {
Ok(r) => r,
Err(_) => PingResult {
monitor_id: monitor.id.clone(),
@@ -67,6 +71,7 @@ pub async fn fetch_and_run(
cert_expiry_days: None,
meta: None,
region: if region_owned.is_empty() { None } else { Some(region_owned.clone()) },
run_id: Some(run_id_owned.clone()),
},
};
// Post result first, then clear in-flight — this prevents the next
@@ -81,7 +86,7 @@ pub async fn fetch_and_run(
Ok(spawned)
}
async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Option<String>, region: &str) -> PingResult {
async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Option<String>, region: &str, run_id: &str) -> PingResult {
// Compute jitter: how late we actually started vs when we were scheduled
let jitter_ms: Option<i64> = scheduled_at.as_deref().and_then(|s| {
let scheduled = chrono::DateTime::parse_from_rfc3339(s).ok()?;
@@ -135,6 +140,7 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
cert_expiry_days: None,
meta: None,
region: if region.is_empty() { None } else { Some(region.to_string()) },
run_id: Some(run_id.to_string()),
}
},
Ok((status_code, headers, body)) => {
@@ -195,6 +201,7 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
cert_expiry_days,
meta: Some(meta),
region: if region.is_empty() { None } else { Some(region.to_string()) },
run_id: Some(run_id.to_string()),
}
}
}
+1
View File
@@ -28,4 +28,5 @@ pub struct PingResult {
pub cert_expiry_days: Option<i64>,
pub meta: Option<Value>,
pub region: Option<String>,
pub run_id: Option<String>,
}