use std::path::{Path, PathBuf};
use chrono::Utc;
use microsandbox_protocol::heartbeat::Heartbeat;
pub struct HeartbeatReader {
path: PathBuf,
}
impl HeartbeatReader {
pub fn new(runtime_dir: &Path) -> Self {
Self {
path: runtime_dir.join("heartbeat.json"),
}
}
pub fn read(&self) -> Option<Heartbeat> {
let content = std::fs::read_to_string(&self.path).ok()?;
serde_json::from_str(&content).ok()
}
pub fn is_idle(&self, timeout_secs: u64) -> bool {
let heartbeat = match self.read() {
Some(hb) => hb,
None => return false,
};
let elapsed = Utc::now()
.signed_duration_since(heartbeat.last_activity)
.num_seconds();
elapsed >= 0 && elapsed as u64 >= timeout_secs
}
}