diff --git a/backend/src/lib/server.rs b/backend/src/lib/server.rs index 8aa9935..a2c470c 100644 --- a/backend/src/lib/server.rs +++ b/backend/src/lib/server.rs @@ -29,6 +29,12 @@ use super::serializable_event::SerializableEventData; /// Stores the Unix timestamp (in seconds) of the last event received from the ring type LastEventTime = Arc; +/// Seconds without events before health check reports unhealthy +const UNHEALTHY_THRESHOLD_SECS: u64 = 10; + +/// Seconds without events before triggering process exit +const EXIT_THRESHOLD_SECS: u64 = 30; + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct TopAccessesData { pub account: Vec>, @@ -368,13 +374,28 @@ async fn health_handler( .unwrap_or_default() .as_secs(); let last_event = last_event_time.load(Ordering::Relaxed); - let is_healthy = now_secs.saturating_sub(last_event) <= 10; + let time_since_last_event = now_secs.saturating_sub(last_event); + + // Exit process if no events received for EXIT_THRESHOLD_SECS + if time_since_last_event >= EXIT_THRESHOLD_SECS { + error!( + "No events received for {} seconds (threshold: {}), exiting to trigger restart", + time_since_last_event, + EXIT_THRESHOLD_SECS + ); + std::process::exit(1); + } + + let is_healthy = time_since_last_event <= UNHEALTHY_THRESHOLD_SECS; let body = if is_healthy { info!("Health check passed"); r#"{"success": true}"# } else { - warn!("Health check failed - last event time: {} seconds ago", now_secs.saturating_sub(last_event)); + warn!( + "Health check failed - last event time: {} seconds ago", + time_since_last_event + ); r#"{"success": false}"# }; diff --git a/docker-compose.yml b/docker-compose.yml index 30565fb..32a424e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,6 +7,7 @@ services: dockerfile: Dockerfile container_name: backend network_mode: host + restart: always environment: - RUST_LOG=info volumes: