aboutsummaryrefslogtreecommitdiffstats
path: root/crates/configuration/src/state.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/configuration/src/state.rs')
-rw-r--r--crates/configuration/src/state.rs44
1 files changed, 40 insertions, 4 deletions
diff --git a/crates/configuration/src/state.rs b/crates/configuration/src/state.rs
index 7672891..5a51d5b 100644
--- a/crates/configuration/src/state.rs
+++ b/crates/configuration/src/state.rs
@@ -4,13 +4,14 @@ mod routing;
use async_nats::jetstream::Context;
use sqlx::PgPool;
use std::{ops::Deref, sync::Arc};
-use tonic::transport::Endpoint;
-use tracing::error;
-use warden_stack::{Configuration, cache::RedisManager};
+use tracing::{instrument, trace};
+use warden_core::configuration::ReloadEvent;
+use warden_stack::{Configuration, cache::RedisManager, redis::AsyncCommands};
use crate::{
cnfg::LocalConfig,
- server::grpc::interceptor::{Intercepted, MyInterceptor},
+ server::{error::AppError, reload_stream::create_stream},
+ state::cache_key::CacheKey,
};
#[derive(Clone)]
@@ -43,9 +44,44 @@ impl AppState {
) -> Result<AppHandle, AppError> {
let local_config: LocalConfig = serde_json::from_value(configuration.misc.clone())?;
+ create_stream(&services.jetstream, &local_config.nats).await?;
+
Ok(AppHandle(Arc::new(Self {
services,
app_config: local_config,
})))
}
}
+
+#[instrument(skip(state), err(Debug))]
+pub async fn invalidate_cache(state: &AppHandle, key: CacheKey<'_>) -> Result<(), tonic::Status> {
+ trace!("invalidating cache");
+ let mut cache = state
+ .services
+ .cache
+ .get()
+ .await
+ .map_err(|e| tonic::Status::internal(e.to_string()))?;
+
+ cache
+ .del::<_, ()>(key)
+ .await
+ .map_err(|e| tonic::Status::internal(e.to_string()))
+}
+
+#[instrument(skip(state), err(Debug))]
+pub async fn publish_reload(
+ state: &AppHandle,
+ prefix: &str,
+ event: ReloadEvent,
+) -> Result<(), tonic::Status> {
+ trace!("publishing reload event");
+ state
+ .services
+ .jetstream
+ .publish(format!("{prefix}.reload"), event.as_str_name().into())
+ .await
+ .map_err(|e| tonic::Status::internal(e.to_string()))?;
+
+ Ok(())
+}