mecha10-runtime 0.6.5

Runtime supervisor for Mecha10 nodes - launching, monitoring, and lifecycle management
Documentation
//! Runtime configuration types

use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Runtime configuration
#[derive(Clone, Debug)]
pub struct RuntimeConfig {
    /// Log level (trace, debug, info, warn, error)
    pub log_level: String,

    /// Log output format
    pub log_format: LogFormat,

    /// Restart policy for failed nodes
    pub restart_policy: RestartPolicy,

    /// Health check interval
    pub health_check_interval: Duration,

    /// Graceful shutdown timeout
    pub shutdown_timeout: Duration,
}

impl Default for RuntimeConfig {
    fn default() -> Self {
        Self {
            log_level: "info".to_string(),
            log_format: LogFormat::Pretty,
            restart_policy: RestartPolicy::Never,
            health_check_interval: Duration::from_secs(30),
            shutdown_timeout: Duration::from_secs(10),
        }
    }
}

/// Log output format
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum LogFormat {
    /// Human-readable pretty format
    Pretty,
    /// Structured JSON format
    Json,
    /// Compact minimal format
    Compact,
}

/// Restart policy for nodes
#[derive(Clone, Debug)]
pub enum RestartPolicy {
    /// Never restart failed nodes
    Never,

    /// Restart on failure with exponential backoff
    OnFailure {
        /// Maximum number of restart attempts
        max_retries: usize,
        /// Initial backoff duration (doubles each retry)
        backoff: Duration,
    },

    /// Always restart nodes with constant backoff
    Always {
        /// Backoff duration between restarts
        backoff: Duration,
    },
}

impl RestartPolicy {
    /// Check if should restart based on attempt count
    pub fn should_restart(&self, attempt: usize) -> bool {
        match self {
            RestartPolicy::Never => false,
            RestartPolicy::OnFailure { max_retries, .. } => attempt < *max_retries,
            RestartPolicy::Always { .. } => true,
        }
    }

    /// Calculate backoff duration for given attempt
    pub fn backoff_duration(&self, attempt: usize) -> Duration {
        match self {
            RestartPolicy::Never => Duration::from_secs(0),
            RestartPolicy::OnFailure { backoff, .. } => *backoff * 2u32.pow(attempt as u32),
            RestartPolicy::Always { backoff } => *backoff,
        }
    }
}