use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::watch;
use crate::service::context::Context;
use crate::service::lifecycle::Lifecycle;
use crate::{definition, env, errors};
#[async_trait::async_trait]
pub trait Service: ServiceClone + Lifecycle {
fn kind(&self) -> definition::ServiceKind;
fn info(&self) -> serde_json::Value;
fn mode(&self) -> ServiceExecutionMode;
fn initialize(
&mut self,
ctx: Arc<Context>,
definitions: Arc<definition::Definitions>,
envs: Arc<env::Env>,
options: HashMap<String, serde_json::Value>,
) -> errors::Result<()>;
async fn run(&mut self, ctx: Arc<Context>, shutdown_rx: watch::Receiver<()>) -> errors::Result<()>;
async fn stop(&self, ctx: Arc<Context>);
}
pub trait ServiceClone {
fn clone_box(&self) -> Box<dyn Service>;
}
impl<T> ServiceClone for T
where
T: 'static + Service + Clone,
{
fn clone_box(&self) -> Box<dyn Service> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn Service> {
fn clone(&self) -> Self {
ServiceClone::clone_box(self.as_ref())
}
}
#[derive(PartialEq)]
pub enum ServiceExecutionMode {
Block,
NonBlock,
}