Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main: handle termination signals #152

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 30 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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 All @@ -36,41 +37,62 @@
.unwrap_or_exit()
.0;

let data_dir =
create_data_dir().map_err(|e| println!("Error creating LNDK's data dir {e:?}"))?;
setup_logger(config.log_level, config.log_dir)?;

Check warning on line 43 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L40-L43

Added lines #L40 - L43 were not covered by tests
let creds = validate_lnd_creds(
config.cert_path,
config.cert_pem,
config.macaroon_path,
config.macaroon_hex,
)
.map_err(|e| {
println!("Error validating config: {e}.");
error!("Error validating config: {e}.");

Check warning on line 51 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L51

Added line #L51 was not covered by tests
})?;
let address = config.address.clone();
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();
}
}
});
Comment on lines +67 to +83
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add an issue to get integration tests for this up? I can look into it then.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had actually already gotten pretty far into writing a basic integration test for this, before realizing it was probably too big for this PR heh. I'll either put it up as a draft or finish it off. But I have some further integration test ideas I'll post as issues today, if you happen to be interested.


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 {
eprintln!("Error: response_invoice_timeout must be more than 0 seconds.");
error!("Error: response_invoice_timeout must be more than 0 seconds.");

Check warning on line 88 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L88

Added line #L88 was not covered by tests
exit(1);
}
}

let handler = Arc::new(OfferHandler::new(config.response_invoice_timeout));
let messenger = LndkOnionMessenger::new();

let data_dir =
create_data_dir().map_err(|e| println!("Error creating LNDK's data dir {e:?}"))?;
setup_logger(config.log_level, config.log_dir)?;

let mut client = get_lnd_client(args.lnd.clone()).expect("failed to connect to lnd");
let info = client
.lightning()
Expand Down Expand Up @@ -113,7 +135,7 @@
.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
Loading