1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//! The module related to state of the service.

use tokio::sync::watch;

/// The lifecycle state of the service
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum State {
    /// Service is initialized but not started
    NotStarted,
    /// Service is starting up
    Starting,
    /// Service is running as normal
    Started,
    /// Service is shutting down
    Stopping,
    /// Service is stopped
    Stopped,
    /// Service shutdown due to an error (panic)
    StoppedWithError(String),
}

impl State {
    /// is not started
    pub fn not_started(&self) -> bool {
        self == &State::NotStarted
    }

    /// is starting
    pub fn starting(&self) -> bool {
        self == &State::Starting
    }

    /// is started
    pub fn started(&self) -> bool {
        self == &State::Started
    }

    /// is stopped
    pub fn stopped(&self) -> bool {
        matches!(self, State::Stopped | State::StoppedWithError(_))
    }

    /// is stopping
    pub fn stopping(&self) -> bool {
        self == &State::Stopping
    }
}

/// The wrapper around the `watch::Receiver<State>`. It repeats the `Receiver` functionality +
/// a new one.
#[derive(Clone)]
pub struct StateWatcher(watch::Receiver<State>);

#[cfg(feature = "test-helpers")]
impl Default for StateWatcher {
    fn default() -> Self {
        let (_, receiver) = watch::channel(State::NotStarted);
        Self(receiver)
    }
}

#[cfg(feature = "test-helpers")]
impl StateWatcher {
    /// Create a new `StateWatcher` with the `State::Started` state.
    pub fn started() -> Self {
        let (sender, receiver) = watch::channel(State::Started);
        // This function is used only in tests, so for simplicity of the tests, we want to leak sender.
        core::mem::forget(sender);
        Self(receiver)
    }
}

impl StateWatcher {
    /// See [`watch::Receiver::borrow`].
    pub fn borrow(&self) -> watch::Ref<'_, State> {
        self.0.borrow()
    }

    /// See [`watch::Receiver::borrow_and_update`].
    pub fn borrow_and_update(&mut self) -> watch::Ref<'_, State> {
        self.0.borrow_and_update()
    }

    /// See [`watch::Receiver::has_changed`].
    pub fn has_changed(&self) -> Result<bool, watch::error::RecvError> {
        self.0.has_changed()
    }

    /// See [`watch::Receiver::changed`].
    pub async fn changed(&mut self) -> Result<(), watch::error::RecvError> {
        self.0.changed().await
    }

    /// See [`watch::Receiver::same_channel`].
    pub fn same_channel(&self, other: &Self) -> bool {
        self.0.same_channel(&other.0)
    }
}

impl StateWatcher {
    #[tracing::instrument(level = "debug", skip(self), err, ret)]
    /// Infinity loop while the state is `State::Started`. Returns the next received state.
    pub async fn while_started(&mut self) -> anyhow::Result<State> {
        loop {
            let state = self.borrow().clone();
            if !state.started() {
                return Ok(state);
            }

            self.changed().await?;
        }
    }

    /// Future that resolves once the state is `State::Stopped`.
    pub async fn wait_stopping_or_stopped(&mut self) -> anyhow::Result<()> {
        let state = self.borrow().clone();
        while !(state.stopped() || state.stopping()) {
            self.changed().await?;
        }
        Ok(())
    }
}

impl From<watch::Receiver<State>> for StateWatcher {
    fn from(receiver: watch::Receiver<State>) -> Self {
        Self(receiver)
    }
}