Move llama-server cleanup code to a separate file
This commit is contained in:
parent
7de694c0cd
commit
ad06b2a903
@ -0,0 +1,58 @@
|
|||||||
|
use tauri::State;
|
||||||
|
use crate::core::state::AppState;
|
||||||
|
|
||||||
|
pub async fn cleanup_processes(state: State<'_, AppState>) {
|
||||||
|
let mut map = state.llama_server_process.lock().await;
|
||||||
|
let pids: Vec<String> = map.keys().cloned().collect();
|
||||||
|
for pid in pids {
|
||||||
|
if let Some(mut child) = map.remove(&pid) {
|
||||||
|
#[cfg(unix)]
|
||||||
|
{
|
||||||
|
use nix::sys::signal::{kill, Signal};
|
||||||
|
use nix::unistd::Pid;
|
||||||
|
use tokio::time::{timeout, Duration};
|
||||||
|
|
||||||
|
if let Some(raw_pid) = child.id() {
|
||||||
|
let raw_pid = raw_pid as i32;
|
||||||
|
log::info!("Sending SIGTERM to PID {} during shutdown", raw_pid);
|
||||||
|
let _ = kill(Pid::from_raw(raw_pid), Signal::SIGTERM);
|
||||||
|
|
||||||
|
match timeout(Duration::from_secs(2), child.wait()).await {
|
||||||
|
Ok(Ok(status)) => log::info!("Process {} exited gracefully: {}", raw_pid, status),
|
||||||
|
Ok(Err(e)) => log::error!("Error waiting after SIGTERM for {}: {}", raw_pid, e),
|
||||||
|
Err(_) => {
|
||||||
|
log::warn!("SIGTERM timed out for PID {}; sending SIGKILL", raw_pid);
|
||||||
|
let _ = kill(Pid::from_raw(raw_pid), Signal::SIGKILL);
|
||||||
|
let _ = child.wait().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
{
|
||||||
|
use windows_sys::Win32::System::Console::{GenerateConsoleCtrlEvent, CTRL_C_EVENT};
|
||||||
|
use windows_sys::Win32::Foundation::BOOL;
|
||||||
|
use tokio::time::{timeout, Duration};
|
||||||
|
|
||||||
|
if let Some(raw_pid) = child.id() {
|
||||||
|
log::info!("Sending Ctrl-C to PID {} during shutdown", raw_pid);
|
||||||
|
let ok: BOOL = unsafe { GenerateConsoleCtrlEvent(CTRL_C_EVENT, raw_pid) };
|
||||||
|
if ok == 0 {
|
||||||
|
log::error!("Failed to send Ctrl-C to PID {}", raw_pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
match timeout(Duration::from_secs(2), child.wait()).await {
|
||||||
|
Ok(Ok(status)) => log::info!("Process {} exited after Ctrl-C: {}", raw_pid, status),
|
||||||
|
Ok(Err(e)) => log::error!("Error waiting after Ctrl-C for {}: {}", raw_pid, e),
|
||||||
|
Err(_) => {
|
||||||
|
log::warn!("Timed out for PID {}; force-killing", raw_pid);
|
||||||
|
let _ = child.kill().await;
|
||||||
|
let _ = child.wait().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1 +1,2 @@
|
|||||||
pub mod server;
|
pub mod server;
|
||||||
|
pub mod cleanup;
|
||||||
|
|||||||
@ -8,6 +8,7 @@ use core::{
|
|||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::{collections::HashMap, sync::Arc};
|
||||||
use tauri::{Emitter, Manager};
|
use tauri::{Emitter, Manager};
|
||||||
|
use core::utils::extensions::inference_llamacpp_extension::cleanup::cleanup_processes;
|
||||||
|
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
@ -135,92 +136,7 @@ pub fn run() {
|
|||||||
let state = window.app_handle().state::<AppState>();
|
let state = window.app_handle().state::<AppState>();
|
||||||
|
|
||||||
tauri::async_runtime::block_on(async {
|
tauri::async_runtime::block_on(async {
|
||||||
let mut map = state.llama_server_process.lock().await;
|
cleanup_processes(state).await;
|
||||||
let pids: Vec<String> = map.keys().cloned().collect();
|
|
||||||
for pid in pids {
|
|
||||||
if let Some(mut child) = map.remove(&pid) {
|
|
||||||
#[cfg(unix)]
|
|
||||||
{
|
|
||||||
use nix::sys::signal::{kill, Signal};
|
|
||||||
use nix::unistd::Pid;
|
|
||||||
use tokio::time::{timeout, Duration};
|
|
||||||
|
|
||||||
if let Some(raw_pid) = child.id() {
|
|
||||||
let raw_pid = raw_pid as i32;
|
|
||||||
log::info!(
|
|
||||||
"Sending SIGTERM to PID {} during shutdown",
|
|
||||||
raw_pid
|
|
||||||
);
|
|
||||||
let _ = kill(Pid::from_raw(raw_pid), Signal::SIGTERM);
|
|
||||||
|
|
||||||
match timeout(Duration::from_secs(2), child.wait()).await {
|
|
||||||
Ok(Ok(status)) => log::info!(
|
|
||||||
"Process {} exited gracefully: {}",
|
|
||||||
raw_pid,
|
|
||||||
status
|
|
||||||
),
|
|
||||||
Ok(Err(e)) => log::error!(
|
|
||||||
"Error waiting after SIGTERM for {}: {}",
|
|
||||||
raw_pid,
|
|
||||||
e
|
|
||||||
),
|
|
||||||
Err(_) => {
|
|
||||||
log::warn!(
|
|
||||||
"SIGTERM timed out for PID {}; sending SIGKILL",
|
|
||||||
raw_pid
|
|
||||||
);
|
|
||||||
let _ =
|
|
||||||
kill(Pid::from_raw(raw_pid), Signal::SIGKILL);
|
|
||||||
let _ = child.wait().await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
{
|
|
||||||
use tokio::time::{timeout, Duration};
|
|
||||||
use windows_sys::Win32::Foundation::BOOL;
|
|
||||||
use windows_sys::Win32::System::Console::{
|
|
||||||
GenerateConsoleCtrlEvent, CTRL_C_EVENT,
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(raw_pid) = child.id() {
|
|
||||||
log::info!(
|
|
||||||
"Sending Ctrl-C to PID {} during shutdown",
|
|
||||||
raw_pid
|
|
||||||
);
|
|
||||||
let ok: BOOL = unsafe {
|
|
||||||
GenerateConsoleCtrlEvent(CTRL_C_EVENT, raw_pid)
|
|
||||||
};
|
|
||||||
if ok == 0 {
|
|
||||||
log::error!("Failed to send Ctrl-C to PID {}", raw_pid);
|
|
||||||
}
|
|
||||||
|
|
||||||
match timeout(Duration::from_secs(2), child.wait()).await {
|
|
||||||
Ok(Ok(status)) => log::info!(
|
|
||||||
"Process {} exited after Ctrl-C: {}",
|
|
||||||
raw_pid,
|
|
||||||
status
|
|
||||||
),
|
|
||||||
Ok(Err(e)) => log::error!(
|
|
||||||
"Error waiting after Ctrl-C for {}: {}",
|
|
||||||
raw_pid,
|
|
||||||
e
|
|
||||||
),
|
|
||||||
Err(_) => {
|
|
||||||
log::warn!(
|
|
||||||
"Timed out for PID {}; force-killing",
|
|
||||||
raw_pid
|
|
||||||
);
|
|
||||||
let _ = child.kill().await;
|
|
||||||
let _ = child.wait().await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user