Skip to content

Commit 19e46e9

Browse files
authored
Add restart policy + exit on consecutive health failures (#89)
* Add restart policy + exit on consecutive health failures * store last event time * change to 30 second interval instead of 3 failed attempts
1 parent 3d4c33c commit 19e46e9

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

backend/src/lib/server.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ use super::serializable_event::SerializableEventData;
2929
/// Stores the Unix timestamp (in seconds) of the last event received from the ring
3030
type LastEventTime = Arc<AtomicU64>;
3131

32+
/// Seconds without events before health check reports unhealthy
33+
const UNHEALTHY_THRESHOLD_SECS: u64 = 10;
34+
35+
/// Seconds without events before triggering process exit
36+
const EXIT_THRESHOLD_SECS: u64 = 30;
37+
3238
#[derive(Debug, Clone, Serialize, Deserialize)]
3339
pub struct TopAccessesData {
3440
pub account: Vec<AccessEntry<Address>>,
@@ -368,13 +374,28 @@ async fn health_handler(
368374
.unwrap_or_default()
369375
.as_secs();
370376
let last_event = last_event_time.load(Ordering::Relaxed);
371-
let is_healthy = now_secs.saturating_sub(last_event) <= 10;
377+
let time_since_last_event = now_secs.saturating_sub(last_event);
378+
379+
// Exit process if no events received for EXIT_THRESHOLD_SECS
380+
if time_since_last_event >= EXIT_THRESHOLD_SECS {
381+
error!(
382+
"No events received for {} seconds (threshold: {}), exiting to trigger restart",
383+
time_since_last_event,
384+
EXIT_THRESHOLD_SECS
385+
);
386+
std::process::exit(1);
387+
}
388+
389+
let is_healthy = time_since_last_event <= UNHEALTHY_THRESHOLD_SECS;
372390

373391
let body = if is_healthy {
374392
info!("Health check passed");
375393
r#"{"success": true}"#
376394
} else {
377-
warn!("Health check failed - last event time: {} seconds ago", now_secs.saturating_sub(last_event));
395+
warn!(
396+
"Health check failed - last event time: {} seconds ago",
397+
time_since_last_event
398+
);
378399
r#"{"success": false}"#
379400
};
380401

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ services:
77
dockerfile: Dockerfile
88
container_name: backend
99
network_mode: host
10+
restart: always
1011
environment:
1112
- RUST_LOG=info
1213
volumes:

0 commit comments

Comments
 (0)