Skip to content

Commit

Permalink
feat: impl auto destruction + #304 and other ui things
Browse files Browse the repository at this point in the history
  • Loading branch information
louis030195 committed Sep 17, 2024
1 parent 248f714 commit 2160e3c
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 32 deletions.
2 changes: 1 addition & 1 deletion screenpipe-app-tauri/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default function Home() {
))}
</div>
</div>
) : settings.useOllama || settings.openaiApiKey ? (
) : settings.aiUrl ? (
<>
<h1 className="text-2xl font-bold mb-8 text-center mb-12">
where pixels become magic
Expand Down
2 changes: 1 addition & 1 deletion screenpipe-app-tauri/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "screenpipe-app"
version = "0.2.44"
version = "0.2.45"
description = ""
authors = ["you"]
license = ""
Expand Down
25 changes: 1 addition & 24 deletions screenpipe-app-tauri/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,30 +324,7 @@ async fn main() {
}
tauri::RunEvent::ExitRequested { .. } => {
debug!("ExitRequested event");
let app_handle_clone = app_handle.clone();
let app_handle_clone2 = app_handle.clone();
let stores = app_handle.state::<StoreCollection<Wry>>();
let path = app_handle
.path()
.local_data_dir()
.unwrap()
.join("store.bin");
let use_dev_mode = with_store(app_handle.clone(), stores, path, |store| {
Ok(store
.get("devMode")
.unwrap_or(&Value::Bool(false))
.as_bool()
.unwrap_or(false))
})
.unwrap_or(false);
if !use_dev_mode {
tauri::async_runtime::spawn(async move {
let state = app_handle_clone.state::<SidecarState>();
if let Err(e) = kill_all_sreenpipes(state, app_handle_clone2).await {
error!("Failed to kill screenpipe processes: {}", e);
}
});
}

// Add this to shut down the server
if let Some(server_shutdown_tx) = app_handle.try_state::<mpsc::Sender<()>>() {
let _ = server_shutdown_tx.send(());
Expand Down
22 changes: 19 additions & 3 deletions screenpipe-app-tauri/src-tauri/src/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,22 @@ fn spawn_sidecar(app: &tauri::AppHandle) -> Result<CommandChild, String> {
Ok(store
.get("fps")
.and_then(|v| v.as_f64())
.unwrap_or(0.5))
.unwrap_or(0.2))
})
.map_err(|e| e.to_string())?;

let dev_mode = with_store(app.clone(), stores.clone(), path.clone(), |store| {
Ok(store
.get("devMode")
.and_then(|v| v.as_bool())
.unwrap_or(false))
})
.map_err(|e| e.to_string())?;

let port_str = port.to_string();
let mut args = vec!["--port", port_str.as_str()];
let fps_str = fps.to_string();
if fps != 0.5 {
if fps != 0.2 {
args.push("--fps");
args.push(fps_str.as_str());
}
Expand Down Expand Up @@ -253,6 +261,13 @@ fn spawn_sidecar(app: &tauri::AppHandle) -> Result<CommandChild, String> {
args.push(window);
}
}
let current_pid = std::process::id();
let current_pid_str = current_pid.to_string();
// Set auto-destruct PID if not in dev mode
if !dev_mode {
args.push("--auto-destruct-pid");
args.push(current_pid_str.as_str());
}

// args.push("--debug");

Expand Down Expand Up @@ -333,7 +348,6 @@ impl SidecarManager {
debug!("last_restart: {:?}", self.last_restart);

// kill previous task if any

if let Some(task) = self.restart_task.take() {
task.abort();
}
Expand Down Expand Up @@ -363,6 +377,8 @@ impl SidecarManager {
}
}));



Ok(())
}

Expand Down
19 changes: 19 additions & 0 deletions screenpipe-server/src/auto_destruct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::process::Command;
use std::time::Duration;
use tokio::time::sleep;

pub async fn watch_pid(pid: u32) -> bool {
loop {
let output = Command::new("ps")
.arg("-p")
.arg(pid.to_string())
.output()
.expect("failed to execute ps command");

if !output.status.success() {
return true;
}

sleep(Duration::from_secs(1)).await;
}
}
16 changes: 14 additions & 2 deletions screenpipe-server/src/bin/screenpipe-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ use screenpipe_audio::{
};
use screenpipe_core::find_ffmpeg_path;
use screenpipe_server::{
cli::{Cli, CliAudioTranscriptionEngine, CliOcrEngine, Command, PipeCommand},
start_continuous_recording, DatabaseManager, PipeManager, ResourceMonitor, Server,
cli::{Cli, CliAudioTranscriptionEngine, CliOcrEngine, Command, PipeCommand}, start_continuous_recording, watch_pid, DatabaseManager, PipeManager, ResourceMonitor, Server
};
use screenpipe_vision::monitor::list_monitors;
use serde_json::{json, Value};
Expand All @@ -28,6 +27,7 @@ use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::Layer;
use tracing_subscriber::{fmt, EnvFilter};


fn print_devices(devices: &[AudioDevice]) {
println!("available audio devices:");
for device in devices.iter() {
Expand Down Expand Up @@ -558,6 +558,18 @@ async fn main() -> anyhow::Result<()> {
};
pin_mut!(pipes_future);

// Add auto-destruct watcher
if let Some(pid) = cli.auto_destruct_pid {
info!("watching pid {} for auto-destruction", pid);
let shutdown_tx_clone = shutdown_tx.clone();
tokio::spawn(async move {
if watch_pid(pid).await {
info!("watched pid {} has stopped, initiating shutdown", pid);
let _ = shutdown_tx_clone.send(());
}
});
}

let ctrl_c_future = signal::ctrl_c();
pin_mut!(ctrl_c_future);

Expand Down
6 changes: 5 additions & 1 deletion screenpipe-server/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub struct Cli {
/// Optimise based on your needs.
/// Your screen rarely change more than 1 times within a second, right?
#[cfg_attr(not(target_os = "macos"), arg(short, long, default_value_t = 1.0))]
#[cfg_attr(target_os = "macos", arg(short, long, default_value_t = 0.5))]
#[cfg_attr(target_os = "macos", arg(short, long, default_value_t = 0.2))]
pub fps: f64, // ! not crazy about this (unconsistent behaviour across platforms) see https://github.com/mediar-ai/screenpipe/issues/173

/// Audio chunk duration in seconds
Expand Down Expand Up @@ -187,6 +187,10 @@ pub struct Cli {
#[arg(long = "deepgram-api-key")]
pub deepgram_api_key: Option<String>,

/// PID to watch for auto-destruction. If provided, screenpipe will stop when this PID is no longer running.
#[arg(long)]
pub auto_destruct_pid: Option<u32>,

#[command(subcommand)]
pub command: Option<Command>,
}
Expand Down
2 changes: 2 additions & 0 deletions screenpipe-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ mod server;
mod video;
mod video_db;
mod video_utils;
mod auto_destruct;
pub use auto_destruct::watch_pid;
pub use cli::Cli;
pub use core::start_continuous_recording;
pub use db::{ContentSource, ContentType, DatabaseManager, SearchResult};
Expand Down

0 comments on commit 2160e3c

Please sign in to comment.