pub mod contracts;
pub mod enums;
pub mod error;
pub mod hierarchy;
pub mod persistence;
pub mod runtime;
pub mod task_pool;
#[cfg(feature = "distributed")]
pub mod distributed;
use std::future::Future;
use std::sync::Arc;
pub use crate::contracts::{SupervisedTask, SupervisorEventListener};
pub use crate::enums::TaskConfig;
pub use crate::error::{SupervisorError, SupervisorResult, ValidationError};
pub use crate::persistence::TaskStateStore;
pub use crate::runtime::{SupervisionResult, TaskRuntime, spawn_supervised, spawn_supervised_many};
pub struct Supervisor {
runtime: crate::runtime::TaskRuntime,
}
impl Supervisor {
pub fn new() -> Self {
Self {
runtime: crate::runtime::TaskRuntime::new(),
}
}
#[allow(clippy::should_implement_trait)]
pub fn add<T: SupervisedTask + 'static>(mut self, task: T) -> Self {
self.runtime.register(task);
self
}
pub fn add_many<T: SupervisedTask + 'static>(mut self, tasks: Vec<T>) -> Self {
self.runtime.register_many(tasks);
self
}
pub fn add_boxed(mut self, task: Box<dyn SupervisedTask>) -> Self {
self.runtime.register_boxed(task);
self
}
pub fn add_arc(mut self, task: Arc<dyn SupervisedTask>) -> Self {
self.runtime.register_arc(task);
self
}
pub fn with_global_concurrency_limit(mut self, limit: usize) -> Self {
self.runtime.with_global_concurrency_limit(limit);
self
}
pub fn with_event_channel_capacity(mut self, capacity: usize) -> Self {
self.runtime.with_event_channel_capacity(capacity);
self
}
#[cfg(feature = "foxtive-app")]
pub fn app(mut self, app: std::sync::Arc<foxtive::App>) -> Self {
self.runtime.with_app(app);
self
}
pub fn add_listener(mut self, listener: Arc<dyn SupervisorEventListener>) -> Self {
self.runtime.add_listener(listener);
self
}
pub fn with_state_store(mut self, store: Arc<dyn TaskStateStore>) -> Self {
self.runtime.with_state_store(store);
self
}
pub fn require<F>(mut self, name: &'static str, fut: F) -> Self
where
F: Future<Output = SupervisorResult<()>> + Send + 'static,
{
self.runtime.add_prerequisite(name, Box::pin(fut));
self
}
pub fn require_fn<F, Fut>(mut self, name: &'static str, f: F) -> Self
where
F: FnOnce() -> Fut + Send + 'static,
Fut: Future<Output = SupervisorResult<()>> + Send + 'static,
{
self.runtime.add_prerequisite_fn(name, f);
self
}
pub fn runtime(self) -> crate::runtime::TaskRuntime {
self.runtime
}
pub async fn start(
mut self,
) -> Result<crate::runtime::TaskRuntime, crate::error::SupervisorError> {
self.runtime.start_all().await?;
Ok(self.runtime)
}
pub async fn start_and_wait_any(
mut self,
) -> Result<SupervisionResult, crate::error::SupervisorError> {
self.runtime.start_all().await?;
Ok(self.runtime.wait_any().await)
}
pub async fn start_and_wait_all(
mut self,
) -> Result<Vec<SupervisionResult>, crate::error::SupervisorError> {
self.runtime.start_all().await?;
Ok(self.runtime.wait_all().await)
}
}
impl Default for Supervisor {
fn default() -> Self {
Self::new()
}
}