use serde_json::{Value, json};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
pub const FORMAT: u64 = 1;
pub const HEARTBEAT: Duration = Duration::from_secs(1);
pub const MAX_BYTES: u64 = 65_536;
pub const WAITING: &str = "waiting for a connection";
pub const EXCHANGE: &str = "in an exchange";
pub const ENDED: &str = "ended";
pub fn now() -> u64 {
SystemTime::now().duration_since(UNIX_EPOCH).map_or(0, |d| d.as_secs())
}
struct Writer {
path: PathBuf,
value: Mutex<Value>,
writing: Mutex<()>,
}
static WRITER: OnceLock<Arc<Writer>> = OnceLock::new();
impl Writer {
fn write(&self) -> std::io::Result<()> {
let _one_at_a_time = self.writing.lock().unwrap_or_else(|p| p.into_inner());
let mut value = self.value.lock().unwrap_or_else(|p| p.into_inner()).clone();
value["written"] = json!(now());
let bytes = serde_json::to_vec_pretty(&value).map_err(std::io::Error::other)?;
let mut name = self.path.file_name().unwrap_or_default().to_os_string();
name.push(format!(".writing-{}", std::process::id()));
let beside = self.path.with_file_name(name);
std::fs::write(&beside, bytes)?;
std::fs::rename(&beside, &self.path).inspect_err(|_| {
let _ = std::fs::remove_file(&beside);
})
}
}
pub fn start(path: &Path, mut first: Value) -> kute::Result<()> {
first["kute_peer_status"] = json!(FORMAT);
first["heartbeat_seconds"] = json!(HEARTBEAT.as_secs());
first["started"] = json!(now());
first["since"] = json!(now());
let writer = Arc::new(Writer { path: path.to_path_buf(), value: Mutex::new(first), writing: Mutex::new(()) });
writer.write().map_err(|e| kute::KuteError::InvalidConfig(format!("the status file {} cannot be written: {e}", path.display())))?;
if WRITER.set(writer.clone()).is_ok() {
std::thread::spawn(move || {
loop {
std::thread::sleep(HEARTBEAT);
let _ = writer.write();
}
});
}
Ok(())
}
pub fn set(change: impl FnOnce(&mut Value)) {
if let Some(writer) = WRITER.get() {
change(&mut writer.value.lock().unwrap_or_else(|p| p.into_inner()));
}
}
pub fn enter(state: &str, change: impl FnOnce(&mut Value)) {
if let Some(writer) = WRITER.get() {
{
let mut value = writer.value.lock().unwrap_or_else(|p| p.into_inner());
value["state"] = json!(state);
value["since"] = json!(now());
change(&mut value);
}
let _ = writer.write();
}
}
pub fn end(result: &kute::Result<()>) {
enter(ENDED, |value| {
value["ended"] = match result {
Ok(()) => json!({ "ok": true }),
Err(e) => json!({ "ok": false, "error": e.to_string() }),
};
});
}
pub fn read(path: &Path) -> Value {
let absent = |why: &str| json!({ "present": false, "why": why, "file": path });
let Ok(metadata) = std::fs::metadata(path) else {
return absent("no status file is there: no peer has been started with --status-file naming it, or the file was removed");
};
if !metadata.is_file() || metadata.len() > MAX_BYTES {
return absent("what is there is not a status file: it is not a plain file of at most 64 KiB");
}
let Some(status) = std::fs::read(path).ok().and_then(|bytes| serde_json::from_slice::<Value>(&bytes).ok()).filter(|v| v["kute_peer_status"] == json!(FORMAT) && v["state"].is_string()) else {
return absent("the file could not be read as a peer status of the kind this version reads");
};
let read_at = now();
let written = status["written"].as_u64().unwrap_or(0);
let age = read_at.saturating_sub(written);
let beat = status["heartbeat_seconds"].as_u64().unwrap_or(1).clamp(1, 60);
let ended = status["state"] == ENDED;
let from_the_future = written > read_at + beat;
let stale = !ended && (age > 3 * beat || from_the_future);
json!({
"present": true,
"file": path,
"read_at": read_at,
"age_seconds": age,
"written_later_than_now": from_the_future,
"stale": stale,
"status": status,
"means": if ended {
"the peer wrote that it had ended. kute peer serve answers one connection and ends; nothing is running."
} else if stale {
"the file says a peer is running, but it has not been written for longer than three of its beats, or says it was written later than now. The peer has stopped without saying so, or cannot write, or the file is not a peer's. Nothing here shows a running peer."
} else {
"a process wrote this within the last few seconds. It is that process's own account; this service did not connect to it."
},
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn two_writers_at_once_leave_a_whole_file_saying_the_last_state() {
let dir = std::env::temp_dir().join(format!("kute-peer-status-writers-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("peer.json");
let writer = Arc::new(Writer { path: path.clone(), value: Mutex::new(json!({ "kute_peer_status": FORMAT, "state": WAITING, "pad": "x".repeat(40_000) })), writing: Mutex::new(()) });
writer.write().unwrap();
let stop = Arc::new(std::sync::atomic::AtomicBool::new(false));
let beats: Vec<_> = (0..3)
.map(|_| {
let (writer, stop) = (writer.clone(), stop.clone());
std::thread::spawn(move || {
while !stop.load(std::sync::atomic::Ordering::Relaxed) {
let _ = writer.write();
}
})
})
.collect();
let reader = {
let (path, stop) = (path.clone(), stop.clone());
std::thread::spawn(move || {
let (mut whole, mut broken) = (0u64, 0u64);
while !stop.load(std::sync::atomic::Ordering::Relaxed) {
if let Ok(bytes) = std::fs::read(&path) {
if serde_json::from_slice::<Value>(&bytes).is_ok() { whole += 1 } else { broken += 1 }
}
}
(whole, broken)
})
};
let mut lost = 0;
let mut failed: Vec<String> = Vec::new();
for round in 0..60 {
let state = format!("state {round}");
writer.value.lock().unwrap()["state"] = json!(state);
if let Err(e) = writer.write() { failed.push(e.to_string()); }
std::thread::sleep(Duration::from_millis(1));
stop.store(round == 59, std::sync::atomic::Ordering::Relaxed);
}
for beat in beats {
beat.join().unwrap();
}
let (whole, broken) = reader.join().unwrap();
let last: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
if last["state"] != json!("state 59") {
lost += 1;
}
let left: Vec<_> = std::fs::read_dir(&dir).unwrap().map(|e| e.unwrap().file_name()).filter(|n| n.to_string_lossy().contains("writing")).collect();
let _ = std::fs::remove_dir_all(&dir);
assert!(whole > 0);
assert_eq!((broken, lost, left.len(), failed.len()), (0, 0, 0, 0), "state writes that failed: {} (first: {:?}); reads that were not a whole status: {broken} of {}; last state lost: {lost}; temp files left: {left:?}", failed.len(), failed.first(), whole + broken);
}
}