73 lines
2.2 KiB
Rust
73 lines
2.2 KiB
Rust
// mod to communicate with api-grub and preproc services
|
|
// using Unix-Socket Client
|
|
|
|
use anyhow::{Error, Result};
|
|
use integr_structs::api::ApiConfigV2;
|
|
use tokio::time::{sleep, Duration};
|
|
use tokio::net::UnixStream;
|
|
use std::env;
|
|
use tokio::sync::mpsc::Receiver;
|
|
use log::{info, error};
|
|
use serde_json::to_string_pretty;
|
|
|
|
#[derive(Debug)]
|
|
enum UnixSocketConsumer {
|
|
ApiGrubber,
|
|
Preproc,
|
|
}
|
|
// to create us-client
|
|
struct UnixSocketClient;
|
|
// to catch new configs from integr submod
|
|
type ConfigGateway = Receiver<ApiConfigV2>;
|
|
|
|
pub async fn init_delivery_mech(rx: ConfigGateway) -> Result<()> {
|
|
let mut rx = rx;
|
|
loop {
|
|
while rx.is_empty() {
|
|
continue;
|
|
};
|
|
info!("New config was pulled. Redirecting it ...");
|
|
let config = rx.recv().await.unwrap();
|
|
match UnixSocketConsumer::ApiGrubber.get_stream_object().await {
|
|
Ok(socket) => {
|
|
socket.try_write(to_string_pretty(&config)?.as_bytes())?;
|
|
info!("New api config was pulled");
|
|
},
|
|
Err(er) => {
|
|
error!("Cannot coonect to ApiGrubber Unix-Socket due to {er}");
|
|
}
|
|
}
|
|
sleep(Duration::from_secs(3)).await;
|
|
}
|
|
// let api_sock = UnixSocketConsumer::ApiGrubber.get_stream_object().await?;
|
|
// let preproc_sock = UnixSocketConsumer::Preproc.get_stream_object().await?;
|
|
}
|
|
|
|
impl UnixSocketConsumer {
|
|
pub async fn get_stream_object(&self) -> Result<UnixStream> {
|
|
let socket_file = match self {
|
|
UnixSocketConsumer::ApiGrubber => env::var("API_GRUBBER_SOCKET")?,
|
|
UnixSocketConsumer::Preproc => env::var("PREPROC_SOCKET")?,
|
|
};
|
|
UnixStream::connect(socket_file).await.or_else(|_| Err(Error::msg(format!("Cannot create {:?} Unix-Socket client", self))))
|
|
}
|
|
}
|
|
|
|
// async fn check_unix_socket_file(path: &str) -> bool {
|
|
// std::path::Path::new(path).exists()
|
|
// }
|
|
|
|
#[cfg(test)]
|
|
mod delivery_unittests {
|
|
#[allow(unused_imports)]
|
|
use super::*;
|
|
use tokio::test;
|
|
|
|
//
|
|
#[test]
|
|
async fn check_api_unix_socket_client_creation() { assert!(true); }
|
|
|
|
#[test]
|
|
async fn check_preproc_unix_socket_client_creation() { assert!(true); }
|
|
//
|
|
} |