Skip to main content

qml_rs/core/
server_info.rs

1//! Live server registry entry used by the heartbeat/dead-server-detection
2//! machinery (D1). Each running [`BackgroundJobServer`](crate::processing::BackgroundJobServer)
3//! registers one [`ServerInfo`] row in storage and bumps `last_heartbeat`
4//! on a fixed interval. Peers scan for rows whose `last_heartbeat` is
5//! older than `dead_server_timeout` and reclaim their in-flight jobs.
6
7use chrono::{DateTime, Utc};
8use serde::{Deserialize, Serialize};
9
10/// Registration entry for a live `BackgroundJobServer` instance.
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
12pub struct ServerInfo {
13    /// Globally-unique identifier for this server instance. Format is
14    /// `"<server_name>#<uuid>"` so logs and dashboards can still show the
15    /// user-facing label. This is also written into
16    /// [`JobState::Processing.server_name`](crate::core::JobState::Processing)
17    /// on every claimed job, so reclaim can find them.
18    pub server_id: String,
19    /// User-facing server name from
20    /// [`ServerConfig::server_name`](crate::processing::ServerConfig).
21    /// Not unique on its own.
22    pub server_name: String,
23    /// When this server instance started.
24    pub started_at: DateTime<Utc>,
25    /// Most recent heartbeat timestamp. Peers use this to decide whether
26    /// the server is dead.
27    pub last_heartbeat: DateTime<Utc>,
28    /// Number of worker threads configured on this server.
29    pub worker_count: u32,
30    /// Queues this server is pulling from.
31    pub queues: Vec<String>,
32}
33
34impl ServerInfo {
35    /// Create a fresh `ServerInfo` with `started_at == last_heartbeat == now`.
36    pub fn new(
37        server_id: impl Into<String>,
38        server_name: impl Into<String>,
39        worker_count: u32,
40        queues: Vec<String>,
41    ) -> Self {
42        let now = Utc::now();
43        Self {
44            server_id: server_id.into(),
45            server_name: server_name.into(),
46            started_at: now,
47            last_heartbeat: now,
48            worker_count,
49            queues,
50        }
51    }
52}