Handle SIGTERM for the docker containers + relay (#1735)

* Handle SIGTERM for some docker containers

* Implement SIGTERM handling for the relay
This commit is contained in:
Serban Iorga
2022-12-28 15:47:09 +02:00
committed by Bastian Köcher
parent e47f1e42e0
commit 9495e1cfcb
3 changed files with 37 additions and 6 deletions
+2
View File
@@ -16,6 +16,8 @@ log = "0.4.17"
num-format = "0.4"
num-traits = "0.2"
structopt = "0.3"
signal-hook = "0.3.14"
signal-hook-async-std = "0.2.2"
strum = { version = "0.21.0", features = ["derive"] }
# Bridge dependencies
+34 -2
View File
@@ -18,7 +18,11 @@
use std::convert::TryInto;
use async_std::prelude::*;
use codec::{Decode, Encode};
use futures::{select, FutureExt};
use signal_hook::consts::*;
use signal_hook_async_std::Signals;
use structopt::{clap::arg_enum, StructOpt};
use strum::{EnumString, EnumVariantNames};
@@ -37,6 +41,9 @@ mod relay_messages;
mod relay_parachains;
mod resubmit_transactions;
/// The target that will be used when publishing logs related to this pallet.
pub const LOG_TARGET: &str = "bridge";
/// Parse relay CLI args.
pub fn parse_args() -> Command {
Command::from_args()
@@ -100,8 +107,7 @@ impl Command {
}
/// Run the command.
pub async fn run(self) -> anyhow::Result<()> {
self.init_logger();
async fn do_run(self) -> anyhow::Result<()> {
match self {
Self::RelayHeaders(arg) => arg.run().await?,
Self::RelayMessages(arg) => arg.run().await?,
@@ -114,6 +120,32 @@ impl Command {
}
Ok(())
}
/// Run the command.
pub async fn run(self) {
self.init_logger();
let exit_signals = match Signals::new([SIGINT, SIGTERM]) {
Ok(signals) => signals,
Err(e) => {
log::error!(target: LOG_TARGET, "Could not register exit signals: {}", e);
return
},
};
let run = self.do_run().fuse();
futures::pin_mut!(exit_signals, run);
select! {
signal = exit_signals.next().fuse() => {
log::info!(target: LOG_TARGET, "Received exit signal {:?}", signal);
},
result = run => {
if let Err(e) = result {
log::error!(target: LOG_TARGET, "substrate-relay: {}", e);
}
},
}
}
}
arg_enum! {
+1 -4
View File
@@ -24,8 +24,5 @@ mod cli;
fn main() {
let command = cli::parse_args();
let run = command.run();
let result = async_std::task::block_on(run);
if let Err(error) = result {
log::error!(target: "bridge", "substrate-relay: {}", error);
}
async_std::task::block_on(run);
}