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

fix: tray icon not changing on failed health check (#1761)

Divanshu Grover7bdb6b88f43a
diff --git a/screenpipe-app-tauri/src-tauri/src/health.rs b/screenpipe-app-tauri/src-tauri/src/health.rs
index 08624def2..1a7ca1c2f 100644
--- a/screenpipe-app-tauri/src-tauri/src/health.rs
+++ b/screenpipe-app-tauri/src-tauri/src/health.rs
@@ -1,6 +1,6 @@
use anyhow::Result;
-use serde::Deserialize;
use dark_light::Mode;
+use serde::Deserialize;
use tauri::{path::BaseDirectory, Manager};
use tokio::time::{interval, Duration};
@@ -22,6 +22,8 @@ struct HealthCheckResponse {
verbose_instructions: Option<String>,
}
+/// Starts a background task that periodically checks the health of the sidecar
+/// and updates the tray icon accordingly.
pub async fn start_health_check(app: tauri::AppHandle) -> Result<()> {
let mut interval = interval(Duration::from_secs(1));
let client = reqwest::Client::new();
@@ -35,7 +37,9 @@ pub async fn start_health_check(app: tauri::AppHandle) -> Result<()> {
let theme = dark_light::detect().unwrap_or(Mode::Dark); // Get current theme
let health_result = check_health(&client).await;
let current_status = match health_result {
- Ok(health) if health.status == "unhealthy" || health.status == "error" => "unhealthy",
+ Ok(health) if health.status == "unhealthy" || health.status == "error" => {
+ "unhealthy"
+ }
Ok(_) => "healthy",
Err(_) => "error",
};
@@ -46,7 +50,7 @@ pub async fn start_health_check(app: tauri::AppHandle) -> Result<()> {
last_theme = theme;
if let Some(main_tray) = app.tray_by_id("screenpipe_main") {
- let icon_path = if current_status == "unhealthy" {
+ let icon_path = if current_status == "unhealthy" || current_status == "error" {
if theme == Mode::Light {
"assets/screenpipe-logo-tray-black-failed.png"
} else {
@@ -60,7 +64,8 @@ pub async fn start_health_check(app: tauri::AppHandle) -> Result<()> {
}
};
- let icon_path = app.path()
+ let icon_path = app
+ .path()
.resolve(icon_path, BaseDirectory::Resource)
.expect("failed to resolve icon path");
@@ -75,20 +80,32 @@ pub async fn start_health_check(app: tauri::AppHandle) -> Result<()> {
Ok(())
}
-
+/// Checks the health of the sidecar by making a request to its health endpoint.
+/// Returns an error if the sidecar is not running or not responding.
async fn check_health(client: &reqwest::Client) -> Result<HealthCheckResponse> {
- let response = client
+ match client
.get("http://localhost:3030/health")
.header("Cache-Control", "no-cache")
.header("Pragma", "no-cache")
.timeout(Duration::from_secs(5)) // on windows it never times out
.send()
- .await?;
-
- if !response.status().is_success() {
- anyhow::bail!("health check failed with status: {}", response.status());
+ .await
+ {
+ Ok(response) if response.status().is_success() => response
+ .json::<HealthCheckResponse>()
+ .await
+ .map_err(|e| anyhow::anyhow!("failed to parse sidecar response: {}", e)),
+ Ok(response) => {
+ anyhow::bail!("health check failed with status: {}", response.status())
+ }
+ Err(e) if e.is_timeout() => {
+ anyhow::bail!("health check timeout, sidecar may not be running")
+ }
+ Err(e) if e.is_connect() => {
+ anyhow::bail!("sidecar connection refused, it may not be running")
+ }
+ Err(e) => {
+ anyhow::bail!("sidecar health check error: {}", e)
+ }
}
-
- let health = response.json::<HealthCheckResponse>().await?;
- Ok(health)
}