Skip to content

Commit

Permalink
Handle SIGTERM for the docker containers + relay (paritytech#1735)
Browse files Browse the repository at this point in the history
* Handle SIGTERM for some docker containers

* Implement SIGTERM handling for the relay
  • Loading branch information
serban300 authored Dec 28, 2022
1 parent 810ca44 commit 6f525bf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
2 changes: 2 additions & 0 deletions bridges/relays/bin-substrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 34 additions & 2 deletions bridges/relays/bin-substrate/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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()
Expand Down Expand Up @@ -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?,
Expand All @@ -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! {
Expand Down
5 changes: 1 addition & 4 deletions bridges/relays/bin-substrate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 6f525bf

Please sign in to comment.