1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//! Graceful Shutdown — signal handling and orderly server termination.
//!
//! Provides:
//! - OS signal detection (Ctrl+C / SIGTERM on Unix, Ctrl+C on Windows)
//! - Programmatic shutdown trigger (via `/v1/shutdown` endpoint)
//! - `ShutdownCoordinator` — shared notify channel between signal handler, API, and server
//! - Pre-shutdown hooks: auto-save config, audit log recording
//!
//! The shutdown signal is a tokio::sync::Notify that `axum::serve` awaits
//! via `with_graceful_shutdown`. When triggered, axum stops accepting new
//! connections and drains in-flight requests before returning.
use serde::Serialize;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::Notify;
// ── Shutdown reason ─────────────────────────────────────────────────────
/// Why the server is shutting down.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ShutdownReason {
/// OS signal (Ctrl+C, SIGTERM).
Signal,
/// Programmatic shutdown via API endpoint.
Api,
}
impl ShutdownReason {
pub fn as_str(&self) -> &'static str {
match self {
ShutdownReason::Signal => "signal",
ShutdownReason::Api => "api",
}
}
}
// ── Shutdown coordinator ────────────────────────────────────────────────
/// Coordinates graceful shutdown across signal handlers, API, and server.
///
/// Shared via `Arc` between the signal listener, the `/v1/shutdown` handler,
/// and the `axum::serve(...).with_graceful_shutdown(...)` future.
pub struct ShutdownCoordinator {
notify: Notify,
triggered: AtomicBool,
started_at: Instant,
}
impl ShutdownCoordinator {
/// Create a new coordinator.
pub fn new(started_at: Instant) -> Self {
ShutdownCoordinator {
notify: Notify::new(),
triggered: AtomicBool::new(false),
started_at,
}
}
/// Trigger shutdown. Idempotent — second call is a no-op.
/// Returns true if this call was the one that triggered shutdown.
pub fn trigger(&self) -> bool {
let was_triggered = self.triggered.swap(true, Ordering::SeqCst);
if !was_triggered {
self.notify.notify_waiters();
true
} else {
false
}
}
/// Whether shutdown has been triggered.
pub fn is_triggered(&self) -> bool {
self.triggered.load(Ordering::SeqCst)
}
/// Wait for shutdown to be triggered. Resolves immediately if already triggered.
pub async fn wait(&self) {
if self.is_triggered() {
return;
}
self.notify.notified().await;
}
/// Server uptime at the moment of query.
pub fn uptime_secs(&self) -> u64 {
self.started_at.elapsed().as_secs()
}
}
// ── Shutdown status ─────────────────────────────────────────────────────
/// Status report returned by the shutdown endpoint.
#[derive(Debug, Clone, Serialize)]
pub struct ShutdownStatus {
pub initiated: bool,
pub reason: ShutdownReason,
pub uptime_secs: u64,
pub message: String,
}
// ── Signal listener ─────────────────────────────────────────────────────
/// Listen for OS shutdown signals and trigger the coordinator.
///
/// On Unix: listens for SIGTERM and SIGINT (Ctrl+C).
/// On Windows: listens for Ctrl+C.
///
/// This function is designed to be spawned as a tokio task:
/// ```ignore
/// tokio::spawn(listen_signals(coordinator.clone()));
/// ```
pub async fn listen_signals(coordinator: Arc<ShutdownCoordinator>) {
let ctrl_c = tokio::signal::ctrl_c();
#[cfg(unix)]
{
use tokio::signal::unix::{signal, SignalKind};
let mut sigterm = signal(SignalKind::terminate())
.expect("failed to install SIGTERM handler");
tokio::select! {
_ = ctrl_c => {
eprintln!("\n Received Ctrl+C, initiating graceful shutdown...");
}
_ = sigterm.recv() => {
eprintln!("\n Received SIGTERM, initiating graceful shutdown...");
}
}
}
#[cfg(not(unix))]
{
let _ = ctrl_c.await;
eprintln!("\n Received Ctrl+C, initiating graceful shutdown...");
}
coordinator.trigger();
}
// ── Pre-shutdown hooks ──────────────────────────────────────────────────
/// Actions to perform before the server fully stops.
///
/// - Auto-save config to disk (if config persistence is active).
/// - Record shutdown in audit trail.
/// - Emit shutdown event on bus.
///
/// This is called from the server launcher after axum::serve returns.
pub fn run_pre_shutdown_hooks(
state: &mut crate::axon_server::ServerState,
reason: ShutdownReason,
use_color: bool,
) {
// Record in audit trail
state.audit_log.record(
"system",
crate::audit_trail::AuditAction::ServerShutdown,
"server",
serde_json::json!({ "reason": reason.as_str(), "uptime_secs": state.started_at.elapsed().as_secs() }),
true,
);
// Emit shutdown event
state.event_bus.publish(
"server.shutdown",
serde_json::json!({ "reason": reason.as_str() }),
"system",
);
// Auto-save config if persistence path is configured
let config_path = crate::config_persistence::resolve_path(state.config.config_path.as_deref());
if crate::config_persistence::exists(&config_path) || state.config.config_path.is_some() {
let snap = crate::server_config::snapshot(
&state.rate_limiter,
&state.request_logger,
&state.api_keys,
);
let result = crate::config_persistence::save(&snap, &config_path, crate::runner::AXON_VERSION);
if result.success {
if use_color {
eprintln!("\x1b[2;36m Config auto-saved to {}\x1b[0m", result.path);
} else {
eprintln!(" Config auto-saved to {}", result.path);
}
}
}
if use_color {
eprintln!("\x1b[1;33m AxonServer stopped ({})\x1b[0m", reason.as_str());
} else {
eprintln!(" AxonServer stopped ({})", reason.as_str());
}
}
// ── Tests ────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn coordinator_trigger_idempotent() {
let coord = ShutdownCoordinator::new(Instant::now());
assert!(!coord.is_triggered());
let first = coord.trigger();
assert!(first);
assert!(coord.is_triggered());
let second = coord.trigger();
assert!(!second); // idempotent
assert!(coord.is_triggered());
}
#[test]
fn coordinator_uptime() {
let coord = ShutdownCoordinator::new(Instant::now());
// Just check it doesn't panic and returns a reasonable value
assert!(coord.uptime_secs() < 5);
}
#[test]
fn shutdown_reason_serialization() {
let signal_json = serde_json::to_value(ShutdownReason::Signal).unwrap();
assert_eq!(signal_json, "signal");
let api_json = serde_json::to_value(ShutdownReason::Api).unwrap();
assert_eq!(api_json, "api");
}
#[test]
fn shutdown_reason_as_str() {
assert_eq!(ShutdownReason::Signal.as_str(), "signal");
assert_eq!(ShutdownReason::Api.as_str(), "api");
}
#[test]
fn shutdown_status_serializable() {
let status = ShutdownStatus {
initiated: true,
reason: ShutdownReason::Api,
uptime_secs: 3600,
message: "shutting down".to_string(),
};
let json = serde_json::to_value(&status).unwrap();
assert_eq!(json["initiated"], true);
assert_eq!(json["reason"], "api");
assert_eq!(json["uptime_secs"], 3600);
assert_eq!(json["message"], "shutting down");
}
#[tokio::test]
async fn coordinator_wait_resolves_when_triggered() {
let coord = Arc::new(ShutdownCoordinator::new(Instant::now()));
let coord2 = coord.clone();
// Trigger from another task
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
coord2.trigger();
});
// Should resolve without hanging
tokio::time::timeout(std::time::Duration::from_secs(2), coord.wait())
.await
.expect("wait should resolve within timeout");
assert!(coord.is_triggered());
}
#[tokio::test]
async fn coordinator_wait_resolves_immediately_if_already_triggered() {
let coord = ShutdownCoordinator::new(Instant::now());
coord.trigger();
// Should resolve immediately
tokio::time::timeout(std::time::Duration::from_millis(50), coord.wait())
.await
.expect("wait should resolve immediately when already triggered");
}
}