feat: 用作业对象包住后台进程并传入父进程号

This commit is contained in:
2026-10-25 07:39:44 +08:00
parent 555b9f06cb
commit 2bca8a1dbf
4 changed files with 171 additions and 4 deletions
+16 -4
View File
@@ -7,6 +7,9 @@ use std::time::{Duration, Instant};
use tauri::Manager;
#[cfg(windows)]
mod job;
static BACKEND: Mutex<Option<Child>> = Mutex::new(None);
static PORT: AtomicU16 = AtomicU16::new(0);
@@ -121,6 +124,7 @@ fn start_backend(app: &tauri::App) {
.current_dir(&plan.cwd)
.env("NQI_PORT", port.to_string())
.env("NQI_DATA_DIR", &plan.data_dir)
.env("NQI_PARENT_PID", std::process::id().to_string())
.env_remove("HTTP_PROXY")
.env_remove("HTTPS_PROXY")
.env_remove("ALL_PROXY")
@@ -140,11 +144,17 @@ fn start_backend(app: &tauri::App) {
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
command.creation_flags(0x0800_0000);
}
if let Ok(child) = command.spawn() {
*BACKEND.lock().expect("backend lock") = Some(child);
command.creation_flags(job::creation_flags());
}
let Ok(child) = command.spawn() else {
return;
};
#[cfg(windows)]
let child = match job::adopt(child) {
Ok(child) => child,
Err(_) => return,
};
*BACKEND.lock().expect("backend lock") = Some(child);
wait_until_ready(port);
}
@@ -168,6 +178,8 @@ fn wait_until_ready(port: u16) {
}
fn stop_backend() {
#[cfg(windows)]
job::terminate_tree();
if let Ok(mut slot) = BACKEND.lock() {
if let Some(child) = slot.as_mut() {
let _ = child.kill();