Skip to content

Commit

Permalink
main: handle sigterm/sigint signals
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitalturtle committed Aug 28, 2024
1 parent 88b1484 commit f94892c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ rand_core = "0.6.4"
log = "0.4.17"
log4rs = { version = "1.2.0", features = ["file_appender"] }
rcgen = { version = "0.13.1", features = ["pem", "x509-parser"] }
tokio = { version = "1.25.0", features = ["rt", "rt-multi-thread"] }
tokio = { version = "1.25.0", features = ["rt", "rt-multi-thread", "signal"] }
tonic = { version = "0.11", features = [ "tls", "transport" ] }
tonic_lnd = { git = "https://github.com/orbitalturtle/tonic_lnd", rev="18c5a71084886024a6b90307bfb8822288c5daea", package="fedimint-tonic-lnd", features = ["lightningrpc", "routerrpc", "versionrpc"] }
hex = "0.4.3"
Expand Down
26 changes: 24 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::path::PathBuf;
use std::process::exit;
use std::sync::Arc;
use tokio::select;
use tokio::signal::unix::SignalKind;
use tonic::transport::{Server, ServerTlsConfig};
use tonic_lnd::lnrpc::GetInfoRequest;

Expand Down Expand Up @@ -53,13 +54,34 @@ async fn main() -> Result<(), ()> {
let lnd_args = LndCfg::new(config.address, creds.clone());

let (shutdown, listener) = triggered::trigger();
let signals = LifecycleSignals { shutdown, listener };
let signals = LifecycleSignals {
shutdown: shutdown.clone(),
listener: listener.clone(),
};

Check warning on line 60 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L57-L60

Added lines #L57 - L60 were not covered by tests
let args = Cfg {
lnd: lnd_args,
signals,
skip_version_check: config.skip_version_check,
};

let mut sigterm_stream = tokio::signal::unix::signal(SignalKind::terminate())
.map_err(|e| error!("Error initializing sigterm signal: {e}."))?;
let mut sigint_stream = tokio::signal::unix::signal(SignalKind::interrupt())
.map_err(|e| error!("Error initializing sigint signal: {e}."))?;

tokio::spawn(async move {
tokio::select! {
_ = sigint_stream.recv() => {
info!("Received CTRL-C, shutting down..");
shutdown.trigger();
}
_ = sigterm_stream.recv() => {
info!("Received SIGTERM, shutting down..");
shutdown.trigger();
}
}
});

Check warning on line 84 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L67-L84

Added lines #L67 - L84 were not covered by tests
let response_invoice_timeout = config.response_invoice_timeout;
if let Some(timeout) = response_invoice_timeout {
if timeout == 0 {
Expand Down Expand Up @@ -113,7 +135,7 @@ async fn main() -> Result<(), ()> {
.tls_config(ServerTlsConfig::new().identity(identity))
.expect("couldn't configure tls")
.add_service(OffersServer::new(server))
.serve(addr);
.serve_with_shutdown(addr, listener);

Check warning on line 138 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L138

Added line #L138 was not covered by tests

info!("Starting lndk's grpc server at address {grpc_host}:{grpc_port}");

Expand Down

0 comments on commit f94892c

Please sign in to comment.