From b984464a79027b7b0dc8783892f6154535d16be5 Mon Sep 17 00:00:00 2001 From: Waffle Date: Wed, 21 Jul 2021 15:53:52 +0300 Subject: [PATCH] Improve log messages when `^C` is received with `^C` handler setted up Now they tell the user to not repeatedly press `^C` since only the first signal actually does anything, all subsequesnt ones are ignored. --- CHANGELOG.md | 4 ++++ src/dispatching/dispatcher.rs | 38 +++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac7d6ca8..af180059 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [unreleased] +### Changed + +- Improved log messages when `^C` is received with `^C` handler setted up + ## [0.5.0] - 2021-07-08 ### Added diff --git a/src/dispatching/dispatcher.rs b/src/dispatching/dispatcher.rs index ff603c05..d0cadeff 100644 --- a/src/dispatching/dispatcher.rs +++ b/src/dispatching/dispatcher.rs @@ -113,10 +113,18 @@ where loop { tokio::signal::ctrl_c().await.expect("Failed to listen for ^C"); - log::info!("^C received, trying to shutdown the dispatcher..."); - - // If dispatcher wasn't running, then there is nothing to do - shutdown_inner(&state).ok(); + match shutdown_inner(&state) { + Ok(()) => log::info!("^C received, trying to shutdown the dispatcher..."), + Err(Ok(AlreadyShuttingDown)) => { + log::info!( + "^C received, the dispatcher is already shutting down, ignoring the \ + signal" + ) + } + Err(Err(IdleShutdownError)) => { + log::info!("^C received, the dispatcher isn't running, ignoring the signal") + } + } } }); @@ -546,10 +554,13 @@ impl ShutdownToken { /// If you don't need to wait for shutdown, the returned future can be /// ignored. pub fn shutdown(&self) -> Result + '_, IdleShutdownError> { - shutdown_inner(&self.dispatcher_state).map(|()| async move { - log::info!("Trying to shutdown the dispatcher..."); - self.shutdown_notify_back.notified().await - }) + match shutdown_inner(&self.dispatcher_state) { + Ok(()) | Err(Ok(AlreadyShuttingDown)) => Ok(async move { + log::info!("Trying to shutdown the dispatcher..."); + self.shutdown_notify_back.notified().await + }), + Err(Err(err)) => Err(err), + } } } @@ -619,14 +630,19 @@ fn shutdown_check_timeout_for(update_listener: &impl UpdateListener) -> Du shutdown_check_timeout.checked_add(MIN_SHUTDOWN_CHECK_TIMEOUT).unwrap_or(shutdown_check_timeout) } -fn shutdown_inner(state: &DispatcherState) -> Result<(), IdleShutdownError> { +struct AlreadyShuttingDown; + +fn shutdown_inner( + state: &DispatcherState, +) -> Result<(), Result> { use ShutdownState::*; let res = state.compare_exchange(Running, ShuttingDown); match res { - Ok(_) | Err(ShuttingDown) => Ok(()), - Err(Idle) => Err(IdleShutdownError), + Ok(_) => Ok(()), + Err(ShuttingDown) => Err(Ok(AlreadyShuttingDown)), + Err(Idle) => Err(Err(IdleShutdownError)), Err(Running) => unreachable!(), } }