use crate::runner::RunnerConfig;
use reqwest::header::{HeaderValue, AUTHORIZATION};
pub fn start(config: &RunnerConfig) -> tokio::task::JoinHandle<()> {
let url = config.api_url(&format!("/runners/{}/heartbeat", config.runner_id));
let auth = config.auth_header();
let interval_ms = config.heartbeat_interval_ms;
let client = reqwest::Client::new();
tokio::spawn(async move {
let mut interval = tokio::time::interval(std::time::Duration::from_millis(interval_ms));
loop {
interval.tick().await;
match client
.post(&url)
.header(AUTHORIZATION, HeaderValue::from_str(&auth).unwrap())
.send()
.await
{
Ok(resp) if resp.status().is_success() => {}
Ok(resp) => {
eprintln!("heartbeat warning: server returned {}", resp.status());
}
Err(e) => {
eprintln!("heartbeat error: {e}");
}
}
}
})
}
pub async fn send_once(
config: &RunnerConfig,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let url = config.api_url(&format!("/runners/{}/heartbeat", config.runner_id));
let client = reqwest::Client::new();
let resp = client
.post(&url)
.header(
AUTHORIZATION,
HeaderValue::from_str(&config.auth_header()).unwrap(),
)
.send()
.await?;
if resp.status().is_success() {
Ok(())
} else {
Err(format!("heartbeat failed: {}", resp.status()).into())
}
}