Skip to content

Commit ebb616d

Browse files
author
vsilent
committed
fix: add heartbeat staleness check to agent snapshot endpoint
- If last_heartbeat is older than 5 minutes, override status to 'offline' - If agent never had a heartbeat, return 'offline' instead of raw DB status - Prevents stale 'online' status persisting after agent stops reporting
1 parent f3948da commit ebb616d

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

‎src/routes/agent/snapshot.rs‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,26 @@ pub async fn snapshot_handler(
146146
containers
147147
);
148148

149-
let agent_snapshot = agent.map(|a| AgentSnapshot {
150-
version: a.version,
151-
capabilities: a.capabilities,
152-
system_info: a.system_info,
153-
status: Some(a.status),
154-
last_heartbeat: a.last_heartbeat,
149+
// Derive effective status: if heartbeat is stale (>5 min), override to "offline"
150+
let agent_snapshot = agent.map(|a| {
151+
let effective_status = match a.last_heartbeat {
152+
Some(hb) => {
153+
let stale_threshold = chrono::Duration::seconds(300); // 5 minutes
154+
if chrono::Utc::now() - hb > stale_threshold {
155+
"offline".to_string()
156+
} else {
157+
a.status.clone()
158+
}
159+
}
160+
None => "offline".to_string(), // Never had a heartbeat
161+
};
162+
AgentSnapshot {
163+
version: a.version,
164+
capabilities: a.capabilities,
165+
system_info: a.system_info,
166+
status: Some(effective_status),
167+
last_heartbeat: a.last_heartbeat,
168+
}
155169
});
156170
tracing::debug!("[SNAPSHOT HANDLER] Agent Snapshot : {:?}", agent_snapshot);
157171

0 commit comments

Comments
 (0)