feat: added config update message handling
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
use nxmesh_proto::{AgentMessage, ConfigUpdate, MasterMessage, master_message::Payload};
|
||||
|
||||
@@ -6,27 +6,37 @@ use crate::service::master_handler::{MasterHandlerError, MessageResult};
|
||||
|
||||
#[async_trait::async_trait]
|
||||
pub trait MasterMessageHandler: Send + Sync + 'static {
|
||||
async fn handle_master_message(&self, message: MasterMessage) -> MessageResult<()>;
|
||||
async fn handle_master_message(
|
||||
&self,
|
||||
agent_id: &str,
|
||||
message: MasterMessage,
|
||||
) -> MessageResult<()>;
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
pub trait OnConfigUpdateHandler: Send + Sync + 'static {
|
||||
// Handle the config update message from master, write the config content to files, validate the new config and reload nginx
|
||||
async fn on_config_update(&self, config_info: ConfigUpdate) -> MessageResult<()>;
|
||||
async fn on_config_update(
|
||||
&self,
|
||||
agent_id: &str,
|
||||
timestamp: i64,
|
||||
message_id: &str,
|
||||
config_info: ConfigUpdate,
|
||||
) -> MessageResult<()>;
|
||||
}
|
||||
|
||||
pub struct HandlerImpl<OCH>
|
||||
where
|
||||
OCH: OnConfigUpdateHandler + ?Sized,
|
||||
{
|
||||
on_config_update_handler: Arc<OCH>,
|
||||
on_config_update_handler: Weak<OCH>,
|
||||
}
|
||||
|
||||
impl<OCH> HandlerImpl<OCH>
|
||||
where
|
||||
OCH: OnConfigUpdateHandler + ?Sized,
|
||||
{
|
||||
pub fn new(on_config_update_handler: Arc<OCH>) -> Self {
|
||||
pub fn new(on_config_update_handler: Weak<OCH>) -> Self {
|
||||
Self {
|
||||
on_config_update_handler,
|
||||
}
|
||||
@@ -38,11 +48,26 @@ impl<OCH> MasterMessageHandler for HandlerImpl<OCH>
|
||||
where
|
||||
OCH: OnConfigUpdateHandler + ?Sized,
|
||||
{
|
||||
async fn handle_master_message(&self, message: MasterMessage) -> MessageResult<()> {
|
||||
async fn handle_master_message(
|
||||
&self,
|
||||
agent_id: &str,
|
||||
message: MasterMessage,
|
||||
) -> MessageResult<()> {
|
||||
match message.payload {
|
||||
Some(Payload::ConfigUpdate(config_info)) => {
|
||||
self.on_config_update_handler
|
||||
.on_config_update(config_info)
|
||||
let on_config_update_handler =
|
||||
self.on_config_update_handler.upgrade().ok_or_else(|| {
|
||||
MasterHandlerError::MessageHandlingError(
|
||||
"Failed to upgrade weak reference to config update handler".to_string(),
|
||||
)
|
||||
})?;
|
||||
on_config_update_handler
|
||||
.on_config_update(
|
||||
agent_id,
|
||||
message.timestamp,
|
||||
&message.message_id,
|
||||
config_info,
|
||||
)
|
||||
.await
|
||||
}
|
||||
Some(_) => {
|
||||
|
||||
Reference in New Issue
Block a user