From 0d7b89fbad5f5de00855db15b2d4adee6e97361e Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 26 Mar 2025 14:28:01 +0700 Subject: [PATCH] chore: kill sidecar process on exit --- package.json | 3 +++ src-tauri/binaries/download.sh | 5 +++++ src-tauri/src/core/setup.rs | 18 +++++++++++++----- src-tauri/src/lib.rs | 8 +++++++- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index e6cbc87c0..4516e86ba 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src-tauri/binaries/download.sh b/src-tauri/binaries/download.sh index 29cd4258d..7ef186d20 100755 --- a/src-tauri/binaries/download.sh +++ b/src-tauri/binaries/download.sh @@ -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}" diff --git a/src-tauri/src/core/setup.rs b/src-tauri/src/core/setup.rs index e8fbb8d73..c28c3bab9 100644 --- a/src-tauri/src/core/setup.rs +++ b/src-tauri/src/core/setup.rs @@ -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(()) } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 2afe3397f..ce16e557e 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -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, @@ -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"); }