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 19, 2024
1 parent 88b1484 commit dd9e22a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ configure_me = "0.4.0"
bytes = "1.4.0"
triggered = "0.1.2"
prost = "0.12"
signal-hook = "0.3.17"

[dev-dependencies]
bitcoincore-rpc = { package="core-rpc", version = "0.17.0" }
Expand Down
21 changes: 19 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ use lndk::{
};
use lndkrpc::offers_server::OffersServer;
use log::{error, info};
use signal_hook::consts::{SIGINT, SIGTERM};
use signal_hook::iterator::Signals;
use std::fs::create_dir_all;
use std::path::PathBuf;
use std::process::exit;
use std::sync::Arc;
use std::thread;
use tokio::select;
use tonic::transport::{Server, ServerTlsConfig};
use tonic_lnd::lnrpc::GetInfoRequest;
Expand Down Expand Up @@ -53,13 +56,27 @@ 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 62 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L59-L62

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

let mut shutdown_signals = Signals::new([SIGINT, SIGTERM]).map_err(|e| {
error!("Error initializing shutdown signals: {e}.");
})?;

thread::spawn(move || {
if let Some(sig) = shutdown_signals.forever().next() {
info!("Received shutdown signal {:?}. Shutting down.", sig);
shutdown.trigger();
}
});

Check warning on line 79 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L69-L79

Added lines #L69 - L79 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 +130,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 133 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L133

Added line #L133 was not covered by tests

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

Expand Down

0 comments on commit dd9e22a

Please sign in to comment.