Skip to content

Commit

Permalink
remove key custodian status and refactor transaction code
Browse files Browse the repository at this point in the history
  • Loading branch information
Chethan-rao committed Jan 2, 2024
1 parent ab5110b commit 0cdfb9d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 60 deletions.
15 changes: 1 addition & 14 deletions crates/api_models/src/health_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,5 @@
pub struct RouterHealthCheckResponse {
pub database: String,
pub redis: String,
pub locker: LockerHealthResponse,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LockerHealthResponse {
pub status: String,
pub key_custodian_status: KeyCustodianStatus,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum KeyCustodianStatus {
Unavailable,
Locked,
Unlocked,
pub locker: String,
}
2 changes: 2 additions & 0 deletions crates/router/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ pub const EMAIL_TOKEN_TIME_IN_SECS: u64 = 60 * 60 * 24; // 1 day
pub const VERIFY_CONNECTOR_ID_PREFIX: &str = "conn_verify";
#[cfg(feature = "olap")]
pub const VERIFY_CONNECTOR_MERCHANT_ID: &str = "test_merchant";

pub const LOCKER_HEALTH_CALL_PATH: &str = "/health";
39 changes: 15 additions & 24 deletions crates/router/src/db/health_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,25 @@ use crate::{
core::errors::{self, CustomResult},
routes,
services::api as services,
types::storage,
};

#[async_trait::async_trait]
pub trait HealthCheckInterface {
async fn health_check_db(
&self,
db: &dyn StorageInterface,
) -> CustomResult<(), errors::HealthCheckDBError>;
async fn health_check_db(&self) -> CustomResult<(), errors::HealthCheckDBError>;
async fn health_check_redis(
&self,
db: &dyn StorageInterface,
) -> CustomResult<(), errors::HealthCheckRedisError>;
async fn health_check_locker(
&self,
state: &routes::AppState,
) -> CustomResult<u16, errors::HealthCheckLockerError>;
) -> CustomResult<(), errors::HealthCheckLockerError>;
}

#[async_trait::async_trait]
impl HealthCheckInterface for Store {
async fn health_check_db(
&self,
db: &dyn StorageInterface,
) -> CustomResult<(), errors::HealthCheckDBError> {
async fn health_check_db(&self) -> CustomResult<(), errors::HealthCheckDBError> {
let conn = connection::pg_connection_write(self)
.await
.change_context(errors::HealthCheckDBError::DBError)?;
Expand All @@ -49,19 +44,19 @@ impl HealthCheckInterface for Store {

logger::debug!("Database read was successful");

db.insert_config(ConfigNew {
let config = ConfigNew {
key: "test_key".to_string(),
config: "test_value".to_string(),
})
.await
.map_err(|err| {
};

config.insert(&conn).await.map_err(|err| {
logger::error!(write_err=?err,"Error while writing to database");
errors::HealthCheckDBError::DBWriteError
})?;

logger::debug!("Database write was successful");

db.delete_config_by_key("test_key").await.map_err(|err| {
storage::Config::delete_by_key(&conn, "test_key").await.map_err(|err| {
logger::error!(delete_err=?err,"Error while deleting element in the database");
errors::HealthCheckDBError::DBDeleteError
})?;
Expand Down Expand Up @@ -111,14 +106,13 @@ impl HealthCheckInterface for Store {
async fn health_check_locker(
&self,
state: &routes::AppState,
) -> CustomResult<u16, errors::HealthCheckLockerError> {
) -> CustomResult<(), errors::HealthCheckLockerError> {
let locker = &state.conf.locker;
let mut status_code = 0;
if !locker.mock_locker {
let mut url = locker.host_rs.to_owned();
url.push_str("/health");
let request = services::Request::new(services::Method::Get, &url);
status_code = services::call_connector_api(state, request)
services::call_connector_api(state, request)
.await
.change_context(errors::HealthCheckLockerError::FailedToCallLocker)?
.map(|resp| resp.status_code)
Expand All @@ -128,16 +122,13 @@ impl HealthCheckInterface for Store {

logger::debug!("Locker call was successful");

Ok(status_code)
Ok(())
}
}

#[async_trait::async_trait]
impl HealthCheckInterface for MockDb {
async fn health_check_db(
&self,
_: &dyn StorageInterface,
) -> CustomResult<(), errors::HealthCheckDBError> {
async fn health_check_db(&self) -> CustomResult<(), errors::HealthCheckDBError> {
Ok(())
}

Expand All @@ -151,7 +142,7 @@ impl HealthCheckInterface for MockDb {
async fn health_check_locker(
&self,
_: &routes::AppState,
) -> CustomResult<u16, errors::HealthCheckLockerError> {
Ok(0)
) -> CustomResult<(), errors::HealthCheckLockerError> {
Ok(())
}
}
5 changes: 2 additions & 3 deletions crates/router/src/db/kafka_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2138,9 +2138,8 @@ impl AuthorizationInterface for KafkaStore {
impl HealthCheckInterface for KafkaStore {
async fn health_check_db(
&self,
db: &dyn StorageInterface,
) -> CustomResult<(), errors::HealthCheckDBError> {
self.diesel_store.health_check_db(db).await
self.diesel_store.health_check_db().await
}

async fn health_check_redis(
Expand All @@ -2153,7 +2152,7 @@ impl HealthCheckInterface for KafkaStore {
async fn health_check_locker(
&self,
state: &routes::AppState,
) -> CustomResult<u16, errors::HealthCheckLockerError> {
) -> CustomResult<(), errors::HealthCheckLockerError> {
self.diesel_store.health_check_locker(state).await
}
}
25 changes: 6 additions & 19 deletions crates/router/src/routes/health.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use actix_web::web;
use api_models::health_check::{
KeyCustodianStatus, LockerHealthResponse, RouterHealthCheckResponse,
};
use api_models::health_check::RouterHealthCheckResponse;
use router_env::{instrument, logger, tracing};

use super::app;
Expand All @@ -25,7 +23,7 @@ pub async fn deep_health_check(state: web::Data<app::AppState>) -> impl actix_we

logger::debug!("Database health check begin");

let db_status = match db.health_check_db(db).await {
let db_status = match db.health_check_db().await {
Ok(_) => "Health is good".to_string(),
Err(err) => {
status_code = 500;
Expand All @@ -48,19 +46,11 @@ pub async fn deep_health_check(state: web::Data<app::AppState>) -> impl actix_we

logger::debug!("Locker health check begin");

let (locker_status, key_custodian_status) = match db.health_check_locker(&state).await {
Ok(status_code) => {
let status_message = "Health is good".to_string();
let key_custodian_status = if status_code == 403 {
KeyCustodianStatus::Locked
} else {
KeyCustodianStatus::Unlocked
};
(status_message, key_custodian_status)
}
let locker_status = match db.health_check_locker(&state).await {
Ok(_) => "Health is good".to_string(),
Err(err) => {
status_code = 500;
(err.to_string(), KeyCustodianStatus::Unavailable)
err.to_string()
}
};

Expand All @@ -69,10 +59,7 @@ pub async fn deep_health_check(state: web::Data<app::AppState>) -> impl actix_we
let response = serde_json::to_string(&RouterHealthCheckResponse {
database: db_status,
redis: redis_status,
locker: LockerHealthResponse {
status: locker_status,
key_custodian_status,
},
locker: locker_status,
})
.unwrap_or_default();

Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/services/api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use router_env::tracing_actix_web::RequestId;
use super::{request::Maskable, Request};
use crate::{
configs::settings::{Locker, Proxy},
consts::LOCKER_HEALTH_CALL_PATH,
core::{
errors::{ApiClientError, CustomResult},
payments,
Expand Down Expand Up @@ -119,6 +120,7 @@ pub fn proxy_bypass_urls(locker: &Locker) -> Vec<String> {
format!("{locker_host_rs}/cards/add"),
format!("{locker_host_rs}/cards/retrieve"),
format!("{locker_host_rs}/cards/delete"),
format!("{locker_host_rs}{}", LOCKER_HEALTH_CALL_PATH),
format!("{locker_host}/card/addCard"),
format!("{locker_host}/card/getCard"),
format!("{locker_host}/card/deleteCard"),
Expand Down

0 comments on commit 0cdfb9d

Please sign in to comment.