Skip to content

Commit 6bb26fe

Browse files
authored
Handle SIGTERM for graceful shutdown (#61)
1 parent c60f4d6 commit 6bb26fe

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/indexing_app.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use crate::{
1212
};
1313
use anyhow::{Context, Result};
1414
use std::sync::Arc;
15-
use tokio::{signal::ctrl_c, sync::mpsc};
15+
use tokio::{
16+
signal::{self, ctrl_c},
17+
sync::mpsc,
18+
};
1619
use tracing::{error, info, warn};
1720

1821
pub struct IndexingApp {
@@ -142,10 +145,10 @@ impl IndexingApp {
142145
let _ = tokio::spawn(async move { event_collector_runner.run().await });
143146

144147
// Spawn a task that handles Ctrl+C and aborts the collector
145-
let ctrl_c_task = IndexingApp::spawn_ctrl_c_handler(self.cancellation_token.clone());
148+
let kill_task = IndexingApp::spawn_kill_handler(self.cancellation_token.clone());
146149

147-
// Wait for Ctrl+C handler
148-
ctrl_c_task.await?;
150+
// Wait for kill handler
151+
kill_task.await?;
149152

150153
// Wait for all processors to finish
151154
for handle in processor_handles {
@@ -157,10 +160,20 @@ impl IndexingApp {
157160
Ok(())
158161
}
159162

160-
fn spawn_ctrl_c_handler(cancellation_token: CancellationToken) -> tokio::task::JoinHandle<()> {
163+
fn spawn_kill_handler(cancellation_token: CancellationToken) -> tokio::task::JoinHandle<()> {
161164
tokio::spawn(async move {
162-
ctrl_c().await.ok();
163-
warn!("Received Ctrl+C, shutting down gracefully...");
165+
let mut sigterm = signal::unix::signal(signal::unix::SignalKind::terminate())
166+
.expect("Failed to create SIGTERM signal");
167+
168+
tokio::select! {
169+
_ = ctrl_c() => {
170+
warn!("Received Ctrl+C, shutting down gracefully...");
171+
172+
}
173+
_ = sigterm.recv() => {
174+
warn!("Received SIGTERM, shutting down gracefully...");
175+
}
176+
}
164177
// Signal cancellation to processor
165178
cancellation_token.graceful_shutdown();
166179
})

0 commit comments

Comments
 (0)