Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

kill sidecar on update

Ezra Ellette14d58f8211a1
diff --git a/screenpipe-app-tauri/src-tauri/src/main.rs b/screenpipe-app-tauri/src-tauri/src/main.rs
index 75ea4fd26..c8f848f98 100755
--- a/screenpipe-app-tauri/src-tauri/src/main.rs
+++ b/screenpipe-app-tauri/src-tauri/src/main.rs
@@ -5,10 +5,10 @@ use commands::load_pipe_config;
use commands::save_pipe_config;
use serde_json::Value;
use sidecar::SidecarManager;
+use tokio::runtime::Handle;
use std::env;
use std::fs;
use std::fs::File;
-use std::future::IntoFuture;
use std::path::PathBuf;
use std::sync::Arc;
use tauri::Config;
@@ -74,7 +74,7 @@ fn show_main_window(app_handle: &tauri::AppHandle) {
}
#[tokio::main]
-async fn main() {
+async fn main() {
let _ = fix_path_env::fix();
let sidecar_state = SidecarState(Arc::new(tokio::sync::Mutex::new(None)));
@@ -198,7 +198,7 @@ async fn main() {
let quit = MenuItemBuilder::with_id("quit", "Quit Screenpipe").build(app)?;
let menu = MenuBuilder::new(app).items(&[&show, update_manager.update_now_menu_item_ref(), &menu_divider, &quit]).build()?;
let _ = main_tray.set_menu(Some(menu));
-
+
main_tray.on_menu_event(move |app_handle, event| match event.id().as_ref() {
"show" => {
show_main_window(&app_handle);
@@ -207,8 +207,16 @@ async fn main() {
println!("quit clicked");
app_handle.exit(0);
},
- "update_now" => {
- update_manager.update_screenpipe();
+ "update_now" => {
+ tokio::task::block_in_place(move || {
+ Handle::current().block_on(async move {
+ if let Err(err) = sidecar::kill_sidecar(app_handle.state::<SidecarState>(), app_handle.clone()).await {
+ error!("Failed to kill sidecar: {}", err);
+ }
+ });
+
+ });
+ // update_manager.update_screenpipe();
}
_ => (),
});
diff --git a/screenpipe-app-tauri/src-tauri/src/sidecar.rs b/screenpipe-app-tauri/src-tauri/src/sidecar.rs
index 41a3b9bfa..f0ce70a01 100644
--- a/screenpipe-app-tauri/src-tauri/src/sidecar.rs
+++ b/screenpipe-app-tauri/src-tauri/src/sidecar.rs
@@ -4,6 +4,7 @@ use std::env;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tauri::async_runtime::JoinHandle;
+use tauri::Emitter;
use tauri::{Manager, State, Wry};
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
use tauri_plugin_shell::ShellExt;
@@ -11,7 +12,8 @@ use tauri_plugin_store::{with_store, StoreCollection};
use tokio::sync::Mutex;
use tokio::time::sleep;
use tracing::{debug, error, info};
-use tauri::Emitter;
+use tracing_subscriber::field::debug;
+
#[tauri::command]
pub async fn kill_all_sreenpipes(
state: State<'_, SidecarState>,
@@ -60,6 +62,22 @@ pub async fn kill_all_sreenpipes(
}
}
+#[tauri::command]
+pub async fn kill_sidecar(
+ state: State<'_, SidecarState>,
+ app: tauri::AppHandle,
+) -> Result<(), String> {
+ debug!("Killing sidecar");
+
+ let mut manager = state.0.lock().await;
+ if let Some(manager) = manager.as_mut() {
+ if let Some(restart_task) = manager.restart_task.take() {
+ restart_task.abort();
+ }
+ }
+ kill_all_sreenpipes(state.clone(), app).await
+}
+
#[tauri::command]
pub async fn spawn_screenpipe(
state: tauri::State<'_, SidecarState>,
@@ -149,7 +167,12 @@ fn spawn_sidecar(app: &tauri::AppHandle) -> Result<CommandChild, String> {
Ok(store
.get("ignoredWindows")
.and_then(|v| v.as_array())
- .map(|arr| arr.iter().filter_map(|v| v.as_str()).map(String::from).collect::<Vec<_>>())
+ .map(|arr| {
+ arr.iter()
+ .filter_map(|v| v.as_str())
+ .map(String::from)
+ .collect::<Vec<_>>()
+ })
.unwrap_or_default())
})
.map_err(|e| e.to_string())?;
@@ -158,13 +181,16 @@ fn spawn_sidecar(app: &tauri::AppHandle) -> Result<CommandChild, String> {
Ok(store
.get("includedWindows")
.and_then(|v| v.as_array())
- .map(|arr| arr.iter().filter_map(|v| v.as_str()).map(String::from).collect::<Vec<_>>())
+ .map(|arr| {
+ arr.iter()
+ .filter_map(|v| v.as_str())
+ .map(String::from)
+ .collect::<Vec<_>>()
+ })
.unwrap_or_default())
})
.map_err(|e| e.to_string())?;
-
-
let deepgram_api_key = with_store(app.clone(), stores.clone(), path.clone(), |store| {
Ok(store
.get("deepgramApiKey")
@@ -174,10 +200,7 @@ fn spawn_sidecar(app: &tauri::AppHandle) -> Result<CommandChild, String> {
.unwrap_or(String::from("default"));
let fps = with_store(app.clone(), stores.clone(), path.clone(), |store| {
- Ok(store
- .get("fps")
- .and_then(|v| v.as_f64())
- .unwrap_or(0.2))
+ Ok(store.get("fps").and_then(|v| v.as_f64()).unwrap_or(0.2))
})
.map_err(|e| e.to_string())?;
@@ -325,7 +348,9 @@ fn spawn_sidecar(app: &tauri::AppHandle) -> Result<CommandChild, String> {
CommandEvent::Stderr(line) => {
let log_line = String::from_utf8(line).unwrap();
error!("Sidecar stderr: {}", log_line);
- app_handle.emit("sidecar_log", format!("ERROR: {}", log_line)).unwrap();
+ app_handle
+ .emit("sidecar_log", format!("ERROR: {}", log_line))
+ .unwrap();
}
_ => {}
}
@@ -395,8 +420,6 @@ impl SidecarManager {
}
}));
-
-
Ok(())
}