60 lines
1.6 KiB
Rust
60 lines
1.6 KiB
Rust
use std::time::Duration;
|
|
|
|
use async_nats::{Client, ConnectOptions};
|
|
use tracing::Level;
|
|
|
|
mod health;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> anyhow::Result<()> {
|
|
let subscriber = tracing_subscriber::fmt()
|
|
.with_max_level(Level::TRACE)
|
|
.finish();
|
|
|
|
tracing::subscriber::set_global_default(subscriber)
|
|
.expect("to set a tracing global subscriber");
|
|
|
|
let client = ConnectOptions::new()
|
|
.name("Prymn Agent demo_agent")
|
|
.custom_inbox_prefix("_INBOX_demo_agent")
|
|
.user_and_password("demo_agent".to_owned(), "demo_agent_password".to_owned())
|
|
.connect("localhost")
|
|
.await
|
|
.map_err(|err| err)?;
|
|
|
|
tracing::info!("connected to nats server");
|
|
|
|
init_health_subsystem(client);
|
|
|
|
// wait_for_commands(client).await;
|
|
|
|
tokio::signal::ctrl_c().await?;
|
|
Ok(())
|
|
}
|
|
|
|
fn init_health_subsystem(client: Client) {
|
|
let system = health::System::new();
|
|
let health_monitor = health::HealthMonitor::new();
|
|
let health_monitor_clone = health_monitor.clone();
|
|
|
|
std::thread::spawn(move || loop {
|
|
const REFRESH_INTERVAL: Duration = Duration::from_secs(1);
|
|
health_monitor.check_system(&system);
|
|
std::thread::sleep(REFRESH_INTERVAL);
|
|
});
|
|
|
|
tokio::spawn(async move {
|
|
let mut recv = health_monitor_clone.monitor();
|
|
|
|
while let Ok(()) = recv.changed().await {
|
|
let payload = serde_json::to_string(&*recv.borrow()).unwrap();
|
|
|
|
tracing::info!(%payload, "sending health event");
|
|
|
|
client
|
|
.publish("agents.v1.demo_agent.health", payload.into())
|
|
.await
|
|
.unwrap();
|
|
}
|
|
});
|
|
}
|