Supervisor: gateway and API restart counters are forgotten after a stable period (a service that runs for weeks is not ended by the sixth crash); test that a killed API or gateway is restarted in place

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-11-02 21:17:37 +01:00
co-authored by Claude Sonnet 5.5
parent 61a0eb7375
commit b70538dd3f
2 changed files with 45 additions and 0 deletions
+13
View File
@@ -805,7 +805,11 @@ impl Supervisor {
self.paths.logs().display()
));
let mut ready_since = Instant::now();
// Gateway and API restarts in a row; like the engine's they are forgotten after a stable period, so that a service that
// runs for weeks is not ended by the sixth API crash of the month.
let (mut gateway_restarts, mut api_restarts) = (0u32, 0u32);
let (mut gateway_since, mut api_since) = (Instant::now(), Instant::now());
let stable = Duration::from_secs(self.config.restart.stable_after_s);
// `leleka reconfigure` leaves a request file; look for it once a second.
let mut requests = tokio::time::interval(Duration::from_secs(1));
requests.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
@@ -870,6 +874,7 @@ impl Supervisor {
self.log.line(&format!("health: {why}; restarting the API"));
self.restarts_total += 1;
self.last_restart = Some((unix_now(), why));
api_since = Instant::now();
match self.restart_api(&mut api).await {
Ok(()) => watchdog.reset(),
Err(Flow::Stopped) => break Ok(()),
@@ -891,6 +896,9 @@ impl Supervisor {
}
status = gateway.wait() => {
let text = status.map_or_else(|e| e.to_string(), |s| s.to_string());
if gateway_since.elapsed() >= stable {
gateway_restarts = 0;
}
gateway_restarts += 1;
self.log.line(&format!("gateway exited ({text}); restart {gateway_restarts}/3"));
if gateway_restarts > 3 {
@@ -901,9 +909,13 @@ impl Supervisor {
}
gateway = self.spawn("gateway", &self.paths.bin("leleka-daemon"), &daemon_args, &BTreeMap::new(), None)?;
self.pids[1] = gateway.id();
gateway_since = Instant::now();
}
status = api.wait() => {
let text = status.map_or_else(|e| e.to_string(), |s| s.to_string());
if api_since.elapsed() >= stable {
api_restarts = 0;
}
api_restarts += 1;
self.log.line(&format!("API exited ({text}); restart {api_restarts}/5"));
if api_restarts > 5 {
@@ -914,6 +926,7 @@ impl Supervisor {
}
api = self.spawn("api", &self.paths.bin("strata-next"), &api_args, &BTreeMap::new(), None)?;
self.pids[2] = api.id();
api_since = Instant::now();
}
}
};
+32
View File
@@ -525,6 +525,38 @@ fn verify_quick_reports_the_health_of_the_installation_without_reading_the_model
assert!(wait_exit(&mut supervisor, Duration::from_secs(60)).unwrap().success());
}
#[test]
fn a_killed_api_or_gateway_is_restarted_in_place_and_the_engine_is_left_alone() {
let fixture = Fixture::new("sidecars");
fixture.install_fake_binaries(&fixture.dirs.prefix.join("bin"));
fs::create_dir_all(fixture.fake_dir()).unwrap();
let config = fixture.write_state(file_profile());
fs::create_dir_all(&fixture.dirs.assets).unwrap();
fs::write(fixture.dirs.paths().kar(), b"archive").unwrap();
tune(&config, |c| c["health"]["enabled"] = serde_json::json!(false));
let mut supervisor = start_supervisor(&fixture, &config, None);
let ready = fixture.wait_for("ready", Duration::from_secs(60), |s| s["phase"] == "ready");
let (engine, gateway, api) = (
ready["engine_pid"].as_u64().unwrap(),
ready["gateway_pid"].as_u64().unwrap(),
ready["api_pid"].as_u64().unwrap(),
);
signal(api, "KILL");
let after_api = fixture.wait_for("a new API", Duration::from_secs(30), |s| s["api_pid"].as_u64().is_some_and(|p| p != api) && s["phase"] == "ready");
assert_eq!(after_api["engine_pid"].as_u64(), Some(engine));
assert_eq!(after_api["restarts_total"], 0, "an API restart is not an engine restart");
signal(gateway, "KILL");
let after_gateway = fixture.wait_for("a new gateway", Duration::from_secs(30), |s| s["gateway_pid"].as_u64().is_some_and(|p| p != gateway));
assert_eq!(after_gateway["engine_pid"].as_u64(), Some(engine));
let started = Instant::now();
while fixture.http_status("/ready") != 200 && started.elapsed() < Duration::from_secs(30) {
std::thread::sleep(Duration::from_millis(200));
}
assert_eq!(fixture.http_status("/ready"), 200);
signal(u64::from(supervisor.id()), "TERM");
assert!(wait_exit(&mut supervisor, Duration::from_secs(60)).unwrap().success());
}
#[test]
fn an_engine_that_cannot_start_fails_the_service_and_names_the_log() {
let fixture = Fixture::new("fail");