Feature/event: Optimize and Enhance Notify Crate Functionality (#512)

* improve code for notify

* fix

* cargo fmt

* improve code and create `DEFAULT_DELIMITER`

* fix

* fix

* improve code for notify

* fmt

* Update crates/notify/src/registry.rs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update crates/notify/src/factory.rs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix cllipy

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
houseme
2025-06-26 12:24:00 +08:00
committed by GitHub
parent 61e600b9ba
commit 831cb0b6d9
31 changed files with 342 additions and 151 deletions
+46 -6
View File
@@ -5,8 +5,10 @@ use crate::{
target::Target,
};
use ecstore::config::{Config, ENABLE_KEY, ENABLE_OFF, ENABLE_ON, KVS};
use rustfs_config::notify::NOTIFY_ROUTE_PREFIX;
use rustfs_config::{DEFAULT_DELIMITER, ENV_PREFIX};
use std::collections::HashMap;
use tracing::{error, info};
use tracing::{debug, error, info};
/// Registry for managing target factories
pub struct TargetRegistry {
@@ -64,20 +66,58 @@ impl TargetRegistry {
// Iterate through configuration sections
for (section, subsections) in &config.0 {
// Only process notification sections
if !section.starts_with("notify_") {
if !section.starts_with(NOTIFY_ROUTE_PREFIX) {
continue;
}
// Extract target type from section name
let target_type = section.trim_start_matches("notify_");
let target_type = section.trim_start_matches(NOTIFY_ROUTE_PREFIX);
// Iterate through subsections (each representing a target instance)
for (target_id, target_config) in subsections {
// Skip disabled targets
if target_config.lookup(ENABLE_KEY).unwrap_or_else(|| ENABLE_OFF.to_string()) != ENABLE_ON {
let enable_from_config = target_config.lookup(ENABLE_KEY).unwrap_or_else(|| ENABLE_OFF.to_string());
debug!("Target enablement from config: {}/{}: {}", target_type, target_id, enable_from_config);
// Check environment variable for target enablement example: RUSTFS_NOTIFY_WEBHOOK_ENABLE|RUSTFS_NOTIFY_WEBHOOK_ENABLE_[TARGET_ID]
let env_key = if target_id == DEFAULT_DELIMITER {
// If no specific target ID, use the base target type, example: RUSTFS_NOTIFY_WEBHOOK_ENABLE
format!(
"{}{}{}{}{}",
ENV_PREFIX,
NOTIFY_ROUTE_PREFIX,
target_type.to_uppercase(),
DEFAULT_DELIMITER,
ENABLE_KEY
)
} else {
// If specific target ID, append it to the key, example: RUSTFS_NOTIFY_WEBHOOK_ENABLE_[TARGET_ID]
format!(
"{}{}{}{}{}{}{}",
ENV_PREFIX,
NOTIFY_ROUTE_PREFIX,
target_type.to_uppercase(),
DEFAULT_DELIMITER,
ENABLE_KEY,
DEFAULT_DELIMITER,
target_id.to_uppercase()
)
}
.to_uppercase();
debug!("Target env key: {},Target id: {}", env_key, target_id);
let enable_from_env = std::env::var(&env_key)
.map(|v| v.eq_ignore_ascii_case(ENABLE_ON) || v.eq_ignore_ascii_case("true"))
.unwrap_or(false);
debug!("Target env value: {},key: {},Target id: {}", enable_from_env, env_key, target_id);
debug!(
"Target enablement from env: {}/{}: result: {}",
target_type, target_id, enable_from_config
);
if enable_from_config != ENABLE_ON && !enable_from_env {
info!("Skipping disabled target: {}/{}", target_type, target_id);
continue;
}
debug!("create target: {}/{} start", target_type, target_id);
// Create target
match self.create_target(target_type, target_id.clone(), target_config).await {
Ok(target) => {
@@ -85,7 +125,7 @@ impl TargetRegistry {
targets.push(target);
}
Err(e) => {
error!("Failed to create target {}/{}: {}", target_type, target_id, e);
error!("Failed to create target {}/{}: reason: {}", target_type, target_id, e);
}
}
}