refactor: kill core by process name

This commit is contained in:
MystiPanda 2024-06-18 12:09:54 +08:00
parent 3083ab74a6
commit 6fa0f92ceb
2 changed files with 18 additions and 33 deletions

View File

@ -2,12 +2,12 @@ use super::service;
use super::{clash_api, logger::Logger}; use super::{clash_api, logger::Logger};
use crate::log_err; use crate::log_err;
use crate::{config::*, utils::dirs}; use crate::{config::*, utils::dirs};
use anyhow::{bail, Context, Result}; use anyhow::{bail, Result};
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use parking_lot::Mutex; use parking_lot::Mutex;
use serde_yaml::Mapping; use serde_yaml::Mapping;
use std::{fs, io::Write, sync::Arc, time::Duration}; use std::{sync::Arc, time::Duration};
use sysinfo::{Pid, System}; use sysinfo::System;
use tauri::api::process::{Command, CommandChild, CommandEvent}; use tauri::api::process::{Command, CommandChild, CommandEvent};
use tokio::time::sleep; use tokio::time::sleep;
@ -30,21 +30,6 @@ impl CoreManager {
} }
pub fn init(&self) -> Result<()> { pub fn init(&self) -> Result<()> {
// kill old clash process
let _ = dirs::clash_pid_path()
.and_then(|path| fs::read(path).map(|p| p.to_vec()).context(""))
.and_then(|pid| String::from_utf8_lossy(&pid).parse().context(""))
.map(|pid| {
let mut system = System::new();
system.refresh_all();
if let Some(proc) = system.process(Pid::from_u32(pid)) {
if proc.name().contains("clash") {
log::debug!(target: "app", "kill old clash process");
proc.kill();
}
}
});
tauri::async_runtime::spawn(async { tauri::async_runtime::spawn(async {
// 启动clash // 启动clash
log_err!(Self::global().run_core().await); log_err!(Self::global().run_core().await);
@ -95,6 +80,14 @@ impl CoreManager {
None => false, None => false,
}; };
let mut system = System::new();
system.refresh_all();
let procs = system.processes_by_name("clash-meta");
for proc in procs {
log::debug!(target: "app", "kill all clash process");
proc.kill();
}
if *self.use_service_mode.lock() { if *self.use_service_mode.lock() {
log::debug!(target: "app", "stop the core by service"); log::debug!(target: "app", "stop the core by service");
log_err!(service::stop_core_by_service().await); log_err!(service::stop_core_by_service().await);
@ -149,17 +142,6 @@ impl CoreManager {
let cmd = Command::new_sidecar(clash_core)?; let cmd = Command::new_sidecar(clash_core)?;
let (mut rx, cmd_child) = cmd.args(args).spawn()?; let (mut rx, cmd_child) = cmd.args(args).spawn()?;
// 将pid写入文件中
crate::log_err!((|| {
let pid = cmd_child.pid();
let path = dirs::clash_pid_path()?;
fs::File::create(path)
.context("failed to create the pid file")?
.write(format!("{pid}").as_bytes())
.context("failed to write pid to the file")?;
<Result<()>>::Ok(())
})());
let mut sidecar = self.sidecar.lock(); let mut sidecar = self.sidecar.lock();
*sidecar = Some(cmd_child); *sidecar = Some(cmd_child);
drop(sidecar); drop(sidecar);
@ -256,6 +238,13 @@ impl CoreManager {
log::debug!(target: "app", "stop the core by sidecar"); log::debug!(target: "app", "stop the core by sidecar");
let _ = child.kill(); let _ = child.kill();
} }
let mut system = System::new();
system.refresh_all();
let procs = system.processes_by_name("clash-meta");
for proc in procs {
log::debug!(target: "app", "kill all clash process");
proc.kill();
}
Ok(()) Ok(())
} }

View File

@ -88,10 +88,6 @@ pub fn profiles_path() -> Result<PathBuf> {
Ok(app_home_dir()?.join(PROFILE_YAML)) Ok(app_home_dir()?.join(PROFILE_YAML))
} }
pub fn clash_pid_path() -> Result<PathBuf> {
Ok(app_home_dir()?.join("clash.pid"))
}
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
pub fn service_path() -> Result<PathBuf> { pub fn service_path() -> Result<PathBuf> {
Ok(app_resources_dir()?.join("clash-verge-service")) Ok(app_resources_dir()?.join("clash-verge-service"))