Skip to content

Commit

Permalink
fix(shutdown): fix shutdown logic (#800)
Browse files Browse the repository at this point in the history
* fix shutdown logic
* simplify
  • Loading branch information
Alex6323 authored Oct 11, 2022
1 parent 887a32c commit 3af58ea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/bin/inx-chronicle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async fn main() -> Result<(), Error> {

// We wait for either the interrupt signal to arrive or for a component of our system to signal a shutdown.
tokio::select! {
_ = process::interupt_or_terminate() => {
_ = process::interrupt_or_terminate() => {
tracing::info!("received ctrl-c or terminate");
},
res = tasks.join_next() => {
Expand All @@ -135,7 +135,7 @@ async fn main() -> Result<(), Error> {

// Allow the user to abort if the tasks aren't shutting down quickly.
tokio::select! {
_ = process::interupt_or_terminate() => {
_ = process::interrupt_or_terminate() => {
tracing::info!("received second ctrl-c or terminate - aborting");
tasks.shutdown().await;
tracing::info!("Abort successful");
Expand Down
34 changes: 11 additions & 23 deletions src/bin/inx-chronicle/process.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use tokio::{
signal::unix::{signal, SignalKind},
sync::mpsc,
};
use tokio::signal::unix::{signal, SignalKind};

pub async fn interupt_or_terminate() -> mpsc::UnboundedReceiver<()> {
let (shutdown_send, shutdown_recv) = mpsc::unbounded_channel();
pub async fn interrupt_or_terminate() {
let mut sigterm = signal(SignalKind::terminate()).expect("cannot listen to `SIGTERM`");
let mut sigint = signal(SignalKind::interrupt()).expect("cannot listen to `SIGINT`");

let _ = tokio::spawn(async move {
let mut sigterm = signal(SignalKind::terminate()).expect("cannot listen to `SIGTERM`");
let mut sigint = signal(SignalKind::interrupt()).expect("cannot listen to `SIGINT`");

tokio::select! {
_ = sigterm.recv() => {
tracing::info!("received `SIGTERM`, sending shutdown signal")
}
_ = sigint.recv() => {
tracing::info!("received `SIGTERM`, sending shutdown signal")
}
tokio::select! {
_ = sigterm.recv() => {
tracing::info!("received `SIGTERM`, sending shutdown signal")
}

shutdown_send.send(()).expect("Could not send shutdown signal");
})
.await;

shutdown_recv
_ = sigint.recv() => {
tracing::info!("received `SIGTERM`, sending shutdown signal")
}
}
}

0 comments on commit 3af58ea

Please sign in to comment.