Discover: report phase progress and reduce transient observation contention

This commit is contained in:
Pavel Isaev 2026-11-11 21:24:50 +03:00
parent b5215ca0a5
commit 23d7269772
2 changed files with 34 additions and 2 deletions

View file

@ -4,6 +4,25 @@ use archipelago_contracts::system_bus::{ErrorCode, Phase};
use serde_json::{json, Value};
use std::sync::{Arc, Mutex};
/// Coarse, monotonic progress percentage derived from the operation phase.
/// Discover renders this as its transaction progress bar; the daemon does not
/// currently report a byte-accurate percentage, so the phase is the durable cue.
fn phase_percent(phase: Phase) -> u64 {
match phase {
Phase::Admission => 2,
Phase::Metadata => 5,
Phase::Details => 15,
Phase::Planning => 30,
Phase::Downloading => 40,
Phase::Composing => 55,
Phase::Finalizing => 70,
Phase::Validating => 80,
Phase::Staging => 90,
Phase::Reconciling => 95,
Phase::Finished => 100,
}
}
pub struct OperationEvents {
id: String,
journal: Arc<Mutex<Journal>>,
@ -32,7 +51,7 @@ impl Events for OperationEvents {
journal.update(&self.id, |record| {
record.phase = phase;
record.can_cancel = can_cancel;
record.progress = json!({"message": message, "unit": "steps", "completed": 0});
record.progress = json!({"message": message, "unit": "steps", "completed": 0, "percent": phase_percent(phase)});
Ok(())
}).map_err(unavailable)?;
Ok(())

View file

@ -103,7 +103,20 @@ impl Store {
pub fn observe() -> Result<Snapshot, String> {
let lock = OpenOptions::new().create(true).truncate(false).write(true).mode(0o600)
.open("/run/archipelago-compose.lock").map_err(|e| e.to_string())?;
lock.try_lock().map_err(|_| "host engine busy; cached observation remains reconciling")?;
// Mutations hold this same lock; wait through the short ones (a refresh or
// a small package plan) instead of immediately degrading the observation to
// "reconciling", which Discover surfaces as a transient error. Long writers
// (staging, downloads) still fail fast after the short budget.
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
loop {
match lock.try_lock() {
Ok(()) => break,
Err(std::fs::TryLockError::WouldBlock) if std::time::Instant::now() < deadline => {
std::thread::sleep(std::time::Duration::from_millis(50));
}
Err(_) => return Err("host engine busy; cached observation remains reconciling".into()),
}
}
let result = Command::new("timeout").args(["--kill-after=2", "10", "ostree", "admin", "status", "--json"])
.env("LC_ALL", "C").output().map_err(|e| e.to_string())?;
if !result.status.success() || result.stdout.len() > 1024 * 1024 { return Err("local OSTree status unavailable".into()); }