refactor(core): optimize RunningMode handling and improve state management

This commit is contained in:
Tunglies
2025-10-28 19:16:42 +08:00
parent 2af0af0837
commit 2a7ccb5bde
4 changed files with 23 additions and 18 deletions

View File

@@ -14,7 +14,7 @@ impl CoreManager {
pub async fn start_core(&self) -> Result<()> { pub async fn start_core(&self) -> Result<()> {
self.prepare_startup().await?; self.prepare_startup().await?;
match self.get_running_mode() { match *self.get_running_mode() {
RunningMode::Service => self.start_core_by_service().await, RunningMode::Service => self.start_core_by_service().await,
RunningMode::NotRunning | RunningMode::Sidecar => self.start_core_by_sidecar().await, RunningMode::NotRunning | RunningMode::Sidecar => self.start_core_by_sidecar().await,
} }
@@ -23,7 +23,7 @@ impl CoreManager {
pub async fn stop_core(&self) -> Result<()> { pub async fn stop_core(&self) -> Result<()> {
ClashLogger::global().clear_logs(); ClashLogger::global().clear_logs();
match self.get_running_mode() { match *self.get_running_mode() {
RunningMode::Service => self.stop_core_by_service().await, RunningMode::Service => self.stop_core_by_service().await,
RunningMode::Sidecar => self.stop_core_by_sidecar(), RunningMode::Sidecar => self.stop_core_by_sidecar(),
RunningMode::NotRunning => Ok(()), RunningMode::NotRunning => Ok(()),

View File

@@ -11,7 +11,7 @@ use tokio::sync::Semaphore;
use crate::process::CommandChildGuard; use crate::process::CommandChildGuard;
use crate::singleton_lazy; use crate::singleton_lazy;
#[derive(Debug, Clone, Copy, serde::Serialize, PartialEq, Eq)] #[derive(Debug, serde::Serialize, PartialEq, Eq)]
pub enum RunningMode { pub enum RunningMode {
Service, Service,
Sidecar, Sidecar,
@@ -37,14 +37,14 @@ pub struct CoreManager {
#[derive(Debug)] #[derive(Debug)]
struct State { struct State {
running_mode: RunningMode, running_mode: Arc<RunningMode>,
child_sidecar: Option<CommandChildGuard>, child_sidecar: Option<CommandChildGuard>,
} }
impl Default for State { impl Default for State {
fn default() -> Self { fn default() -> Self {
Self { Self {
running_mode: RunningMode::NotRunning, running_mode: Arc::new(RunningMode::NotRunning),
child_sidecar: None, child_sidecar: None,
} }
} }
@@ -61,12 +61,16 @@ impl Default for CoreManager {
} }
impl CoreManager { impl CoreManager {
pub fn get_running_mode(&self) -> RunningMode { pub fn get_running_mode(&self) -> Arc<RunningMode> {
self.state.lock().running_mode Arc::clone(&self.state.lock().running_mode)
} }
pub fn set_running_mode(&self, mode: RunningMode) { pub fn set_running_mode(&self, mode: RunningMode) {
self.state.lock().running_mode = mode; self.state.lock().running_mode = Arc::new(mode);
}
pub fn set_running_child_sidecar(&self, child: CommandChildGuard) {
self.state.lock().child_sidecar = Some(child);
} }
pub async fn init(&self) -> Result<()> { pub async fn init(&self) -> Result<()> {

View File

@@ -15,12 +15,13 @@ use anyhow::Result;
use compact_str::CompactString; use compact_str::CompactString;
use flexi_logger::DeferredNow; use flexi_logger::DeferredNow;
use log::Level; use log::Level;
use scopeguard::defer;
use std::collections::VecDeque; use std::collections::VecDeque;
use tauri_plugin_shell::ShellExt; use tauri_plugin_shell::ShellExt;
impl CoreManager { impl CoreManager {
pub async fn get_clash_logs(&self) -> Result<VecDeque<CompactString>> { pub async fn get_clash_logs(&self) -> Result<VecDeque<CompactString>> {
match self.get_running_mode() { match *self.get_running_mode() {
RunningMode::Service => service::get_clash_logs_by_service().await, RunningMode::Service => service::get_clash_logs_by_service().await,
RunningMode::Sidecar => Ok(ClashLogger::global().get_logs().clone()), RunningMode::Sidecar => Ok(ClashLogger::global().get_logs().clone()),
RunningMode::NotRunning => Ok(VecDeque::new()), RunningMode::NotRunning => Ok(VecDeque::new()),
@@ -49,11 +50,8 @@ impl CoreManager {
let pid = child.pid(); let pid = child.pid();
logging!(trace, Type::Core, "Sidecar started with PID: {}", pid); logging!(trace, Type::Core, "Sidecar started with PID: {}", pid);
{ self.set_running_child_sidecar(CommandChildGuard::new(child));
let mut state = self.state.lock(); self.set_running_mode(RunningMode::Sidecar);
state.child_sidecar = Some(CommandChildGuard::new(child));
state.running_mode = RunningMode::Sidecar;
}
let shared_writer: SharedWriter = let shared_writer: SharedWriter =
std::sync::Arc::new(tokio::sync::Mutex::new(sidecar_writer().await?)); std::sync::Arc::new(tokio::sync::Mutex::new(sidecar_writer().await?));
@@ -93,14 +91,15 @@ impl CoreManager {
pub(super) fn stop_core_by_sidecar(&self) -> Result<()> { pub(super) fn stop_core_by_sidecar(&self) -> Result<()> {
logging!(info, Type::Core, "Stopping sidecar"); logging!(info, Type::Core, "Stopping sidecar");
defer! {
self.set_running_mode(RunningMode::NotRunning);
}
let mut state = self.state.lock(); let mut state = self.state.lock();
if let Some(child) = state.child_sidecar.take() { if let Some(child) = state.child_sidecar.take() {
let pid = child.pid(); let pid = child.pid();
drop(child); drop(child);
logging!(trace, Type::Core, "Sidecar stopped (PID: {:?})", pid); logging!(trace, Type::Core, "Sidecar stopped (PID: {:?})", pid);
} }
state.running_mode = RunningMode::NotRunning;
Ok(()) Ok(())
} }
@@ -114,8 +113,10 @@ impl CoreManager {
pub(super) async fn stop_core_by_service(&self) -> Result<()> { pub(super) async fn stop_core_by_service(&self) -> Result<()> {
logging!(info, Type::Core, "Stopping service"); logging!(info, Type::Core, "Stopping service");
defer! {
self.set_running_mode(RunningMode::NotRunning);
}
service::stop_core_by_service().await?; service::stop_core_by_service().await?;
self.set_running_mode(RunningMode::NotRunning);
Ok(()) Ok(())
} }
} }

View File

@@ -11,7 +11,7 @@ use tokio::sync::{Mutex, MutexGuard};
pub type SharedWriter = Arc<Mutex<FileLogWriter>>; pub type SharedWriter = Arc<Mutex<FileLogWriter>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum Type { pub enum Type {
Cmd, Cmd,
Core, Core,