|
| 1 | +//! Lightweight update notice check for the proxy path. |
| 2 | +//! |
| 3 | +//! Before exec'ing the CLI, we check a cache file to see if a newer version |
| 4 | +//! is available. If the cache is stale (>24h), we do a quick HTTP check with |
| 5 | +//! a short timeout to refresh it. The notice is printed to stderr. |
| 6 | +
|
| 7 | +use std::path::{Path, PathBuf}; |
| 8 | +use std::time::{Duration, SystemTime, UNIX_EPOCH}; |
| 9 | + |
| 10 | +use colored::Colorize; |
| 11 | + |
| 12 | +use crate::cli::install::fetch_latest_release_version; |
| 13 | + |
| 14 | +/// How long to cache the update check result. |
| 15 | +const CHECK_INTERVAL: Duration = Duration::from_secs(24 * 60 * 60); |
| 16 | +const UPDATE_CHECK_TIMEOUT: Duration = Duration::from_secs(2); |
| 17 | + |
| 18 | +/// Cache file name. |
| 19 | +const CACHE_FILENAME: &str = ".update_check_cache"; |
| 20 | + |
| 21 | +#[derive(serde::Serialize, serde::Deserialize, Default)] |
| 22 | +struct Cache { |
| 23 | + /// Unix timestamp of the last successful check. |
| 24 | + last_check_secs: u64, |
| 25 | + /// The latest version string (without "v" prefix). |
| 26 | + latest_version: String, |
| 27 | +} |
| 28 | + |
| 29 | +impl Cache { |
| 30 | + fn read(config_dir: &Path) -> Option<Self> { |
| 31 | + let contents = std::fs::read_to_string(Self::path(config_dir)).ok()?; |
| 32 | + serde_json::from_str(&contents).ok() |
| 33 | + } |
| 34 | + |
| 35 | + fn write(&self, config_dir: &Path) { |
| 36 | + if let Ok(json) = serde_json::to_string(self) { |
| 37 | + let _ = std::fs::write(Self::path(config_dir), json); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + fn path(config_dir: &Path) -> PathBuf { |
| 42 | + config_dir.join(CACHE_FILENAME) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +fn now_secs() -> u64 { |
| 47 | + SystemTime::now() |
| 48 | + .duration_since(UNIX_EPOCH) |
| 49 | + .unwrap_or_default() |
| 50 | + .as_secs() |
| 51 | +} |
| 52 | + |
| 53 | +/// Resolve the latest version, using the cache if fresh or fetching from the network. |
| 54 | +/// |
| 55 | +/// On success, updates the cache. On network failure, leaves the cache unchanged |
| 56 | +/// so we retry on the next invocation. |
| 57 | +fn latest_version_or_cached(config_dir: &Path) -> Option<semver::Version> { |
| 58 | + let cache = Cache::read(config_dir); |
| 59 | + let now = now_secs(); |
| 60 | + |
| 61 | + // Cache is fresh — use it. |
| 62 | + if let Some(ref cache) = cache |
| 63 | + && now.saturating_sub(cache.last_check_secs) < CHECK_INTERVAL.as_secs() |
| 64 | + { |
| 65 | + return semver::Version::parse(&cache.latest_version).ok(); |
| 66 | + } |
| 67 | + |
| 68 | + // Cache is stale or missing — fetch from network. |
| 69 | + let client = crate::cli::reqwest_client_builder() |
| 70 | + .timeout(UPDATE_CHECK_TIMEOUT) |
| 71 | + .build() |
| 72 | + .ok()?; |
| 73 | + |
| 74 | + let latest = crate::cli::tokio_block_on(async { fetch_latest_release_version(&client).await }).flatten(); |
| 75 | + |
| 76 | + match latest { |
| 77 | + Ok(version) => { |
| 78 | + Cache { |
| 79 | + last_check_secs: now, |
| 80 | + latest_version: version.to_string(), |
| 81 | + } |
| 82 | + .write(config_dir); |
| 83 | + Some(version) |
| 84 | + } |
| 85 | + Err(e) => { |
| 86 | + log::debug!("Failed to fetch latest version from network; will retry next invocation: {e}"); |
| 87 | + // Don't update cache — retry next time. |
| 88 | + // Fall back to stale cache if available. |
| 89 | + cache.and_then(|c| semver::Version::parse(&c.latest_version).ok()) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/// Check for updates and print a notice to stderr if a newer version is available. |
| 95 | +/// |
| 96 | +/// This is designed to be called from the proxy path before exec'ing the CLI. |
| 97 | +/// It reads a cache file to avoid hitting the network on every invocation. |
| 98 | +/// If the cache is stale, it makes a quick HTTP request (with timeout) to refresh. |
| 99 | +/// |
| 100 | +/// `config_dir` should be the SpacetimeDB config directory (e.g. `~/.spacetime`). |
| 101 | +#[allow(clippy::disallowed_macros)] |
| 102 | +pub(crate) fn maybe_print_update_notice(config_dir: &Path) { |
| 103 | + let current = env!("CARGO_PKG_VERSION"); |
| 104 | + let current = match semver::Version::parse(current) { |
| 105 | + Ok(v) => v, |
| 106 | + Err(e) => { |
| 107 | + log::debug!("Failed to parse current version: {e}"); |
| 108 | + return; |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + let latest = match latest_version_or_cached(config_dir) { |
| 113 | + Some(v) => v, |
| 114 | + None => return, |
| 115 | + }; |
| 116 | + |
| 117 | + if latest > current { |
| 118 | + eprintln!( |
| 119 | + "{}", |
| 120 | + format!("A new version of SpacetimeDB is available: v{latest} (current: v{current})").yellow() |
| 121 | + ); |
| 122 | + eprintln!("Run `spacetime version upgrade` to update."); |
| 123 | + eprintln!(); |
| 124 | + } |
| 125 | +} |
0 commit comments