chore: kill sidecar process on exit

This commit is contained in:
Louis 2025-03-26 14:28:01 +07:00
parent 27e85010d4
commit dfbeb553b3
No known key found for this signature in database
GPG Key ID: 44FA9F4D33C37DE2
4 changed files with 28 additions and 6 deletions

View File

@ -22,6 +22,9 @@
"dev:web": "yarn workspace @janhq/web dev",
"dev:server": "yarn workspace @janhq/server dev",
"dev": "concurrently -n \"NEXT,ELECTRON\" -c \"yellow,blue\" --kill-others \"yarn dev:web\" \"yarn dev:electron\"",
"install:cortex:linux:darwin": "cd src-tauri/binaries && ./download.sh",
"install:cortex:win32": "cd src-tauri/binaries && download.bat",
"install:cortex": "run-script-os",
"dev:tauri": "tauri dev",
"build:server": "cd server && yarn build",
"build:core": "cd core && yarn build && yarn pack",

View File

@ -24,6 +24,11 @@ SHARED_PATH="."
# Detect platform
OS_TYPE=$(uname)
if ls ./cortex-server* 1> /dev/null 2>&1; then
echo "cortex-server file with prefix already exists. Exiting."
exit 0
fi
if [ "$OS_TYPE" == "Linux" ]; then
# Linux downloads
download "${CORTEX_RELEASE_URL}/v${CORTEX_VERSION}/cortex-${CORTEX_VERSION}-linux-amd64.tar.gz" "${BIN_PATH}"

View File

@ -1,10 +1,8 @@
use flate2::read::GzDecoder;
use tauri::{App, Manager};
use tauri::{App, Listener, Manager};
use tauri_plugin_shell::process::CommandEvent;
use tauri_plugin_shell::ShellExt;
use std::fs::{self, File};
use std::io::Read;
use std::path::PathBuf;
use std::{fs::{self, File}, io::Read, path::PathBuf, sync::{Arc, Mutex}};
use tar::Archive;
use crate::AppState;
@ -189,7 +187,10 @@ pub fn setup_sidecar(app: &App) -> Result<(), String> {
});
}
let (mut rx, mut _child) = sidecar_command.spawn().expect("Failed to spawn sidecar");
let (mut rx, _child) = sidecar_command.spawn().expect("Failed to spawn sidecar");
let child = Arc::new(Mutex::new(Some(_child)));
let child_clone = child.clone();
tauri::async_runtime::spawn(async move {
// read events such as stdout
while let Some(event) = rx.recv().await {
@ -199,6 +200,13 @@ pub fn setup_sidecar(app: &App) -> Result<(), String> {
}
}
});
app.handle().listen("kill-sidecar", move |_| {
let mut child_guard = child_clone.lock().unwrap();
if let Some(actual_child) = child_guard.take() {
actual_child.kill().unwrap();
}
});
Ok(())
}

View File

@ -2,7 +2,7 @@ mod core;
use core::setup::{self, setup_engine_binaries, setup_sidecar};
use rand::{distributions::Alphanumeric, Rng};
use tauri::{command, State};
use tauri::{command, Emitter, State};
struct AppState {
app_token: Option<String>,
@ -69,6 +69,12 @@ pub fn run() {
Ok(())
})
.on_window_event(|window, event| match event {
tauri::WindowEvent::CloseRequested { api, .. } => {
window.emit("kill-sidecar", ()).unwrap();
}
_ => {}
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}