Skip to main content

schedr_master/
problems.rs

1//! A master's problems now (decision 129): what its heartbeat writes to its row, shown on its
2//! node on the Deployments page. Each kind of problem is one entry, set while it lasts and
3//! cleared once it is over.
4
5use std::collections::BTreeMap;
6use std::sync::{Arc, Mutex, PoisonError};
7
8use schedr_db::nodes::NodeProblem;
9
10#[cfg(test)]
11mod tests;
12
13/// The kinds of problem a master reports, each at most once.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
15pub enum ProblemKind {
16    /// Its lease isn't renewed in time: it pauses until it is.
17    Lease,
18    /// A round failed.
19    Round,
20    /// It can't listen for new commands: it polls.
21    Notifications,
22}
23
24/// A master's problems now, shared by its parts (cloned, the same list).
25#[derive(Debug, Clone, Default)]
26pub struct Problems(Arc<Mutex<BTreeMap<ProblemKind, NodeProblem>>>);
27
28impl Problems {
29    /// `kind` is a problem now: an error or a warning, saying `message`.
30    pub fn set(&self, kind: ProblemKind, error: bool, message: impl Into<String>) {
31        self.map().insert(
32            kind,
33            NodeProblem {
34                error,
35                message: message.into(),
36            },
37        );
38    }
39
40    /// `kind` is over.
41    pub fn clear(&self, kind: ProblemKind) {
42        self.map().remove(&kind);
43    }
44
45    /// The problems now, in a fixed order.
46    #[must_use]
47    pub fn now(&self) -> Vec<NodeProblem> {
48        self.map().values().cloned().collect()
49    }
50
51    fn map(&self) -> std::sync::MutexGuard<'_, BTreeMap<ProblemKind, NodeProblem>> {
52        self.0.lock().unwrap_or_else(PoisonError::into_inner)
53    }
54}