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
//! Graceful shutdown handling for JACS.
//!
//! This module provides utilities for graceful shutdown of JACS applications,
//! ensuring all resources are properly cleaned up.
//!
//! # Resource Cleanup
//!
//! JACS manages several resources that require proper cleanup:
//!
//! ## Cryptographic Keys (Automatic via Drop)
//! Private key material is wrapped in [`crate::crypt::private_key::ZeroizingVec`],
//! which implements `Drop` to securely zero memory when the key goes out of scope.
//! This happens automatically and requires no explicit shutdown handling.
//!
//! ## Observability Resources
//! The observability subsystem (logging, metrics, tracing) uses background workers
//! that need to flush pending data on shutdown. Use [`shutdown`] to ensure all
//! telemetry is properly exported.
//!
//! ## Storage
//! Storage backends (filesystem, S3, HTTP) use synchronous operations and don't
//! require explicit cleanup. File handles are closed when their owning structs
//! are dropped.
//!
//! # Example
//!
//! ```rust,ignore
//! use jacs::shutdown;
//!
//! fn main() {
//! // Application setup and execution...
//!
//! // On application exit, call shutdown to ensure clean termination
//! shutdown::shutdown();
//! }
//! ```
//!
//! # Signal Handling
//!
//! For CLI applications, install a signal handler to catch SIGINT/SIGTERM:
//!
//! ```rust,ignore
//! use jacs::shutdown;
//!
//! fn main() {
//! // Install signal handler for graceful shutdown
//! shutdown::install_signal_handler();
//!
//! // Application logic...
//! }
//! ```
use ;
/// Global flag indicating shutdown has been requested.
static SHUTDOWN_REQUESTED: AtomicBool = new;
/// Check if shutdown has been requested (e.g., via signal handler).
///
/// Long-running operations can check this flag to exit early when
/// a graceful shutdown is in progress.
///
/// # Example
/// ```rust,ignore
/// while !shutdown::is_shutdown_requested() {
/// // Process work...
/// }
/// ```
/// Request shutdown. This sets the global shutdown flag.
///
/// This is typically called by signal handlers or when the application
/// needs to initiate a graceful shutdown.
/// Perform graceful shutdown of all JACS resources.
///
/// This function:
/// 1. Resets observability (flushes logs, exports pending metrics)
/// 2. Allows background workers time to complete
///
/// Cryptographic key cleanup happens automatically via Drop implementations
/// and does not require explicit calls.
///
/// # Example
/// ```rust,ignore
/// fn main() {
/// // ... application logic ...
///
/// // On exit, ensure clean shutdown
/// jacs::shutdown::shutdown();
/// }
/// ```
/// Install a signal handler for graceful shutdown on Unix systems.
///
/// This installs handlers for:
/// - SIGINT (Ctrl+C)
/// - SIGTERM (kill command)
///
/// When a signal is received, the handler:
/// 1. Sets the shutdown requested flag
/// 2. Calls [`shutdown`] to clean up resources
/// 3. Exits the process with code 0
///
/// # Platform Support
/// - Unix: Full signal handling with SIGINT and SIGTERM
/// - Windows: Only Ctrl+C handling via ctrlc behavior
/// - WASM: No-op (signals not applicable)
///
/// # Example
/// ```rust,ignore
/// fn main() {
/// jacs::shutdown::install_signal_handler();
///
/// // Application logic...
/// // On Ctrl+C, graceful shutdown will occur automatically
/// }
/// ```
/// Install signal handler - Windows implementation.
/// Install signal handler - WASM stub (no-op).
/// RAII guard that performs shutdown when dropped.
///
/// Use this to ensure shutdown is called even when returning early
/// from a function due to errors.
///
/// # Example
/// ```rust,ignore
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let _guard = jacs::shutdown::ShutdownGuard::new();
///
/// // If any of these return early, shutdown still happens
/// do_something()?;
/// do_something_else()?;
///
/// Ok(())
/// // shutdown::shutdown() called automatically when _guard is dropped
/// }
/// ```