use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use colored::Colorize;
use super::BrokerState;
use crate::update::{self, Fetcher, MirrorOutcome};
pub const ENV_ENABLE: &str = "ZAKURO_AUTO_UPDATE";
pub const ENV_INTERVAL: &str = "ZAKURO_AUTO_UPDATE_INTERVAL";
pub const DEFAULT_INTERVAL_SECS: u64 = 3600;
pub const MIN_INTERVAL_SECS: u64 = 300;
const MIN_FIRST_DELAY_SECS: u64 = 60;
const IDLE_POLLS_REQUIRED: u32 = 3;
const IDLE_POLL_SECS: u64 = 5;
pub const RESTART_EXIT_CODE: i32 = 75;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Config {
pub enabled: bool,
pub interval_secs: u64,
}
pub fn parse_config(enable: Option<&str>, interval: Option<&str>) -> Config {
let enabled = enable.is_some_and(|v| {
matches!(
v.trim().to_ascii_lowercase().as_str(),
"1" | "true" | "yes" | "on"
)
});
let interval_secs = interval
.and_then(|v| v.trim().parse::<u64>().ok())
.unwrap_or(DEFAULT_INTERVAL_SECS)
.max(MIN_INTERVAL_SECS);
Config {
enabled,
interval_secs,
}
}
pub fn config_from_env() -> Config {
parse_config(
std::env::var(ENV_ENABLE).ok().as_deref(),
std::env::var(ENV_INTERVAL).ok().as_deref(),
)
}
pub fn is_newer_version(latest: &str, current: &str) -> bool {
match (
crate::build_id::version_triple(latest),
crate::build_id::version_triple(current),
) {
(Some(l), Some(c)) => l > c,
_ => false,
}
}
pub fn is_idle(in_flight: usize, local_worker_active: u64, settlement_pending: usize) -> bool {
in_flight == 0 && local_worker_active == 0 && settlement_pending == 0
}
#[derive(Debug, Clone, Default)]
struct Status {
enabled: bool,
interval_secs: u64,
last_check_unix: Option<u64>,
latest_seen: Option<String>,
last_error: Option<String>,
}
static STATUS: Mutex<Status> = Mutex::new(Status {
enabled: false,
interval_secs: DEFAULT_INTERVAL_SECS,
last_check_unix: None,
latest_seen: None,
last_error: None,
});
fn with_status(f: impl FnOnce(&mut Status)) {
if let Ok(mut s) = STATUS.lock() {
f(&mut s);
}
}
pub fn health_json() -> serde_json::Value {
let s = STATUS.lock().map(|s| s.clone()).unwrap_or_default();
serde_json::json!({
"enabled": s.enabled,
"interval_secs": s.interval_secs,
"last_check_unix": s.last_check_unix,
"latest_seen": s.latest_seen,
"last_error": s.last_error,
})
}
fn now_unix() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0)
}
fn jitter(bound: u64) -> u64 {
if bound == 0 {
return 0;
}
let n = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.subsec_nanos() as u64)
.unwrap_or(0);
let mut x = n ^ ((std::process::id() as u64) << 32) ^ 0x9E37_79B9_7F4A_7C15;
x ^= x >> 33;
x = x.wrapping_mul(0xFF51_AFD7_ED55_8CCD);
x ^= x >> 33;
x % bound
}
fn log(msg: &str) {
println!(" {} {}", "[AUTO-UPDATE]".cyan(), msg);
}
fn log_warn(msg: &str) {
eprintln!(" {} {}", "[AUTO-UPDATE]".yellow(), msg);
}
pub struct Cycle<'a> {
pub fetch: &'a dyn Fetcher,
pub base: &'a str,
pub platforms: &'a [String],
pub current: &'a str,
pub idle: &'a dyn Fn() -> bool,
pub sleep: &'a dyn Fn(Duration),
pub install: &'a dyn Fn() -> Result<MirrorOutcome, String>,
pub interval: Duration,
}
#[derive(Debug, PartialEq, Eq)]
pub enum CycleResult {
UpToDate(String),
NotIdle(String),
Installed(String),
InstallFailed(String),
CheckFailed(String),
}
pub fn run_cycle(c: &Cycle) -> CycleResult {
let latest = match update::check_latest_via(c.fetch, c.base, c.platforms) {
Ok(v) => v,
Err(e) => {
let e = format!("check failed (mirror {}): {e}", c.base);
with_status(|s| {
s.last_check_unix = Some(now_unix());
s.last_error = Some(e.clone());
});
log_warn(&e);
return CycleResult::CheckFailed(e);
}
};
with_status(|s| {
s.last_check_unix = Some(now_unix());
s.latest_seen = Some(latest.clone());
s.last_error = None;
});
if !is_newer_version(&latest, c.current) {
log(&format!(
"running {}, latest {latest}: up to date",
c.current
));
return CycleResult::UpToDate(latest);
}
log(&format!(
"running {}, latest {latest} (mirror {}): newer, waiting for idle",
c.current, c.base
));
let polls_budget = (c.interval.as_secs() / IDLE_POLL_SECS).max(IDLE_POLLS_REQUIRED as u64);
let mut streak = 0;
let mut idle_confirmed = false;
for _ in 0..polls_budget {
if (c.idle)() {
streak += 1;
if streak >= IDLE_POLLS_REQUIRED {
idle_confirmed = true;
break;
}
} else {
streak = 0;
}
(c.sleep)(Duration::from_secs(IDLE_POLL_SECS));
}
if !idle_confirmed {
log(&format!(
"broker never idle for {} polls; giving up on {latest} until the next check",
IDLE_POLLS_REQUIRED
));
return CycleResult::NotIdle(latest);
}
match (c.install)() {
Ok(MirrorOutcome::Installed { version, .. }) => CycleResult::Installed(version),
Ok(MirrorOutcome::UpToDate(v)) => CycleResult::UpToDate(v),
Err(e) => {
let e = format!("install failed: {e}; will retry at the next check");
with_status(|s| s.last_error = Some(e.clone()));
log_warn(&e);
CycleResult::InstallFailed(e)
}
}
}
pub fn wait_until_idle(idle: &dyn Fn() -> bool, sleep: &dyn Fn(Duration)) {
let mut streak = 0;
loop {
if idle() {
streak += 1;
if streak >= IDLE_POLLS_REQUIRED {
return;
}
} else {
streak = 0;
}
sleep(Duration::from_secs(IDLE_POLL_SECS));
}
}
pub fn spawn(state: Arc<BrokerState>) {
let cfg = config_from_env();
with_status(|s| {
s.enabled = cfg.enabled;
s.interval_secs = cfg.interval_secs;
});
if !cfg.enabled {
return;
}
log(&format!(
"enabled: checking the mirror every {}s (+jitter), installing only when idle",
cfg.interval_secs
));
let spawned = std::thread::Builder::new()
.name("zc-auto-update".into())
.spawn(move || {
let interval = Duration::from_secs(cfg.interval_secs);
let first = MIN_FIRST_DELAY_SECS
+ jitter(
cfg.interval_secs
.saturating_sub(MIN_FIRST_DELAY_SECS)
.max(1),
);
std::thread::sleep(Duration::from_secs(first));
let fetch = update::UreqFetcher::new();
loop {
let base = update::mirror_base();
if let Err(e) = &base {
log_warn(e);
with_status(|s| s.last_error = Some(e.clone()));
} else if let (Some(platforms), Ok(base)) =
(update::host_mirror_platforms(), base.as_ref())
{
let idle = || {
let local_active: u64 = state
.workers
.list()
.iter()
.filter(|w| w.source_node.is_none() && state.is_local_worker(&w.uri))
.map(|w| w.active_requests as u64)
.sum();
is_idle(
state.active_requests.len(),
local_active,
state.tx_buffer.pending_count(),
)
};
let result = run_cycle(&Cycle {
fetch: &fetch,
base,
platforms: &platforms,
current: env!("CARGO_PKG_VERSION"),
idle: &idle,
sleep: &std::thread::sleep,
install: &update::install_latest_from_mirror,
interval,
});
if let CycleResult::Installed(v) = result {
log(&format!("installed {v}, restarting once idle"));
wait_until_idle(&idle, &std::thread::sleep);
log(&format!("installed {v}, restarting"));
if let (Some(url), Some(key)) =
(&state.config.api_url, &state.config.api_key)
{
state.tx_buffer.flush_to_api(url, key, &state.wal);
}
let _ = state.wal.flush_buffer();
std::process::exit(RESTART_EXIT_CODE);
}
} else {
with_status(|s| {
s.last_error = Some("no mirror build for this OS/architecture".into())
});
}
let extra = jitter(cfg.interval_secs / 10 + 1);
std::thread::sleep(interval + Duration::from_secs(extra));
}
});
if let Err(e) = spawned {
log_warn(&format!("could not start updater thread: {e}"));
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::update::FetchError;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
#[test]
fn env_enabled_values() {
for v in ["1", "true", "TRUE", "yes", "On", " on "] {
assert!(parse_config(Some(v), None).enabled, "{v}");
}
for v in ["0", "false", "no", "off", "", "enabled", "2"] {
assert!(!parse_config(Some(v), None).enabled, "{v}");
}
assert!(!parse_config(None, None).enabled);
}
#[test]
fn env_interval_default_and_clamp() {
assert_eq!(parse_config(None, None).interval_secs, 3600);
assert_eq!(parse_config(None, Some("garbage")).interval_secs, 3600);
assert_eq!(parse_config(None, Some("10")).interval_secs, 300);
assert_eq!(parse_config(None, Some("300")).interval_secs, 300);
assert_eq!(parse_config(None, Some(" 7200 ")).interval_secs, 7200);
}
#[test]
fn newer_version_decision() {
assert!(is_newer_version("0.0.10", "0.0.9"));
assert!(!is_newer_version("0.0.9", "0.0.10"));
assert!(!is_newer_version("0.0.9", "0.0.9"));
assert!(!is_newer_version("0.0.8", "0.0.9"));
assert!(is_newer_version("v0.1.0", "0.0.99"));
assert!(!is_newer_version("junk", "0.0.1"));
}
#[test]
fn wait_until_idle_needs_a_fresh_streak() {
let seq = [false, true, false, true, true, true];
let i = Cell::new(0);
let slept = Cell::new(0);
wait_until_idle(
&|| {
let v = seq[i.get()];
i.set(i.get() + 1);
v
},
&|_| slept.set(slept.get() + 1),
);
assert_eq!(i.get(), seq.len());
assert_eq!(slept.get(), seq.len() - 1);
}
#[test]
fn idle_decision() {
assert!(is_idle(0, 0, 0));
assert!(!is_idle(1, 0, 0));
assert!(!is_idle(0, 2, 0));
assert!(!is_idle(0, 0, 3));
}
struct Mock(HashMap<String, Vec<u8>>, RefCell<Vec<String>>);
impl Fetcher for Mock {
fn get(&self, url: &str) -> Result<Vec<u8>, FetchError> {
self.1.borrow_mut().push(url.to_string());
self.0.get(url).cloned().ok_or(FetchError::Status(404))
}
}
const BASE: &str = "https://mirror.test/zc";
fn mirror(version: &str, bin: &[u8]) -> Mock {
let mut m = HashMap::new();
m.insert(
format!("{BASE}/manifest/linux_amd64"),
serde_json::json!({"version": version, "sha256": crate::dataset::sha256_hex(bin)})
.to_string()
.into_bytes(),
);
m.insert(
format!("{BASE}/download/linux_amd64?version={version}"),
bin.to_vec(),
);
Mock(m, RefCell::new(vec![]))
}
fn temp_exe(tag: &str) -> std::path::PathBuf {
let dir = std::env::temp_dir().join(format!("zc-autoupd-{tag}-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let exe = dir.join("zc");
std::fs::write(&exe, b"old").unwrap();
exe
}
fn cycle<'a>(
f: &'a Mock,
plats: &'a [String],
current: &'a str,
idle: &'a dyn Fn() -> bool,
sleep: &'a dyn Fn(Duration),
install: &'a dyn Fn() -> Result<MirrorOutcome, String>,
) -> Cycle<'a> {
Cycle {
fetch: f,
base: BASE,
platforms: plats,
current,
idle,
sleep,
install,
interval: Duration::from_secs(300),
}
}
#[test]
fn cycle_installs_newer_when_idle() {
let exe = temp_exe("ok");
let f = mirror("0.0.10", b"new bin");
let plats = vec!["linux_amd64".to_string()];
let install = || {
update::update_via_mirror(&f, BASE, &plats, "0.0.9", &exe, &|_, _| true)
.map_err(|e| format!("{e:?}"))
};
let r = run_cycle(&cycle(&f, &plats, "0.0.9", &|| true, &|_| {}, &install));
assert_eq!(r, CycleResult::Installed("0.0.10".into()));
assert_eq!(std::fs::read(&exe).unwrap(), b"new bin");
}
#[test]
fn cycle_skips_equal_and_older_without_installing() {
let f = mirror("0.0.9", b"x");
let plats = vec!["linux_amd64".to_string()];
let called = Cell::new(false);
let install = || {
called.set(true);
Err("no".to_string())
};
for cur in ["0.0.9", "0.0.10"] {
let r = run_cycle(&cycle(&f, &plats, cur, &|| true, &|_| {}, &install));
assert_eq!(r, CycleResult::UpToDate("0.0.9".into()));
}
assert!(!called.get());
}
#[test]
fn cycle_requires_three_consecutive_idle_polls() {
let f = mirror("0.0.10", b"x");
let plats = vec!["linux_amd64".to_string()];
let n = Cell::new(0);
let idle = || {
n.set(n.get() + 1);
n.get() != 2
};
let installed = Cell::new(false);
let install = || {
installed.set(true);
Ok(MirrorOutcome::Installed {
version: "0.0.10".into(),
platform: "linux_amd64".into(),
})
};
let r = run_cycle(&cycle(&f, &plats, "0.0.9", &idle, &|_| {}, &install));
assert_eq!(r, CycleResult::Installed("0.0.10".into()));
assert_eq!(n.get(), 5);
assert!(installed.get());
}
#[test]
fn cycle_gives_up_when_never_idle_and_never_installs() {
let f = mirror("0.0.10", b"x");
let plats = vec!["linux_amd64".to_string()];
let install = || panic!("must not install while busy");
let r = run_cycle(&cycle(&f, &plats, "0.0.9", &|| false, &|_| {}, &install));
assert_eq!(r, CycleResult::NotIdle("0.0.10".into()));
}
#[test]
fn cycle_reports_install_and_check_failures() {
let f = mirror("0.0.10", b"x");
let plats = vec!["linux_amd64".to_string()];
let install = || Err("dir not writable".to_string());
let r = run_cycle(&cycle(&f, &plats, "0.0.9", &|| true, &|_| {}, &install));
assert!(matches!(r, CycleResult::InstallFailed(_)));
let other = vec!["darwin_arm64".to_string()];
let r = run_cycle(&cycle(&f, &other, "0.0.9", &|| true, &|_| {}, &install));
assert!(matches!(r, CycleResult::CheckFailed(_)));
}
#[test]
fn jitter_is_bounded() {
for _ in 0..100 {
assert!(jitter(60) < 60);
}
assert_eq!(jitter(0), 0);
}
}