use crate::error::types::SupervisorError;
use crate::id::types::SupervisorPath;
use crate::policy::budget::RestartBudgetConfig;
use crate::policy::failure_window::FailureWindowConfig;
use crate::policy::group::GroupDependencyEdge;
use crate::policy::meltdown::MeltdownPolicy;
use crate::policy::task_role_defaults::{SeverityClass, TaskRole};
use crate::spec::child::{BackoffPolicy, ChildSpec, HealthPolicy, RestartPolicy};
use crate::spec::shutdown::TreeShutdownPolicy;
use crate::spec::supervisor::{
BackpressureConfig, ChildStrategyOverride, DynamicSupervisorPolicy, EscalationPolicy,
GroupConfig, GroupStrategy, RestartLimit, SupervisionStrategy, SupervisorSpec,
};
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct SupervisorSpecBuilder {
spec: SupervisorSpec,
}
impl SupervisorSpecBuilder {
pub fn root(children: Vec<ChildSpec>) -> Self {
Self {
spec: SupervisorSpec::root(children),
}
}
pub fn new(children: Vec<ChildSpec>) -> Self {
Self::root(children)
}
pub fn path(mut self, path: SupervisorPath) -> Self {
self.spec.path = path;
self
}
pub fn strategy(mut self, strategy: SupervisionStrategy) -> Self {
self.spec.strategy = strategy;
self
}
pub fn children(mut self, children: Vec<ChildSpec>) -> Self {
self.spec.children = children;
self
}
pub fn child(mut self, mut child: ChildSpec) -> Self {
child.shutdown_budget = self.spec.tree_shutdown.budget;
self.spec.children.push(child);
self
}
pub fn config_version(mut self, config_version: impl Into<String>) -> Self {
self.spec.config_version = config_version.into();
self
}
pub fn default_restart_policy(mut self, default_restart_policy: RestartPolicy) -> Self {
self.spec.default_restart_policy = default_restart_policy;
self
}
pub fn default_backoff_policy(mut self, default_backoff_policy: BackoffPolicy) -> Self {
self.spec.default_backoff_policy = default_backoff_policy;
self
}
pub fn default_health_policy(mut self, default_health_policy: HealthPolicy) -> Self {
self.spec.default_health_policy = default_health_policy;
self
}
pub fn tree_shutdown(mut self, tree_shutdown: TreeShutdownPolicy) -> Self {
self.spec.tree_shutdown = tree_shutdown;
self
}
pub fn supervisor_failure_limit(mut self, supervisor_failure_limit: u32) -> Self {
self.spec.supervisor_failure_limit = supervisor_failure_limit;
self
}
pub fn restart_limit(mut self, restart_limit: RestartLimit) -> Self {
self.spec.restart_limit = Some(restart_limit);
self
}
pub fn without_restart_limit(mut self) -> Self {
self.spec.restart_limit = None;
self
}
pub fn escalation_policy(mut self, escalation_policy: EscalationPolicy) -> Self {
self.spec.escalation_policy = Some(escalation_policy);
self
}
pub fn without_escalation_policy(mut self) -> Self {
self.spec.escalation_policy = None;
self
}
pub fn group_strategies(mut self, group_strategies: Vec<GroupStrategy>) -> Self {
self.spec.group_strategies = group_strategies;
self
}
pub fn group_strategy(mut self, group_strategy: GroupStrategy) -> Self {
self.spec.group_strategies.push(group_strategy);
self
}
pub fn group_configs(mut self, group_configs: Vec<GroupConfig>) -> Self {
self.spec.group_configs = group_configs;
self
}
pub fn group_config(mut self, group_config: GroupConfig) -> Self {
self.spec.group_configs.push(group_config);
self
}
pub fn group_dependencies(mut self, group_dependencies: Vec<GroupDependencyEdge>) -> Self {
self.spec.group_dependencies = group_dependencies;
self
}
pub fn group_dependency(mut self, group_dependency: GroupDependencyEdge) -> Self {
self.spec.group_dependencies.push(group_dependency);
self
}
pub fn severity_defaults(
mut self,
severity_defaults: HashMap<TaskRole, SeverityClass>,
) -> Self {
self.spec.severity_defaults = severity_defaults;
self
}
pub fn severity_default(mut self, task_role: TaskRole, severity: SeverityClass) -> Self {
self.spec.severity_defaults.insert(task_role, severity);
self
}
pub fn without_severity_default(mut self, task_role: TaskRole) -> Self {
self.spec.severity_defaults.remove(&task_role);
self
}
pub fn child_strategy_overrides(
mut self,
child_strategy_overrides: Vec<ChildStrategyOverride>,
) -> Self {
self.spec.child_strategy_overrides = child_strategy_overrides;
self
}
pub fn child_strategy_override(
mut self,
child_strategy_override: ChildStrategyOverride,
) -> Self {
self.spec
.child_strategy_overrides
.push(child_strategy_override);
self
}
pub fn dynamic_supervisor_policy(
mut self,
dynamic_supervisor_policy: DynamicSupervisorPolicy,
) -> Self {
self.spec.dynamic_supervisor_policy = dynamic_supervisor_policy;
self
}
pub fn control_channel_capacity(mut self, control_channel_capacity: usize) -> Self {
self.spec.control_channel_capacity = control_channel_capacity;
self
}
pub fn event_channel_capacity(mut self, event_channel_capacity: usize) -> Self {
self.spec.event_channel_capacity = event_channel_capacity;
self
}
pub fn backpressure_config(mut self, backpressure_config: BackpressureConfig) -> Self {
self.spec.backpressure_config = backpressure_config;
self
}
pub fn meltdown_policy(mut self, meltdown_policy: MeltdownPolicy) -> Self {
self.spec.meltdown_policy = meltdown_policy;
self
}
pub fn failure_window_config(mut self, failure_window_config: FailureWindowConfig) -> Self {
self.spec.failure_window_config = failure_window_config;
self
}
pub fn restart_budget_config(mut self, restart_budget_config: RestartBudgetConfig) -> Self {
self.spec.restart_budget_config = restart_budget_config;
self
}
pub fn pipeline_journal_capacity(mut self, pipeline_journal_capacity: usize) -> Self {
self.spec.pipeline_journal_capacity = pipeline_journal_capacity;
self
}
pub fn pipeline_subscriber_capacity(mut self, pipeline_subscriber_capacity: usize) -> Self {
self.spec.pipeline_subscriber_capacity = pipeline_subscriber_capacity;
self
}
pub fn concurrent_restart_limit(mut self, concurrent_restart_limit: u32) -> Self {
self.spec.concurrent_restart_limit = concurrent_restart_limit;
self
}
pub fn metrics_enabled(mut self, metrics_enabled: bool) -> Self {
self.spec.metrics_enabled = metrics_enabled;
self
}
pub fn audit_enabled(mut self, audit_enabled: bool) -> Self {
self.spec.audit_enabled = audit_enabled;
self
}
pub fn build(self) -> Result<SupervisorSpec, SupervisorError> {
let spec = self.spec;
spec.validate()?;
Ok(spec)
}
}