use serde::Deserialize;
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum RecoveryAction {
#[default]
None,
Restart,
Stop,
#[serde(rename = "command")]
Command(String),
}
#[derive(Debug, Clone, Deserialize)]
pub struct RecoveryConfig {
#[serde(default)]
pub action: RecoveryAction,
#[serde(default = "default_max_attempts")]
pub max_attempts: u32,
#[serde(default = "default_cooldown")]
pub cooldown: u64,
}
impl RecoveryConfig {
#[allow(dead_code)] pub fn cooldown_duration(&self) -> std::time::Duration {
std::time::Duration::from_secs(self.cooldown)
}
#[allow(dead_code)] pub fn should_attempt(&self, last_attempt: Option<std::time::Instant>) -> bool {
match last_attempt {
Some(t) => t.elapsed() >= self.cooldown_duration(),
None => true,
}
}
}
fn default_max_attempts() -> u32 {
3
}
fn default_cooldown() -> u64 {
60
}
impl Default for RecoveryConfig {
fn default() -> Self {
Self {
action: RecoveryAction::None,
max_attempts: default_max_attempts(),
cooldown: default_cooldown(),
}
}
}
impl RecoveryConfig {
#[allow(dead_code)] pub fn restart() -> Self {
Self {
action: RecoveryAction::Restart,
..Default::default()
}
}
#[allow(dead_code)] pub fn stop() -> Self {
Self {
action: RecoveryAction::Stop,
..Default::default()
}
}
#[allow(dead_code)] pub fn command(cmd: &str) -> Self {
Self {
action: RecoveryAction::Command(cmd.to_string()),
..Default::default()
}
}
#[allow(dead_code)] pub fn with_max_attempts(mut self, attempts: u32) -> Self {
self.max_attempts = attempts;
self
}
#[allow(dead_code)] pub fn with_cooldown(mut self, cooldown: u64) -> Self {
self.cooldown = cooldown;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_recovery_action_default() {
let action = RecoveryAction::default();
assert_eq!(action, RecoveryAction::None);
}
#[test]
fn test_recovery_config_builders() {
let restart = RecoveryConfig::restart().with_max_attempts(5);
assert_eq!(restart.action, RecoveryAction::Restart);
assert_eq!(restart.max_attempts, 5);
let stop = RecoveryConfig::stop().with_cooldown(120);
assert_eq!(stop.action, RecoveryAction::Stop);
assert_eq!(stop.cooldown, 120);
let cmd = RecoveryConfig::command("/usr/local/bin/fix.sh");
assert_eq!(
cmd.action,
RecoveryAction::Command("/usr/local/bin/fix.sh".to_string())
);
}
#[test]
fn test_recovery_config_deserialize() {
let toml = r#"
action = "restart"
max_attempts = 5
cooldown = 120
"#;
let config: RecoveryConfig = toml::from_str(toml).unwrap();
assert_eq!(config.action, RecoveryAction::Restart);
assert_eq!(config.max_attempts, 5);
assert_eq!(config.cooldown, 120);
}
#[test]
fn test_recovery_command_deserialize() {
let toml = r#"
action = { command = "/usr/local/bin/restart-service.sh" }
max_attempts = 2
"#;
let config: RecoveryConfig = toml::from_str(toml).unwrap();
assert_eq!(
config.action,
RecoveryAction::Command("/usr/local/bin/restart-service.sh".to_string())
);
}
}