cat_gateway/service/utilities/health/
start.rs

1//! Utilities used for system `Started` functionality.
2
3use std::sync::atomic::{
4    AtomicBool,
5    Ordering::{Acquire, Relaxed, Release},
6};
7
8use tracing::{debug, info};
9
10/// Flag to determine if the service has started
11static STARTED: AtomicBool = AtomicBool::new(false);
12
13/// Flag to determine if the Indexing DB has started.
14static LIVE_INDEX_DB: AtomicBool = AtomicBool::new(false);
15
16/// Flag to determine if the Event DB has started.
17static LIVE_EVENT_DB: AtomicBool = AtomicBool::new(false);
18
19/// Flag to determine if the chain follower has synchronized with the IMMUTABLE tip for
20/// the first time.
21static INITIAL_IMMUTABLE_FOLLOWER_TIP_REACHED: AtomicBool = AtomicBool::new(false);
22
23/// Flag to determine if the chain follower has synchronized with the LIVE tip for the
24/// first time.
25static INITIAL_LIVE_FOLLOWER_TIP_REACHED: AtomicBool = AtomicBool::new(false);
26
27/// Returns whether the service has started or not.
28pub(crate) fn service_has_started() -> bool {
29    STARTED.load(Acquire)
30}
31
32/// Set the `STARTED` flag to `true`
33pub(crate) fn set_to_started() {
34    STARTED.store(true, Release);
35}
36
37/// Returns whether the service has started or not.
38pub(crate) fn condition_for_started() -> bool {
39    let event_db_is_live = event_db_is_live();
40    let index_db_is_live = index_db_is_live();
41    let follower_is_live =
42        live_follower_has_first_reached_tip() && immutable_follower_has_first_reached_tip();
43    debug!(
44        event_db_is_live,
45        index_db_is_live, follower_is_live, "Checking if service has started."
46    );
47    event_db_is_live && index_db_is_live && follower_is_live
48}
49
50/// Returns whether the Event DB is live or not.
51///
52/// Gets the stored value from `LIVE_EVENT_DB` flag.
53pub(crate) fn event_db_is_live() -> bool {
54    LIVE_EVENT_DB.load(Acquire)
55}
56
57/// Set the `LIVE_EVENT_DB` flag.
58pub(crate) fn set_event_db_liveness(flag: bool) {
59    LIVE_EVENT_DB.store(flag, Release);
60}
61
62/// Returns whether the Index DB is live or not.
63///
64/// Gets the stored value from `LIVE_INDEX_DB` flag.
65pub(crate) fn index_db_is_live() -> bool {
66    LIVE_INDEX_DB.load(Acquire)
67}
68
69/// Set the `LIVE_INDEX_DB` flag.
70pub(crate) fn set_index_db_liveness(flag: bool) {
71    LIVE_INDEX_DB.store(flag, Release);
72}
73
74/// Returns whether the Chain Follower has reached Immutable Tip or not.
75///
76/// Gets the stored value from `INITIAL_IMMUTABLE_FOLLOWER_TIP_REACHED` flag.
77pub(crate) fn immutable_follower_has_first_reached_tip() -> bool {
78    INITIAL_IMMUTABLE_FOLLOWER_TIP_REACHED.load(Acquire)
79}
80
81/// Returns whether the Chain Follower has reached Live Tip or not.
82///
83/// Gets the stored value from `INITIAL_LIVE_FOLLOWER_TIP_REACHED` flag.
84pub(crate) fn live_follower_has_first_reached_tip() -> bool {
85    INITIAL_LIVE_FOLLOWER_TIP_REACHED.load(Acquire)
86}
87
88/// Set the `INITIAL_IMMUTABLE_FOLLOWER_TIP_REACHED` as `true`.
89/// returns the previous `INITIAL_IMMUTABLE_FOLLOWER_TIP_REACHED` value.
90///
91/// This value can not be set to `false` afterwards.
92pub(crate) fn set_follower_immutable_first_reached_tip() -> bool {
93    let prev_value = INITIAL_IMMUTABLE_FOLLOWER_TIP_REACHED.swap(true, Relaxed);
94    if !prev_value {
95        info!("Follower has reached IMMUTABLE TIP for the first time");
96    }
97    prev_value
98}
99
100/// Set the `INITIAL_LIVE_FOLLOWER_TIP_REACHED` as `true`,
101/// returns the previous `INITIAL_LIVE_FOLLOWER_TIP_REACHED` value.
102///
103/// This value can not be set to `false` afterwards.
104pub(crate) fn set_follower_live_first_reached_tip() -> bool {
105    let prev_value = INITIAL_LIVE_FOLLOWER_TIP_REACHED.swap(true, Relaxed);
106    if !prev_value {
107        info!("Follower has reached LIVE TIP for the first time");
108    }
109    prev_value
110}