-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shutdown): handle SIGINT/SIGTERM signals
Gracefully shut down all the active tasks when SIGINT or SIGTERM signals are received. Signed-off-by: Riccardo Gallo <[email protected]>
- Loading branch information
Showing
5 changed files
with
82 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ | |
pub mod astarte; | ||
pub mod cli; | ||
pub mod math; | ||
pub mod shutdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// This file is part of Astarte. | ||
// | ||
// Copyright 2024 SECO Mind Srl | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! Define shutdown futures to stop the docker container with CTRL+C command | ||
use color_eyre::eyre; | ||
use color_eyre::eyre::WrapErr; | ||
use tracing::error; | ||
|
||
#[cfg(unix)] | ||
/// Shut down the application in case a SIGTERM or SIGINT is received. | ||
pub fn shutdown() -> eyre::Result<impl std::future::Future<Output = ()>> { | ||
use futures::FutureExt; | ||
use tokio::signal::unix::SignalKind; | ||
|
||
let mut term = tokio::signal::unix::signal(SignalKind::terminate()) | ||
.wrap_err("couldn't create SIGTERM listener")?; | ||
|
||
let future = async move { | ||
let term = std::pin::pin!(async move { | ||
if term.recv().await.is_none() { | ||
error!("no more signal events can be received") | ||
} | ||
}); | ||
|
||
let ctrl_c = std::pin::pin!(tokio::signal::ctrl_c().map(|res| { | ||
if let Err(err) = res { | ||
error!("couldn't receive SIGINT {err}"); | ||
} | ||
})); | ||
|
||
futures::future::select(term, ctrl_c).await; | ||
}; | ||
|
||
Ok(future) | ||
} | ||
|
||
#[cfg(not(unix))] | ||
/// Shut down the application in case a SIGINT is received. | ||
pub fn shutdown() -> eyre::Result<impl std::future::Future<Output = ()>> { | ||
use futures::FutureExt; | ||
|
||
Ok(tokio::signal::ctrl_c().map(|res| { | ||
if let Err(err) = res { | ||
error!("couldn't receive SIGINT {err}"); | ||
} | ||
})) | ||
} |