use eclipse_core::config::Config;
use eclipse_web::app::Shared;
use std::time::Duration;
pub fn spawn_model_probe(state: Shared, cfg: Config) {
let Some(agent) = state.agent.clone() else {
return;
};
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(60));
loop {
interval.tick().await;
let (model, endpoint) = match agent.route_infos().into_iter().next() {
Some(r) => (r.model, r.endpoint),
None => (cfg.provider.model.clone(), cfg.provider.base_url.clone()),
};
let reachable = eclipse_core::provider::advertised_context_window(
&endpoint,
cfg.provider.api_key.as_deref(),
&model,
Duration::from_secs(2),
)
.await
.is_some();
*state.model_probe.lock().unwrap_or_else(|e| e.into_inner()) =
Some((std::time::Instant::now(), reachable));
}
});
}
fn hours_as_secs(hours: u64) -> u64 {
hours.saturating_mul(3600)
}
fn hours_as_ms(hours: u64) -> u64 {
hours.saturating_mul(3_600_000)
}
pub fn spawn_upgrade_check(cfg: Config, state: std::path::PathBuf) {
if cfg.gateway.check_interval_hours == 0 {
return;
}
let every = Duration::from_secs(hours_as_secs(cfg.gateway.check_interval_hours));
tokio::spawn(async move {
loop {
log_outcome(eclipse_core::update::refresh(&state, &cfg.update).await);
tokio::time::sleep(every).await;
}
});
}
fn verdict_line(outcome: &eclipse_core::update::Outcome) -> Option<String> {
use eclipse_core::update::Outcome;
match outcome {
Outcome::NotDue => None,
Outcome::Current => Some("[upgrade] current".to_string()),
Outcome::Behind(behind) => Some(format!(
"[upgrade] behind: {} available, installed {}",
behind.latest, behind.installed
)),
Outcome::Failed(why) => Some(format!("[upgrade] {why}")),
}
}
fn log_outcome(outcome: eclipse_core::update::Outcome) {
if let Some(line) = verdict_line(&outcome) {
eprintln!("{line}");
}
}
pub fn spawn_session_prune(state: Shared, cfg: Config) {
let ttl = cfg.gateway.api_session_ttl_hours;
if ttl == 0 {
return;
}
let Some(agent) = state.agent.clone() else {
return;
};
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(3600));
loop {
interval.tick().await;
let cutoff = eclipse_core::session::now_ms().saturating_sub(hours_as_ms(ttl));
match agent.store().prune_api_sessions(cutoff) {
Ok(0) => {}
Ok(n) => eprintln!("[sessions] pruned {n} API session(s) older than {ttl}h"),
Err(e) => eprintln!("[sessions] could not prune API sessions: {e}"),
}
}
});
}
#[cfg(all(test, unix))]
mod tests {
use super::*;
use eclipse_core::update::{Behind, Outcome};
#[test]
fn a_tick_reports_what_it_found_and_stays_quiet_when_it_asked_nothing() {
let behind = Outcome::Behind(Behind {
installed: "0.7.0".into(),
latest: "v0.8.0".into(),
});
let line = verdict_line(&behind).expect("a behind tick says so");
assert!(
line.contains("v0.8.0") && line.contains("0.7.0"),
"both numbers, or the operator cannot tell what to do: {line}"
);
assert_eq!(verdict_line(&Outcome::NotDue), None);
assert!(verdict_line(&Outcome::Failed("no route".into()))
.expect("a failure is worth a line")
.contains("no route"));
}
#[test]
fn a_huge_ttl_means_never_and_does_not_wrap_into_pruning_everything() {
let never = 1_u64 << 57;
assert_eq!(hours_as_ms(never), u64::MAX, "2**57 hours is not zero ms");
assert_eq!(hours_as_secs(never), u64::MAX, "2**57 hours is not zero s");
let now = 1_700_000_000_000_u64;
assert_eq!(now.saturating_sub(hours_as_ms(never)), 0);
let exemplar = 10_249_152_000_000_u64;
assert_eq!(now.saturating_sub(hours_as_ms(exemplar)), 0);
assert_eq!(hours_as_ms(24), 86_400_000);
assert_eq!(hours_as_secs(6), 21_600);
}
}