mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-10 07:06:53 +00:00
Reconstructing Notify module
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
## ===== global configuration =====
|
||||
#NOTIFIER__STORE_PATH=/var/log/event-notification
|
||||
#NOTIFIER__CHANNEL_CAPACITY=5000
|
||||
#
|
||||
## ===== adapter configuration array format =====
|
||||
## webhook adapter index 0
|
||||
#NOTIFIER__ADAPTERS_0__type=Webhook
|
||||
#NOTIFIER__ADAPTERS_0__endpoint=http://127.0.0.1:3020/webhook
|
||||
#NOTIFIER__ADAPTERS_0__auth_token=your-auth-token
|
||||
#NOTIFIER__ADAPTERS_0__max_retries=3
|
||||
#NOTIFIER__ADAPTERS_0__timeout=50
|
||||
#NOTIFIER__ADAPTERS_0__custom_headers__x_custom_server=server-value
|
||||
#NOTIFIER__ADAPTERS_0__custom_headers__x_custom_client=client-value
|
||||
#
|
||||
## kafka adapter index 1
|
||||
#NOTIFIER__ADAPTERS_1__type=Kafka
|
||||
#NOTIFIER__ADAPTERS_1__brokers=localhost:9092
|
||||
#NOTIFIER__ADAPTERS_1__topic=notifications
|
||||
#NOTIFIER__ADAPTERS_1__max_retries=3
|
||||
#NOTIFIER__ADAPTERS_1__timeout=60
|
||||
#
|
||||
## mqtt adapter index 2
|
||||
#NOTIFIER__ADAPTERS_2__type=Mqtt
|
||||
#NOTIFIER__ADAPTERS_2__broker=mqtt.example.com
|
||||
#NOTIFIER__ADAPTERS_2__port=1883
|
||||
#NOTIFIER__ADAPTERS_2__client_id=event-notifier
|
||||
#NOTIFIER__ADAPTERS_2__topic=events
|
||||
#NOTIFIER__ADAPTERS_2__max_retries=3
|
||||
@@ -1,28 +0,0 @@
|
||||
## ===== 全局配置 =====
|
||||
#NOTIFIER__STORE_PATH=/var/log/event-notification
|
||||
#NOTIFIER__CHANNEL_CAPACITY=5000
|
||||
#
|
||||
## ===== 适配器配置(数组格式) =====
|
||||
## Webhook 适配器(索引 0)
|
||||
#NOTIFIER__ADAPTERS_0__type=Webhook
|
||||
#NOTIFIER__ADAPTERS_0__endpoint=http://127.0.0.1:3020/webhook
|
||||
#NOTIFIER__ADAPTERS_0__auth_token=your-auth-token
|
||||
#NOTIFIER__ADAPTERS_0__max_retries=3
|
||||
#NOTIFIER__ADAPTERS_0__timeout=50
|
||||
#NOTIFIER__ADAPTERS_0__custom_headers__x_custom_server=value
|
||||
#NOTIFIER__ADAPTERS_0__custom_headers__x_custom_client=value
|
||||
#
|
||||
## Kafka 适配器(索引 1)
|
||||
#NOTIFIER__ADAPTERS_1__type=Kafka
|
||||
#NOTIFIER__ADAPTERS_1__brokers=localhost:9092
|
||||
#NOTIFIER__ADAPTERS_1__topic=notifications
|
||||
#NOTIFIER__ADAPTERS_1__max_retries=3
|
||||
#NOTIFIER__ADAPTERS_1__timeout=60
|
||||
#
|
||||
## MQTT 适配器(索引 2)
|
||||
#NOTIFIER__ADAPTERS_2__type=Mqtt
|
||||
#NOTIFIER__ADAPTERS_2__broker=mqtt.example.com
|
||||
#NOTIFIER__ADAPTERS_2__port=1883
|
||||
#NOTIFIER__ADAPTERS_2__client_id=event-notifier
|
||||
#NOTIFIER__ADAPTERS_2__topic=events
|
||||
#NOTIFIER__ADAPTERS_2__max_retries=3
|
||||
@@ -1,29 +0,0 @@
|
||||
# config.toml
|
||||
store_path = "/var/log/event-notifier"
|
||||
channel_capacity = 5000
|
||||
|
||||
[[adapters]]
|
||||
type = "Webhook"
|
||||
endpoint = "http://127.0.0.1:3020/webhook"
|
||||
auth_token = "your-auth-token"
|
||||
max_retries = 3
|
||||
timeout = 50
|
||||
|
||||
[adapters.custom_headers]
|
||||
custom_server = "value_server"
|
||||
custom_client = "value_client"
|
||||
|
||||
[[adapters]]
|
||||
type = "Kafka"
|
||||
brokers = "localhost:9092"
|
||||
topic = "notifications"
|
||||
max_retries = 3
|
||||
timeout = 60
|
||||
|
||||
[[adapters]]
|
||||
type = "Mqtt"
|
||||
broker = "mqtt.example.com"
|
||||
port = 1883
|
||||
client_id = "event-notifier"
|
||||
topic = "events"
|
||||
max_retries = 3
|
||||
@@ -0,0 +1,109 @@
|
||||
use notify::arn::TargetID;
|
||||
use notify::global::notification_system;
|
||||
use notify::{
|
||||
init_logger, BucketNotificationConfig, Event, EventName, LogLevel, NotificationError,
|
||||
};
|
||||
use std::time::Duration;
|
||||
use tracing::info;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), NotificationError> {
|
||||
init_logger(LogLevel::Debug);
|
||||
|
||||
let system = notification_system();
|
||||
|
||||
// --- 初始配置 (Webhook 和 MQTT) ---
|
||||
let mut config = notify::Config::new();
|
||||
|
||||
// Webhook target configuration
|
||||
let mut webhook_kvs = notify::KVS::new();
|
||||
webhook_kvs.set("enable", "on");
|
||||
webhook_kvs.set("endpoint", "http://127.0.0.1:3020/webhook");
|
||||
webhook_kvs.set("auth_token", "secret-token");
|
||||
// webhook_kvs.set("queue_dir", "/tmp/data/webhook");
|
||||
webhook_kvs.set(
|
||||
"queue_dir",
|
||||
"/Users/qun/Documents/rust/rustfs/notify/logs/webhook",
|
||||
);
|
||||
webhook_kvs.set("queue_limit", "10000");
|
||||
let mut webhook_targets = std::collections::HashMap::new();
|
||||
webhook_targets.insert("1".to_string(), webhook_kvs);
|
||||
config.insert("notify_webhook".to_string(), webhook_targets);
|
||||
|
||||
// MQTT target configuration
|
||||
let mut mqtt_kvs = notify::KVS::new();
|
||||
mqtt_kvs.set("enable", "on");
|
||||
mqtt_kvs.set("broker", "mqtt://localhost:1883");
|
||||
mqtt_kvs.set("topic", "rustfs/events");
|
||||
mqtt_kvs.set("qos", "1"); // AtLeastOnce
|
||||
mqtt_kvs.set("username", "test");
|
||||
mqtt_kvs.set("password", "123456");
|
||||
// webhook_kvs.set("queue_dir", "/tmp/data/mqtt");
|
||||
mqtt_kvs.set(
|
||||
"queue_dir",
|
||||
"/Users/qun/Documents/rust/rustfs/notify/logs/mqtt",
|
||||
);
|
||||
mqtt_kvs.set("queue_limit", "10000");
|
||||
|
||||
let mut mqtt_targets = std::collections::HashMap::new();
|
||||
mqtt_targets.insert("1".to_string(), mqtt_kvs);
|
||||
config.insert("notify_mqtt".to_string(), mqtt_targets);
|
||||
|
||||
// 加载配置并初始化系统
|
||||
*system.config.write().await = config;
|
||||
system.init().await?;
|
||||
info!("✅ System initialized with Webhook and MQTT targets.");
|
||||
|
||||
// --- 1. 查询当前活动的 Target ---
|
||||
let active_targets = system.get_active_targets().await;
|
||||
info!("\n---> Currently active targets: {:?}", active_targets);
|
||||
assert_eq!(active_targets.len(), 2);
|
||||
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
|
||||
// --- 2. 精确删除一个 Target (例如 MQTT) ---
|
||||
info!("\n---> Removing MQTT target...");
|
||||
let mqtt_target_id = TargetID::new("1".to_string(), "mqtt".to_string());
|
||||
system.remove_target(&mqtt_target_id, "notify_mqtt").await?;
|
||||
info!("✅ MQTT target removed.");
|
||||
|
||||
// --- 3. 再次查询活动的 Target ---
|
||||
let active_targets_after_removal = system.get_active_targets().await;
|
||||
info!(
|
||||
"\n---> Active targets after removal: {:?}",
|
||||
active_targets_after_removal
|
||||
);
|
||||
assert_eq!(active_targets_after_removal.len(), 1);
|
||||
assert_eq!(active_targets_after_removal[0].id, "1".to_string());
|
||||
|
||||
// --- 4. 发送事件进行验证 ---
|
||||
// 配置一个规则,指向 Webhook 和已删除的 MQTT
|
||||
let mut bucket_config = BucketNotificationConfig::new("us-east-1");
|
||||
bucket_config.add_rule(
|
||||
&[EventName::ObjectCreatedPut],
|
||||
"*".to_string(),
|
||||
TargetID::new("1".to_string(), "webhook".to_string()),
|
||||
);
|
||||
bucket_config.add_rule(
|
||||
&[EventName::ObjectCreatedPut],
|
||||
"*".to_string(),
|
||||
TargetID::new("1".to_string(), "mqtt".to_string()), // 这个规则会匹配,但找不到 Target
|
||||
);
|
||||
system
|
||||
.load_bucket_notification_config("my-bucket", &bucket_config)
|
||||
.await?;
|
||||
|
||||
info!("\n---> Sending an event...");
|
||||
let event = Event::new_test_event("my-bucket", "document.pdf", EventName::ObjectCreatedPut);
|
||||
system
|
||||
.send_event("my-bucket", "s3:ObjectCreated:Put", "document.pdf", event)
|
||||
.await;
|
||||
info!(
|
||||
"✅ Event sent. Only the Webhook target should receive it. Check logs for warnings about the missing MQTT target."
|
||||
);
|
||||
|
||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||
|
||||
info!("\nDemo completed successfully");
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
use notify::arn::TargetID;
|
||||
use notify::global::notification_system;
|
||||
// 1. 使用全局访问器
|
||||
use notify::{
|
||||
init_logger, BucketNotificationConfig, Event, EventName, LogLevel, NotificationError, KVS,
|
||||
};
|
||||
use std::time::Duration;
|
||||
use tracing::info;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), NotificationError> {
|
||||
init_logger(LogLevel::Debug);
|
||||
|
||||
// 获取全局 NotificationSystem 实例
|
||||
let system = notification_system();
|
||||
|
||||
// --- 初始配置 ---
|
||||
let mut config = notify::Config::new();
|
||||
// Webhook target
|
||||
let mut webhook_kvs = KVS::new();
|
||||
webhook_kvs.set("enable", "on");
|
||||
webhook_kvs.set("endpoint", "http://127.0.0.1:3020/webhook");
|
||||
// webhook_kvs.set("queue_dir", "./logs/webhook");
|
||||
webhook_kvs.set(
|
||||
"queue_dir",
|
||||
"/Users/qun/Documents/rust/rustfs/notify/logs/webhook",
|
||||
);
|
||||
let mut webhook_targets = std::collections::HashMap::new();
|
||||
webhook_targets.insert("1".to_string(), webhook_kvs);
|
||||
config.insert("notify_webhook".to_string(), webhook_targets);
|
||||
|
||||
// 加载初始配置并初始化系统
|
||||
*system.config.write().await = config;
|
||||
system.init().await?;
|
||||
info!("✅ System initialized with Webhook target.");
|
||||
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
|
||||
// --- 2. 动态更新系统配置:添加一个 MQTT Target ---
|
||||
info!("\n---> Dynamically adding MQTT target...");
|
||||
let mut mqtt_kvs = KVS::new();
|
||||
mqtt_kvs.set("enable", "on");
|
||||
mqtt_kvs.set("broker", "mqtt://localhost:1883");
|
||||
mqtt_kvs.set("topic", "rustfs/events");
|
||||
mqtt_kvs.set("qos", "1");
|
||||
mqtt_kvs.set("username", "test");
|
||||
mqtt_kvs.set("password", "123456");
|
||||
mqtt_kvs.set("queue_limit", "10000");
|
||||
// mqtt_kvs.set("queue_dir", "./logs/mqtt");
|
||||
mqtt_kvs.set(
|
||||
"queue_dir",
|
||||
"/Users/qun/Documents/rust/rustfs/notify/logs/mqtt",
|
||||
);
|
||||
system
|
||||
.set_target_config("notify_mqtt", "1", mqtt_kvs)
|
||||
.await?;
|
||||
info!("✅ MQTT target added and system reloaded.");
|
||||
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
|
||||
// --- 3. 加载和管理 Bucket 配置 ---
|
||||
info!("\n---> Loading bucket notification config...");
|
||||
let mut bucket_config = BucketNotificationConfig::new("us-east-1");
|
||||
bucket_config.add_rule(
|
||||
&[EventName::ObjectCreatedPut],
|
||||
"*".to_string(),
|
||||
TargetID::new("1".to_string(), "webhook".to_string()),
|
||||
);
|
||||
bucket_config.add_rule(
|
||||
&[EventName::ObjectCreatedPut],
|
||||
"*".to_string(),
|
||||
TargetID::new("1".to_string(), "mqtt".to_string()),
|
||||
);
|
||||
system
|
||||
.load_bucket_notification_config("my-bucket", &bucket_config)
|
||||
.await?;
|
||||
info!("✅ Bucket 'my-bucket' config loaded.");
|
||||
|
||||
// --- 发送事件 ---
|
||||
info!("\n---> Sending an event...");
|
||||
let event = Event::new_test_event("my-bucket", "document.pdf", EventName::ObjectCreatedPut);
|
||||
system
|
||||
.send_event("my-bucket", "s3:ObjectCreated:Put", "document.pdf", event)
|
||||
.await;
|
||||
info!("✅ Event sent. Both Webhook and MQTT targets should receive it.");
|
||||
|
||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||
|
||||
// --- 动态移除配置 ---
|
||||
info!("\n---> Dynamically removing Webhook target...");
|
||||
system.remove_target_config("notify_webhook", "1").await?;
|
||||
info!("✅ Webhook target removed and system reloaded.");
|
||||
|
||||
info!("\n---> Removing bucket notification config...");
|
||||
system.remove_bucket_notification_config("my-bucket").await;
|
||||
info!("✅ Bucket 'my-bucket' config removed.");
|
||||
|
||||
info!("\nDemo completed successfully");
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,17 +1,53 @@
|
||||
use axum::routing::get;
|
||||
use axum::{extract::Json, http::StatusCode, routing::post, Router};
|
||||
use axum::{
|
||||
extract::Json,
|
||||
http::{HeaderMap, Response, StatusCode},
|
||||
routing::post,
|
||||
Router,
|
||||
};
|
||||
use serde_json::Value;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use axum::extract::Query;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ResetParams {
|
||||
reason: Option<String>,
|
||||
}
|
||||
|
||||
// 定义一个全局变量 统计接受到数据条数
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
static WEBHOOK_COUNT: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// 构建应用
|
||||
let app = Router::new()
|
||||
.route("/webhook", post(receive_webhook))
|
||||
.route(
|
||||
"/webhook/reset/{reason}",
|
||||
get(reset_webhook_count_with_path),
|
||||
)
|
||||
.route("/webhook/reset", get(reset_webhook_count))
|
||||
.route("/webhook", get(receive_webhook));
|
||||
// 启动服务器
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3020").await.unwrap();
|
||||
println!("Server running on http://0.0.0.0:3020");
|
||||
let addr = "0.0.0.0:3020";
|
||||
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
||||
println!("Server running on {}", addr);
|
||||
|
||||
// 服务启动后进行自检
|
||||
tokio::spawn(async move {
|
||||
// 给服务器一点时间启动
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
|
||||
match is_service_active(addr).await {
|
||||
Ok(true) => println!("服务健康检查:成功 - 服务正常运行"),
|
||||
Ok(false) => eprintln!("服务健康检查:失败 - 服务未响应"),
|
||||
Err(e) => eprintln!("服务健康检查错误:{}", e),
|
||||
}
|
||||
});
|
||||
|
||||
// 创建关闭信号处理
|
||||
tokio::select! {
|
||||
@@ -26,9 +62,93 @@ async fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
/// 创建一个方法重置 WEBHOOK_COUNT 的值
|
||||
async fn reset_webhook_count_with_path(
|
||||
axum::extract::Path(reason): axum::extract::Path<String>,
|
||||
) -> Response<String> {
|
||||
// 输出当前计数器的值
|
||||
let current_count = WEBHOOK_COUNT.load(Ordering::SeqCst);
|
||||
println!("Current webhook count: {}", current_count);
|
||||
|
||||
println!("Reset webhook count, reason: {}", reason);
|
||||
// 将计数器重置为 0
|
||||
WEBHOOK_COUNT.store(0, Ordering::SeqCst);
|
||||
println!("Webhook count has been reset to 0.");
|
||||
|
||||
Response::builder()
|
||||
.header("Foo", "Bar")
|
||||
.status(StatusCode::OK)
|
||||
.body(format!(
|
||||
"Webhook count reset successfully. Previous count: {}. Reason: {}",
|
||||
current_count, reason
|
||||
))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// 创建一个方法重置 WEBHOOK_COUNT 的值
|
||||
/// 可以通过调用此方法来重置计数器
|
||||
async fn reset_webhook_count(
|
||||
Query(params): Query<ResetParams>,
|
||||
headers: HeaderMap,
|
||||
) -> Response<String> {
|
||||
// 输出当前计数器的值
|
||||
let current_count = WEBHOOK_COUNT.load(Ordering::SeqCst);
|
||||
println!("Current webhook count: {}", current_count);
|
||||
|
||||
let reason = params.reason.unwrap_or_else(|| "未提供原因".to_string());
|
||||
println!("Reset webhook count, reason: {}", reason);
|
||||
|
||||
for header in headers {
|
||||
let (key, value) = header;
|
||||
println!("Header: {:?}: {:?}", key, value);
|
||||
}
|
||||
|
||||
println!("Reset webhook count printed headers");
|
||||
// 将计数器重置为 0
|
||||
WEBHOOK_COUNT.store(0, Ordering::SeqCst);
|
||||
println!("Webhook count has been reset to 0.");
|
||||
Response::builder()
|
||||
.header("Foo", "Bar")
|
||||
.status(StatusCode::OK)
|
||||
.body(format!(
|
||||
"Webhook count reset successfully current_count:{}",
|
||||
current_count
|
||||
))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
async fn is_service_active(addr: &str) -> Result<bool, String> {
|
||||
let socket_addr = tokio::net::lookup_host(addr)
|
||||
.await
|
||||
.map_err(|e| format!("无法解析主机:{}", e))?
|
||||
.next()
|
||||
.ok_or_else(|| "未找到地址".to_string())?;
|
||||
|
||||
println!("正在检查服务状态:{}", socket_addr);
|
||||
|
||||
match tokio::time::timeout(
|
||||
std::time::Duration::from_secs(5),
|
||||
tokio::net::TcpStream::connect(socket_addr),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(Ok(_)) => Ok(true),
|
||||
Ok(Err(e)) => {
|
||||
if e.kind() == std::io::ErrorKind::ConnectionRefused {
|
||||
Ok(false)
|
||||
} else {
|
||||
Err(format!("连接失败:{}", e))
|
||||
}
|
||||
}
|
||||
Err(_) => Err("连接超时".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn receive_webhook(Json(payload): Json<Value>) -> StatusCode {
|
||||
let start = SystemTime::now();
|
||||
let since_the_epoch = start.duration_since(UNIX_EPOCH).expect("Time went backwards");
|
||||
let since_the_epoch = start
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("Time went backwards");
|
||||
|
||||
// get the number of seconds since the unix era
|
||||
let seconds = since_the_epoch.as_secs();
|
||||
@@ -37,12 +157,20 @@ async fn receive_webhook(Json(payload): Json<Value>) -> StatusCode {
|
||||
let (year, month, day, hour, minute, second) = convert_seconds_to_date(seconds);
|
||||
|
||||
// output result
|
||||
println!("current time:{:04}-{:02}-{:02} {:02}:{:02}:{:02}", year, month, day, hour, minute, second);
|
||||
println!(
|
||||
"current time:{:04}-{:02}-{:02} {:02}:{:02}:{:02}",
|
||||
year, month, day, hour, minute, second
|
||||
);
|
||||
println!(
|
||||
"received a webhook request time:{} content:\n {}",
|
||||
seconds,
|
||||
serde_json::to_string_pretty(&payload).unwrap()
|
||||
);
|
||||
WEBHOOK_COUNT.fetch_add(1, Ordering::SeqCst);
|
||||
println!(
|
||||
"Total webhook requests received: {}",
|
||||
WEBHOOK_COUNT.load(Ordering::SeqCst)
|
||||
);
|
||||
StatusCode::OK
|
||||
}
|
||||
|
||||
@@ -93,5 +221,12 @@ fn convert_seconds_to_date(seconds: u64) -> (u32, u32, u32, u32, u32, u32) {
|
||||
// calculate the number of seconds
|
||||
second += total_seconds;
|
||||
|
||||
(year as u32, month as u32, day as u32, hour as u32, minute as u32, second as u32)
|
||||
(
|
||||
year as u32,
|
||||
month as u32,
|
||||
day as u32,
|
||||
hour as u32,
|
||||
minute as u32,
|
||||
second as u32,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user