use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TerminationConfig {
pub max_ticks: u64,
pub multi_worker_strategy: MultiWorkerStrategy,
pub on_exhausted: ExhaustedBehavior,
pub max_consecutive_errors: Option<u64>,
pub graceful_shutdown_ticks: u64,
}
impl Default for TerminationConfig {
fn default() -> Self {
Self {
max_ticks: 1000,
multi_worker_strategy: MultiWorkerStrategy::FirstSuccess,
on_exhausted: ExhaustedBehavior::Fail,
max_consecutive_errors: None,
graceful_shutdown_ticks: 0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum MultiWorkerStrategy {
#[default]
FirstSuccess,
AllComplete,
AllSuccess,
Conditions,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ExhaustedBehavior {
#[default]
Fail,
Success,
CheckConditions,
}
impl TerminationConfig {
pub fn with_max_ticks(max_ticks: u64) -> Self {
Self {
max_ticks,
..Default::default()
}
}
pub fn multi_worker_strategy(mut self, strategy: MultiWorkerStrategy) -> Self {
self.multi_worker_strategy = strategy;
self
}
pub fn on_exhausted(mut self, behavior: ExhaustedBehavior) -> Self {
self.on_exhausted = behavior;
self
}
pub fn max_consecutive_errors(mut self, max: u64) -> Self {
self.max_consecutive_errors = Some(max);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = TerminationConfig::default();
assert_eq!(config.max_ticks, 1000);
assert_eq!(
config.multi_worker_strategy,
MultiWorkerStrategy::FirstSuccess
);
assert_eq!(config.on_exhausted, ExhaustedBehavior::Fail);
}
#[test]
fn test_builder_pattern() {
let config = TerminationConfig::with_max_ticks(500)
.multi_worker_strategy(MultiWorkerStrategy::AllSuccess)
.on_exhausted(ExhaustedBehavior::Success);
assert_eq!(config.max_ticks, 500);
assert_eq!(
config.multi_worker_strategy,
MultiWorkerStrategy::AllSuccess
);
assert_eq!(config.on_exhausted, ExhaustedBehavior::Success);
}
}