Skip to content

Commit 5701c81

Browse files
authored
feat: set State variable for schema version check status (#197)
1 parent cfab339 commit 5701c81

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

catalyst-gateway/bin/src/service/api/health/ready_get.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
resp_2xx::NoContent,
1414
resp_5xx::{server_error, ServerError, ServiceUnavailable},
1515
},
16-
state::State,
16+
state::{SchemaVersionStatus, State},
1717
};
1818

1919
/// All responses
@@ -47,11 +47,24 @@ pub(crate) type AllResponses = response! {
4747
/// * 500 Server Error - If anything within this function fails unexpectedly. (Possible
4848
/// but unlikely)
4949
/// * 503 Service Unavailable - Service is not ready, do not send other requests.
50-
#[allow(clippy::unused_async)]
5150
pub(crate) async fn endpoint(state: Data<&Arc<State>>) -> AllResponses {
5251
match state.event_db.schema_version_check().await {
53-
Ok(_) => T204(NoContent),
52+
Ok(_) => {
53+
tracing::debug!("DB schema version status ok");
54+
if let Ok(mut g) = state.schema_version_status.lock() {
55+
*g = SchemaVersionStatus::Ok;
56+
}
57+
T204(NoContent)
58+
},
5459
Err(crate::event_db::error::Error::TimedOut) => T503(ServiceUnavailable),
55-
Err(err) => T500(server_error!("{}", err.to_string())),
60+
Err(err) => {
61+
tracing::error!("DB schema version status mismatch");
62+
if let Ok(mut g) = state.schema_version_status.lock() {
63+
*g = SchemaVersionStatus::Mismatch;
64+
T503(ServiceUnavailable)
65+
} else {
66+
T500(server_error!("{}", err.to_string()))
67+
}
68+
},
5669
}
5770
}

catalyst-gateway/bin/src/state/mod.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
//! Shared state used by all endpoints.
2-
use std::sync::Arc;
2+
use std::sync::{Arc, Mutex};
33

44
use crate::{
55
cli::Error,
66
event_db::{establish_connection, queries::EventDbQueries},
77
};
88

9+
/// The status of the expected DB schema version.
10+
pub(crate) enum SchemaVersionStatus {
11+
/// The current DB schema version matches what is expected.
12+
Ok,
13+
/// There is a mismatch between the current DB schema version
14+
/// and what is expected.
15+
Mismatch,
16+
}
17+
918
/// Global State of the service
1019
pub(crate) struct State {
1120
/// This can be None, or a handle to the DB.
1221
/// If the DB fails, it can be set to None.
1322
/// If its None, an attempt to get it will try and connect to the DB.
1423
/// This is Private, it needs to be accessed with a function.
15-
// event_db_handle: Arc<ArcSwap<Option<dyn EventDbQueries>>>, // Private need to get it with a
16-
// function.
24+
// event_db_handle: Arc<ArcSwap<Option<dyn EventDbQueries>>>,
25+
// Private need to get it with a function.
1726
pub(crate) event_db: Arc<dyn EventDbQueries>, /* This needs to be obsoleted, we want the DB
1827
* to be able to be down. */
28+
/// Status of the last DB schema version check.
29+
pub(crate) schema_version_status: Mutex<SchemaVersionStatus>,
1930
}
2031

2132
impl State {
2233
/// Create a new global [`State`]
2334
pub(crate) async fn new(database_url: Option<String>) -> Result<Self, Error> {
24-
// Get a configured pool to the Database.
35+
// Get a configured pool to the Database, runs schema version check internally.
2536
let event_db = Arc::new(establish_connection(database_url, false).await?);
2637

27-
let state = Self { event_db };
38+
let state = Self {
39+
event_db,
40+
// It is safe to assume that the schema version matches if `event_db` doesn't fail,
41+
// due to the interior check ran by `establish_connection`.
42+
schema_version_status: Mutex::new(SchemaVersionStatus::Ok),
43+
};
2844

2945
// We don't care if this succeeds or not.
3046
// We just try our best to connect to the event DB.

0 commit comments

Comments
 (0)