tbm-git-repo-service/src/reg.rs
2026-01-09 09:46:51 +07:00

44 lines
1.2 KiB
Rust

use std::time::Duration;
use log::{error, info};
use tokio::time::sleep;
use tonic::transport::Channel;
use crate::reg::registry::{ServiceHeartbeat, ServiceInfo, registry_client::RegistryClient};
pub mod registry {
tonic::include_proto!("registry");
}
pub async fn register_service(client: &mut RegistryClient<Channel>, name: &str, url: &str) {
let req = tonic::Request::new(ServiceInfo {
name: name.to_string(),
url: url.to_string(),
healthz: "/healthz".to_string(),
token: std::env::var("REGISTRY_TOKEN").unwrap_or_default(),
});
let resp = client.register(req).await;
match resp {
Ok(r) => info!("{:?}", r),
Err(e) => error!("error register: {:?}", e)
}
}
pub async fn heartbeat_loop(name: String, url: String) {
loop {
// TODO: adjust connection path
if let Ok(mut client) = RegistryClient::connect("http://localhost:50051").await {
let req = tonic::Request::new(ServiceHeartbeat {
name: name.clone(),
url: url.clone(),
});
let _ = client.heartbeat(req).await;
}
sleep(Duration::from_secs(10)).await;
}
}
pub async fn health() -> &'static str {
"git-fetch-service.rs ok"
}