fix: improve monitor
This commit is contained in:
parent
de5f7580a6
commit
005f635fab
|
|
@ -142,7 +142,6 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
|
||||||
let method = monitor.method.as_deref().unwrap_or("GET").to_uppercase();
|
let method = monitor.method.as_deref().unwrap_or("GET").to_uppercase();
|
||||||
let timeout = std::time::Duration::from_millis(monitor.timeout_ms.unwrap_or(30000));
|
let timeout = std::time::Duration::from_millis(monitor.timeout_ms.unwrap_or(30000));
|
||||||
let is_https = monitor.url.starts_with("https://");
|
let is_https = monitor.url.starts_with("https://");
|
||||||
let url_for_cert = monitor.url.clone();
|
|
||||||
|
|
||||||
// Run the check in a real OS thread using ureq (blocking, synchronous HTTP).
|
// Run the check in a real OS thread using ureq (blocking, synchronous HTTP).
|
||||||
// ureq sets SO_RCVTIMEO/SO_SNDTIMEO at the socket level, which reliably
|
// ureq sets SO_RCVTIMEO/SO_SNDTIMEO at the socket level, which reliably
|
||||||
|
|
@ -151,12 +150,11 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
|
||||||
let url = monitor.url.clone();
|
let url = monitor.url.clone();
|
||||||
let req_headers = monitor.request_headers.clone();
|
let req_headers = monitor.request_headers.clone();
|
||||||
let req_body = monitor.request_body.clone();
|
let req_body = monitor.request_body.clone();
|
||||||
let method_clone = method.clone();
|
|
||||||
|
|
||||||
let (tx, rx) = tokio::sync::oneshot::channel::<Result<(u16, HashMap<String, String>, String), String>>();
|
let (tx, rx) = tokio::sync::oneshot::channel::<Result<(u16, HashMap<String, String>, String), String>>();
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||||
run_check_blocking(&url, &method_clone, req_headers.as_ref(), req_body.as_deref(), timeout)
|
run_check_blocking(&url, &method, req_headers.as_ref(), req_body.as_deref(), timeout)
|
||||||
}));
|
}));
|
||||||
let _ = tx.send(match result {
|
let _ = tx.send(match result {
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
|
|
@ -164,7 +162,7 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
let curl_result = tokio::time::timeout(timeout + std::time::Duration::from_secs(2), rx)
|
let result = tokio::time::timeout(timeout + std::time::Duration::from_secs(2), rx)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| format!("timed out after {}ms", timeout.as_millis()))
|
.map_err(|_| format!("timed out after {}ms", timeout.as_millis()))
|
||||||
.and_then(|r| r.map_err(|_| "check thread dropped".to_string()))
|
.and_then(|r| r.map_err(|_| "check thread dropped".to_string()))
|
||||||
|
|
@ -172,8 +170,6 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
|
||||||
|
|
||||||
let latency_ms = start.elapsed().as_millis() as u64;
|
let latency_ms = start.elapsed().as_millis() as u64;
|
||||||
|
|
||||||
let result = curl_result;
|
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Err(ref e) => {
|
Err(ref e) => {
|
||||||
debug!("{} check error: {e}", monitor.url);
|
debug!("{} check error: {e}", monitor.url);
|
||||||
|
|
@ -192,17 +188,17 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op
|
||||||
run_id: Some(run_id.to_string()),
|
run_id: Some(run_id.to_string()),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Ok((status_code, headers, body)) => {
|
Ok((status, headers, body)) => {
|
||||||
let status = status_code;
|
|
||||||
|
|
||||||
// Start cert expiry check in background — don't block result posting.
|
// Start cert expiry check in background — don't block result posting.
|
||||||
// We'll use None for cert_expiry_days in query evaluation since it
|
// We'll use None for cert_expiry_days in query evaluation since it
|
||||||
// shouldn't delay the main result by seconds of extra TLS handshake.
|
// shouldn't delay the main result by seconds of extra TLS handshake.
|
||||||
let cert_handle = if is_https {
|
let cert_handle = if is_https {
|
||||||
|
let cert_url = monitor.url.clone();
|
||||||
Some(tokio::spawn(async move {
|
Some(tokio::spawn(async move {
|
||||||
match tokio::time::timeout(
|
match tokio::time::timeout(
|
||||||
std::time::Duration::from_secs(5),
|
std::time::Duration::from_secs(5),
|
||||||
check_cert_expiry(&url_for_cert),
|
check_cert_expiry(&cert_url),
|
||||||
).await {
|
).await {
|
||||||
Ok(Ok(days)) => days,
|
Ok(Ok(days)) => days,
|
||||||
_ => None,
|
_ => None,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue