fix: 轮转后台日志并关闭开发访问日志
This commit is contained in:
@@ -10,6 +10,7 @@ use tauri::{Emitter, Manager};
|
||||
#[cfg(windows)]
|
||||
mod job;
|
||||
|
||||
const LOG_LIMIT: u64 = 5 * 1024 * 1024;
|
||||
const CLOSE_GRACE: Duration = Duration::from_secs(10);
|
||||
|
||||
struct BackendState {
|
||||
@@ -72,6 +73,16 @@ fn backend_info() -> BackendInfo {
|
||||
snapshot(&mut state)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn restart_backend(app: tauri::AppHandle) -> BackendInfo {
|
||||
let _guard = lock_launch();
|
||||
let mut state = lock_backend();
|
||||
stop_child(&mut state);
|
||||
prepare(&app, &mut state);
|
||||
spawn_child(&app, &mut state);
|
||||
snapshot(&mut state)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn set_close_guard(active: u32) {
|
||||
ACTIVE.store(active, Ordering::SeqCst);
|
||||
@@ -90,16 +101,6 @@ fn close_app(app: tauri::AppHandle) {
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn restart_backend(app: tauri::AppHandle) -> BackendInfo {
|
||||
let _guard = lock_launch();
|
||||
let mut state = lock_backend();
|
||||
stop_child(&mut state);
|
||||
prepare(&app, &mut state);
|
||||
spawn_child(&app, &mut state);
|
||||
snapshot(&mut state)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn write_sample_csv(path: String) -> Result<(), String> {
|
||||
let content = "\u{FEFF}20米栅格ID\r\n541927061\r\n544536471\r\n";
|
||||
@@ -237,6 +238,7 @@ fn launch_plan(app: &tauri::AppHandle, port: u16) -> Option<Launch> {
|
||||
"127.0.0.1".into(),
|
||||
"--port".into(),
|
||||
port.to_string(),
|
||||
"--no-access-log".into(),
|
||||
],
|
||||
cwd: repo.join("backend"),
|
||||
data_dir: repo.join("data"),
|
||||
@@ -259,6 +261,7 @@ fn spawn_child(app: &tauri::AppHandle, state: &mut BackendState) {
|
||||
state.error = Some(format!("无法创建数据目录:{error}"));
|
||||
return;
|
||||
}
|
||||
rotate_log(&plan.data_dir);
|
||||
let mut command = Command::new(&plan.program);
|
||||
command
|
||||
.args(&plan.args)
|
||||
@@ -268,6 +271,7 @@ fn spawn_child(app: &tauri::AppHandle, state: &mut BackendState) {
|
||||
.env("NQI_DOWNLOAD_DIR", &state.download_dir)
|
||||
.env("NQI_TOKEN", &state.token)
|
||||
.env("NQI_PARENT_PID", std::process::id().to_string())
|
||||
.env("PYTHONUNBUFFERED", "1")
|
||||
.env_remove("HTTP_PROXY")
|
||||
.env_remove("HTTPS_PROXY")
|
||||
.env_remove("ALL_PROXY")
|
||||
@@ -317,6 +321,19 @@ fn open_log(path: &Path) -> Option<(Stdio, Stdio)> {
|
||||
Some((Stdio::from(file), Stdio::from(error)))
|
||||
}
|
||||
|
||||
fn rotate_log(data_dir: &Path) {
|
||||
let log = data_dir.join("backend.log");
|
||||
let Ok(meta) = std::fs::metadata(&log) else {
|
||||
return;
|
||||
};
|
||||
if meta.len() <= LOG_LIMIT {
|
||||
return;
|
||||
}
|
||||
let rotated = data_dir.join("backend.log.1");
|
||||
let _ = std::fs::remove_file(&rotated);
|
||||
let _ = std::fs::rename(&log, rotated);
|
||||
}
|
||||
|
||||
fn stop_child(state: &mut BackendState) {
|
||||
#[cfg(windows)]
|
||||
job::terminate_tree();
|
||||
@@ -344,6 +361,26 @@ fn ensure_started(app: &tauri::AppHandle) {
|
||||
spawn_child(app, &mut state);
|
||||
}
|
||||
|
||||
fn focus_existing(app: &tauri::AppHandle) {
|
||||
let Some(window) = app.get_webview_window("main") else {
|
||||
return;
|
||||
};
|
||||
let _ = window.show();
|
||||
let _ = window.unminimize();
|
||||
#[cfg(windows)]
|
||||
if let Ok(hwnd) = window.hwnd() {
|
||||
unsafe {
|
||||
use windows_sys::Win32::UI::WindowsAndMessaging::{
|
||||
SetForegroundWindow, ShowWindow, SW_RESTORE,
|
||||
};
|
||||
let raw = hwnd.0 as windows_sys::Win32::Foundation::HWND;
|
||||
ShowWindow(raw, SW_RESTORE);
|
||||
SetForegroundWindow(raw);
|
||||
}
|
||||
}
|
||||
let _ = window.set_focus();
|
||||
}
|
||||
|
||||
fn install_close_guard(app: &tauri::AppHandle) {
|
||||
let Some(window) = app.get_webview_window("main") else {
|
||||
return;
|
||||
@@ -379,26 +416,6 @@ fn install_close_guard(app: &tauri::AppHandle) {
|
||||
});
|
||||
}
|
||||
|
||||
fn focus_existing(app: &tauri::AppHandle) {
|
||||
let Some(window) = app.get_webview_window("main") else {
|
||||
return;
|
||||
};
|
||||
let _ = window.show();
|
||||
let _ = window.unminimize();
|
||||
#[cfg(windows)]
|
||||
if let Ok(hwnd) = window.hwnd() {
|
||||
unsafe {
|
||||
use windows_sys::Win32::UI::WindowsAndMessaging::{
|
||||
SetForegroundWindow, ShowWindow, SW_RESTORE,
|
||||
};
|
||||
let raw = hwnd.0 as windows_sys::Win32::Foundation::HWND;
|
||||
ShowWindow(raw, SW_RESTORE);
|
||||
SetForegroundWindow(raw);
|
||||
}
|
||||
}
|
||||
let _ = window.set_focus();
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
let mut builder = tauri::Builder::default();
|
||||
|
||||
Reference in New Issue
Block a user