58 lines
1.6 KiB
Rust
58 lines
1.6 KiB
Rust
use eyre::Result;
|
|
use async_trait::async_trait;
|
|
use tokio::task::spawn_local;
|
|
use std::time::Instant;
|
|
use std::sync::Arc;
|
|
use tokio::sync::oneshot;
|
|
|
|
use crate::endpoint::Endpoint;
|
|
|
|
pub struct RircdState {
|
|
endpoints: Vec<Arc<dyn Endpoint>>,
|
|
//to determine program runtime
|
|
creation_timestamp: Option<Instant>,
|
|
|
|
plainlog: Option<()>,
|
|
sqlite_log: Option<()>,
|
|
postgres_log: Option<()>,
|
|
}
|
|
|
|
impl RircdState {
|
|
pub fn new() -> Result<Self> {
|
|
//TODO impl
|
|
Ok(RircdState {
|
|
endpoints: vec![],
|
|
creation_timestamp: Some(Instant::now()),
|
|
plainlog: None,
|
|
sqlite_log: None,
|
|
postgres_log: None,
|
|
})
|
|
}
|
|
|
|
//TODO impl
|
|
pub fn from_config(config: String) -> Result<Self> {
|
|
Self::new()
|
|
}
|
|
|
|
pub async fn run(mut self) -> Result<()> {
|
|
//try to start each endpoint
|
|
self.endpoints.iter().filter(|ep| ep.is_active()).for_each(|endpoint| {
|
|
let endpoint_name = endpoint.name();
|
|
let (success_status_send,success_status_recv) = oneshot::channel::<Result<()>>();
|
|
tokio::spawn(endpoint.clone().listen(success_status_send));
|
|
//TODO color err msg
|
|
//TODO paralelize and sync at the end
|
|
spawn_local(async move {
|
|
println!("starting {} | {}", endpoint_name,
|
|
match success_status_recv.await {
|
|
Ok(_) => format!("SUCCEEDED |"),
|
|
Err(e) => format!("FAILED| <{}>", e),
|
|
});
|
|
});
|
|
});
|
|
|
|
|
|
Ok(())
|
|
}
|
|
}
|