use crate::{
adapter::{self, Adapter},
config::Config,
export::{Channel, Executor, Extender},
plugin,
scheduler::Runtime,
ActPlugin, ChannelOptions, Signal, StoreAdapter,
};
use std::sync::{Arc, Mutex};
use tracing::info;
#[derive(Clone)]
pub struct Engine {
runtime: Arc<Runtime>,
adapter: Arc<Adapter>,
extender: Arc<Extender>,
}
impl Default for Engine {
fn default() -> Self {
Self::new()
}
}
impl Engine {
pub fn new() -> Self {
Self::new_with_config(&Config::default(), None)
}
pub fn config(&self) -> Arc<Config> {
self.runtime.config().clone()
}
pub fn adapter(&self) -> Arc<Adapter> {
self.adapter.clone()
}
pub fn executor(&self) -> Arc<Executor> {
Arc::new(Executor::new(&self.runtime))
}
pub fn channel(&self) -> Arc<Channel> {
Arc::new(Channel::new(&self.runtime))
}
pub fn channel_with_options(&self, matcher: &ChannelOptions) -> Arc<Channel> {
Arc::new(Channel::channel(&self.runtime, matcher))
}
pub fn extender(&self) -> Arc<Extender> {
self.extender.clone()
}
pub(crate) fn runtime(&self) -> Arc<Runtime> {
self.runtime.clone()
}
pub(crate) fn plugins(&self) -> Arc<Mutex<Vec<Box<dyn ActPlugin>>>> {
self.extender.plugins()
}
pub fn close(&self) {
info!("close");
self.runtime.scher().close();
}
pub fn signal<T: Clone>(&self, init: T) -> Signal<T> {
Signal::new(init)
}
pub fn is_running(&self) -> bool {
self.runtime.is_running()
}
fn init(&self) {
info!("init");
plugin::init(self);
adapter::init(self);
self.runtime.init(self);
}
pub(crate) fn new_with_config(config: &Config, store: Option<Arc<dyn StoreAdapter>>) -> Self {
info!("config: {:?}", config);
let runtime = Runtime::new(config);
let extender = Arc::new(Extender::new(&runtime));
let adapter = Arc::new(Adapter::new());
if let Some(store) = &store {
adapter.set_store(store.clone());
}
let engine = Self {
runtime,
extender,
adapter,
};
engine.init();
engine
}
}