Add signal handling.

Fixes #480.
This commit is contained in:
Orne Brocaar 2024-08-13 13:04:06 +01:00
parent 3aaa114f46
commit 76b7ec4881
4 changed files with 41 additions and 2 deletions

24
Cargo.lock generated
View File

@ -881,6 +881,8 @@ dependencies = [
"serde_urlencoded",
"serde_yaml",
"sha2",
"signal-hook",
"signal-hook-tokio",
"thiserror",
"tokio",
"tokio-executor-trait",
@ -4183,6 +4185,16 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "signal-hook"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801"
dependencies = [
"libc",
"signal-hook-registry",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.2"
@ -4192,6 +4204,18 @@ dependencies = [
"libc",
]
[[package]]
name = "signal-hook-tokio"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "213241f76fb1e37e27de3b6aa1b068a2c333233b59cca6634f634b80a27ecf1e"
dependencies = [
"futures-core",
"libc",
"signal-hook",
"tokio",
]
[[package]]
name = "signature"
version = "2.2.0"

View File

@ -161,6 +161,8 @@
petgraph = "0.6"
prometheus-client = "0.22"
pin-project = "1.1"
signal-hook = "0.3"
signal-hook-tokio = { version = "0.3", features = ["futures-v0_3"] }
# Development and testing
[dev-dependencies]

View File

@ -181,7 +181,12 @@ pub async fn setup() -> Result<()> {
let monitoring_handle = tokio::spawn(monitoring::setup());
let grpc_handle = tokio::spawn(grpc.serve(bind));
let _ = try_join!(grpc_handle, backend_handle, monitoring_handle)?;
tokio::spawn(async move {
if let Err(e) = try_join!(grpc_handle, backend_handle, monitoring_handle) {
error!(error = %e, "Setup API error");
std::process::exit(-1);
}
});
Ok(())
}

View File

@ -1,5 +1,8 @@
use anyhow::Result;
use tracing::info;
use futures::stream::StreamExt;
use signal_hook::consts::signal::{SIGINT, SIGTERM};
use signal_hook_tokio::Signals;
use tracing::{info, warn};
use crate::gateway;
use crate::{adr, api, backend, downlink, integration, region, storage};
@ -20,5 +23,10 @@ pub async fn run() -> Result<()> {
downlink::setup().await;
api::setup().await?;
let mut signals = Signals::new([SIGINT, SIGTERM]).unwrap();
if let Some(signal) = signals.next().await {
warn!(signal = ?signal, "Signal received, terminating process");
}
Ok(())
}