Skip to content

Commit

Permalink
add auto destruction for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashashwini2003 committed Dec 12, 2024
1 parent c49a77b commit fa85c8e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
13 changes: 0 additions & 13 deletions screenpipe-app-tauri/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::env;
use std::fs;
use std::fs::File;
use std::path::PathBuf;
use std::process;
use std::sync::Arc;
use tauri::Config;
use tauri::Manager;
Expand Down Expand Up @@ -120,18 +119,6 @@ async fn main() {
.app_handle()
.set_activation_policy(tauri::ActivationPolicy::Regular);
}

#[cfg(target_os = "windows")]
{
// Kill all screenpipe processes (CLI sidecar)
if let Err(e) = sidecar::kill_all_sreenpipes(
app_handle.state::<SidecarState>(),
app_handle.clone(),
){
error!("Failed to kill sidecar: {}", e);
}
}

window.hide().unwrap();
api.prevent_close();
}
Expand Down
34 changes: 34 additions & 0 deletions screenpipe-app-tauri/src-tauri/src/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{get_base_dir, SidecarState};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::env;
use std::process;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tauri::async_runtime::JoinHandle;
Expand Down Expand Up @@ -103,6 +104,39 @@ pub async fn kill_all_sreenpipes(
}
}

#[cfg(target_os = "windows")]
pub async fn auto_destruct_monitor(auto_destruct_pid: Option<u32>) {
if let Some(pid) = auto_destruct_pid {
loop {
if !is_process_alive(pid) {
tracing::info!("Parent process with PID {} is not alive. Terminating sidecar.", pid);
process::exit(0);
}
sleep(Duration::from_secs(1)).await;
}
}
}

#[cfg(target_os = "windows")]
fn is_process_alive(pid: u32) -> bool {
use windows::Win32::System::Threading::{OpenProcess, PROCESS_QUERY_INFORMATION};
use windows::Win32::Foundation::{CloseHandle, HANDLE};

unsafe {
let process: HANDLE = OpenProcess(PROCESS_QUERY_INFORMATION, false, pid);
if process.is_invalid() {
return false;
}
CloseHandle(process);
true
}
}

#[cfg(not(target_os = "windows"))]
fn is_process_alive(pid: u32) -> bool {
unsafe { libc::kill(pid as i32, 0) == 0 }
}

#[tauri::command]
pub async fn spawn_screenpipe(
state: tauri::State<'_, SidecarState>,
Expand Down

0 comments on commit fa85c8e

Please sign in to comment.