A small, fast container runtime for macOS, Windows and Linux
Rust
Check for updates every 6 hours while Kiri runs (#32)
Precious Okwud01c3edb2325
diff --git a/crates/kiri-win/src/update.rs b/crates/kiri-win/src/update.rsindex 864e7d8..921dcb4 100644--- a/crates/kiri-win/src/update.rs+++ b/crates/kiri-win/src/update.rs@@ -32,6 +32,9 @@ use std::process::{Command, Stdio};use std::sync::atomic::{AtomicBool, Ordering};use std::time::{Duration, Instant};+#[path = "../../kiri/src/ui/recheck.rs"]+mod recheck;+use serde_json::json;use crate::engine::{emit, notice};@@ -449,6 +452,16 @@ pub fn check_now(manual: bool) {{emit(json!({"type": "enginestale", "version": v}).to_string());}+ feed_check(manual);+ });+ // And every 6 hours while Kiri runs (recheck.rs), as on the Mac.+ recheck::start(|| feed_check(false));+}++/// Whether the feed has a newer release, and one already staged; the+/// window's check and the 6-hourly one.+fn feed_check(manual: bool) {+ {// A test build (0.1.8-dev.42, scripts/ci/test-version.sh) isn't// updated from the feed: it would "update" back to its release.if CURRENT.contains("-dev") {@@ -465,6 +478,7 @@ pub fn check_now(manual: bool) {if !manual && crate::engine::prefs()["updateAuto"] == false {return;}+ recheck::checked();match latest() {Ok(r) if newer(&r.version, CURRENT) => {update_state("available", Some(&r.version), None);@@ -483,7 +497,7 @@ pub fn check_now(manual: bool) {}}}- });+ }}/// Download on the card: fetches, checks and stages the release in thediff --git a/crates/kiri/src/ui/index.html b/crates/kiri/src/ui/index.htmlindex bd51120..c61b93b 100644--- a/crates/kiri/src/ui/index.html+++ b/crates/kiri/src/ui/index.html@@ -5786,6 +5786,8 @@ window.kiri = {if (!e.manual && (prefs.updateSkip === r.version || (prefs.updateSnoozeUntil || 0) > Date.now())) break;if (e.manual) { setPref("updateSkip", null); setPref("updateSnoozeUntil", null); }if (ui.upd && ui.upd.phase !== "available") break;+ // The 6-hourly check finds the same release: a card closed until the next launch stays closed.+ if (!e.manual && ui.upd?.hidden && ui.upd.version === r.version) break;ui.upd = { phase: "available", version: r.version, current: e.current, notes: r.notes || "", notesUrl: r.notes_url || "", hidden: false };renderUpdate(); break;}@@ -5794,7 +5796,7 @@ window.kiri = {if (ui.upd.phase !== "downloading") Object.assign(ui.upd, { phase: "downloading", t0: Date.now(), g0: e.got });Object.assign(ui.upd, { got: e.got, total: e.total }); renderUpdate(); break;case "updatestate": ui.updState = { ...e, checking: false }; if (ui.view === "settings" && prefs.tab === "about") render(); break;- case "updateready": ui.upd = { ...(ui.upd || {}), phase: "ready", version: e.version, hidden: false, error: "" }; renderUpdate(); break;+ case "updateready": if (ui.upd?.phase === "ready" && ui.upd.version === e.version && ui.upd.hidden) break; ui.upd = { ...(ui.upd || {}), phase: "ready", version: e.version, hidden: false, error: "" }; renderUpdate(); break;case "updatecancelled": if (ui.upd) Object.assign(ui.upd, { phase: "available", hidden: true }); renderUpdate(); break;case "updateerror": if (ui.upd) Object.assign(ui.upd, { phase: "available", hidden: false, error: e.error }); else toast("error", e.error); renderUpdate(); break;case "enginestale": ui.engineStale = e.version; ui.staleHidden = false; renderUpdate(); break;diff --git a/crates/kiri/src/ui/mod.rs b/crates/kiri/src/ui/mod.rsindex 114b1f1..cbc1f87 100644--- a/crates/kiri/src/ui/mod.rs+++ b/crates/kiri/src/ui/mod.rs@@ -31,6 +31,7 @@ mod linux;use linux::{import, setup, update};mod brands;+mod recheck;mod detail;mod hub;mod k8s;@@ -566,7 +567,12 @@ fn update_check(manual: bool) {{emit(serde_json::json!({"type": "enginestale", "version": v}).to_string());}- // Kiri Dev is rebuilt from source, never updated from the feed, unless+ feed_check(manual);+}++/// Whether the feed has a newer release, and one already staged; the+/// window's check and the 6-hourly one (recheck.rs).+fn feed_check(manual: bool) { // Kiri Dev is rebuilt from source, never updated from the feed, unless// KIRI_UPDATE_URL names a test feed (to try the update card and What's new).if crate::flavor::dev() && std::env::var_os("KIRI_UPDATE_URL").is_none() {if manual {@@ -591,6 +597,7 @@ fn update_check(manual: bool) {if !manual && prefs()["updateAuto"] == false {return;}+ recheck::checked();// Linux's kiri comes from a .deb or .rpm: the feed only says whether a// newer one exists, and About links to the releases page, so no card.if cfg!(target_os = "linux") {@@ -1947,9 +1954,11 @@ fn handle(text: &str, tx: &Sender<Cmd>, autostart: bool, view: Option<&str>) {}});diag::report::spawn_crash_sender(Duration::from_secs(5), None);- // A newer release, checked once per window, quietly; one- // already downloaded; an engine still on an older build.+ // A newer release, checked when the window opens and every 6+ // hours after (recheck.rs), quietly; one already downloaded; an+ // engine still on an older build.std::thread::spawn(|| update_check(false));+ recheck::start(|| feed_check(false));// The first time the app opens: ask, once, whether to use *.kiri.local (the Mac only, so far).if cfg!(target_os = "macos") && !crate::flavor::dev() && !crate::daemon::dns::installed() && prefs()["domainsAsked"] != true {set_pref("domainsAsked", true.into());diff --git a/crates/kiri/src/ui/recheck.rs b/crates/kiri/src/ui/recheck.rsnew file mode 100644index 0000000..cbf7998--- /dev/null+++ b/crates/kiri/src/ui/recheck.rs@@ -0,0 +1,66 @@+//! Asking for a newer release again while Kiri keeps running. The window+//! checks when it opens; a laptop that stays up for days, sleeping and+//! waking with Kiri open, would otherwise never hear of a release until+//! Kiri was quit. Every 6 hours by the wall clock, so time asleep counts:+//! a Mac that wakes after a night checks within minutes. The Mac and Linux+//! (ui/mod.rs) and Windows (kiri-win's update.rs, which includes this file)+//! use it the same way.++use std::sync::{Mutex, Once, PoisonError};+use std::time::{Duration, SystemTime};++/// How long after one check the next is due.+pub const EVERY: Duration = Duration::from_secs(6 * 60 * 60);+/// How often the timer looks at the clock: a monotonic sleep stops while+/// the computer sleeps, so it wakes often and compares wall-clock times.+const TICK: Duration = Duration::from_secs(10 * 60);++static LAST: Mutex<Option<SystemTime>> = Mutex::new(None);++/// A check asked the feed just now.+pub fn checked() {+ *LAST.lock().unwrap_or_else(PoisonError::into_inner) = Some(SystemTime::now());+}++fn due(last: Option<SystemTime>, now: SystemTime) -> bool {+ match last {+ None => true,+ // A clock set back: check, and count from now.+ Some(l) => now.duration_since(l).map_or(true, |d| d >= EVERY),+ }+}++/// Starts the timer once for the life of the process (a page reload asks+/// again): `check` runs whenever a check is due.+pub fn start(check: fn()) {+ static ONCE: Once = Once::new();+ ONCE.call_once(|| {+ let _ = std::thread::Builder::new().name("kiri-recheck".into()).spawn(move || {+ loop {+ std::thread::sleep(TICK);+ let last = *LAST.lock().unwrap_or_else(PoisonError::into_inner);+ if due(last, SystemTime::now()) {+ checked();+ check();+ }+ }+ });+ });+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn due_six_hours_after_the_last_check_by_the_wall_clock() {+ let t = SystemTime::UNIX_EPOCH + Duration::from_secs(1_800_000_000);+ assert!(due(None, t));+ assert!(!due(Some(t), t + Duration::from_secs(5 * 3600)));+ assert!(due(Some(t), t + EVERY));+ // Asleep all night: due the moment it wakes.+ assert!(due(Some(t), t + Duration::from_secs(14 * 3600)));+ // The clock went back.+ assert!(due(Some(t), t - Duration::from_secs(60)));+ }+}