Skip to main content

mecha10_runtime/
config.rs

1//! Runtime configuration types
2
3use serde::{Deserialize, Serialize};
4use std::time::Duration;
5
6#[derive(Clone, Debug)]
7pub struct RuntimeConfig {
8    pub log_level: String,
9
10    pub log_format: LogFormat,
11
12    pub restart_policy: RestartPolicy,
13
14    pub health_check_interval: Duration,
15
16    pub shutdown_timeout: Duration,
17}
18
19impl Default for RuntimeConfig {
20    fn default() -> Self {
21        Self {
22            log_level: "info".to_string(),
23            log_format: LogFormat::Pretty,
24            restart_policy: RestartPolicy::Never,
25            health_check_interval: Duration::from_secs(30),
26            shutdown_timeout: Duration::from_secs(10),
27        }
28    }
29}
30
31#[derive(Clone, Debug, Serialize, Deserialize)]
32pub enum LogFormat {
33    Pretty,
34    Json,
35    Compact,
36}
37
38#[derive(Clone, Debug)]
39pub enum RestartPolicy {
40    Never,
41
42    OnFailure { max_retries: usize, backoff: Duration },
43
44    Always { backoff: Duration },
45}
46
47impl RestartPolicy {
48    /// Check if should restart based on attempt count
49    pub fn should_restart(&self, attempt: usize) -> bool {
50        match self {
51            RestartPolicy::Never => false,
52            RestartPolicy::OnFailure { max_retries, .. } => attempt < *max_retries,
53            RestartPolicy::Always { .. } => true,
54        }
55    }
56
57    /// Calculate backoff duration for given attempt
58    pub fn backoff_duration(&self, attempt: usize) -> Duration {
59        match self {
60            RestartPolicy::Never => Duration::from_secs(0),
61            RestartPolicy::OnFailure { backoff, .. } => *backoff * 2u32.pow(attempt as u32),
62            RestartPolicy::Always { backoff } => *backoff,
63        }
64    }
65}