2025-10-21 17:51:12 +08:00
|
|
|
mod config;
|
|
|
|
|
mod lifecycle;
|
|
|
|
|
mod state;
|
|
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
use parking_lot::Mutex;
|
|
|
|
|
use std::{fmt, sync::Arc, time::Instant};
|
|
|
|
|
use tokio::sync::Semaphore;
|
|
|
|
|
|
|
|
|
|
use crate::process::CommandChildGuard;
|
|
|
|
|
use crate::singleton_lazy;
|
|
|
|
|
|
2025-10-28 19:16:42 +08:00
|
|
|
#[derive(Debug, serde::Serialize, PartialEq, Eq)]
|
2025-10-21 17:51:12 +08:00
|
|
|
pub enum RunningMode {
|
|
|
|
|
Service,
|
|
|
|
|
Sidecar,
|
|
|
|
|
NotRunning,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for RunningMode {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Service => write!(f, "Service"),
|
|
|
|
|
Self::Sidecar => write!(f, "Sidecar"),
|
|
|
|
|
Self::NotRunning => write!(f, "NotRunning"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct CoreManager {
|
|
|
|
|
state: Arc<Mutex<State>>,
|
|
|
|
|
update_semaphore: Arc<Semaphore>,
|
|
|
|
|
last_update: Arc<Mutex<Option<Instant>>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct State {
|
2025-10-28 19:16:42 +08:00
|
|
|
running_mode: Arc<RunningMode>,
|
2025-10-21 17:51:12 +08:00
|
|
|
child_sidecar: Option<CommandChildGuard>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for State {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
2025-10-28 19:16:42 +08:00
|
|
|
running_mode: Arc::new(RunningMode::NotRunning),
|
2025-10-21 17:51:12 +08:00
|
|
|
child_sidecar: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for CoreManager {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
state: Arc::new(Mutex::new(State::default())),
|
|
|
|
|
update_semaphore: Arc::new(Semaphore::new(1)),
|
|
|
|
|
last_update: Arc::new(Mutex::new(None)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl CoreManager {
|
2025-10-28 19:16:42 +08:00
|
|
|
pub fn get_running_mode(&self) -> Arc<RunningMode> {
|
|
|
|
|
Arc::clone(&self.state.lock().running_mode)
|
2025-10-21 17:51:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_running_mode(&self, mode: RunningMode) {
|
2025-10-28 19:16:42 +08:00
|
|
|
self.state.lock().running_mode = Arc::new(mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_running_child_sidecar(&self, child: CommandChildGuard) {
|
|
|
|
|
self.state.lock().child_sidecar = Some(child);
|
2025-10-21 17:51:12 +08:00
|
|
|
}
|
2025-10-21 17:53:02 +08:00
|
|
|
|
2025-10-21 17:51:12 +08:00
|
|
|
pub async fn init(&self) -> Result<()> {
|
|
|
|
|
self.start_core().await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
singleton_lazy!(CoreManager, CORE_MANAGER, CoreManager::default);
|