appcore_ops/heartbeat.rs
1// =============================================================================
2// #######
3// ### ### F: heartbeat.rs
4// ## ## ## ## P: AppCore-Runtime
5// ## ##
6// C: 2026/05/31 13:38:42 by dnettoRaw
7// ## ## ## ## U: 2026/07/23 23:50:45 by dnettoRaw
8// ########### S: 1.0.1-rc.8
9// =============================================================================
10
11//! Heartbeat contracts for runtime liveness signals.
12
13use appcore_core::NodeId;
14
15/// Snapshot of node heartbeat.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct Heartbeat {
18 /// Node that emitted the heartbeat.
19 pub node_id: NodeId,
20 /// Emission timestamp in Unix milliseconds.
21 pub timestamp_ms: u64,
22}
23
24/// Contract for components that can produce a heartbeat.
25pub trait HeartbeatSource {
26 /// Produces the latest heartbeat snapshot.
27 fn heartbeat(&self) -> Heartbeat;
28}
29
30/// Static heartbeat source used in local runtime composition.
31#[derive(Debug, Clone, PartialEq, Eq)]
32pub struct StaticHeartbeatSource {
33 heartbeat: Heartbeat,
34}
35
36impl StaticHeartbeatSource {
37 /// Creates a source that always returns the supplied heartbeat.
38 pub fn new(node_id: NodeId, timestamp_ms: u64) -> Self {
39 Self {
40 heartbeat: Heartbeat {
41 node_id,
42 timestamp_ms,
43 },
44 }
45 }
46}
47
48impl HeartbeatSource for StaticHeartbeatSource {
49 fn heartbeat(&self) -> Heartbeat {
50 self.heartbeat.clone()
51 }
52}
53
54#[cfg(test)]
55#[path = "heartbeat_tests.rs"]
56mod tests;