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.
This commit is contained in:
Waffle 2021-07-21 15:53:52 +03:00
parent 58d87437ba
commit b984464a79
2 changed files with 31 additions and 11 deletions

View file

@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [unreleased] ## [unreleased]
### Changed
- Improved log messages when `^C` is received with `^C` handler setted up
## [0.5.0] - 2021-07-08 ## [0.5.0] - 2021-07-08
### Added ### Added

View file

@ -113,10 +113,18 @@ where
loop { loop {
tokio::signal::ctrl_c().await.expect("Failed to listen for ^C"); tokio::signal::ctrl_c().await.expect("Failed to listen for ^C");
log::info!("^C received, trying to shutdown the dispatcher..."); match shutdown_inner(&state) {
Ok(()) => log::info!("^C received, trying to shutdown the dispatcher..."),
// If dispatcher wasn't running, then there is nothing to do Err(Ok(AlreadyShuttingDown)) => {
shutdown_inner(&state).ok(); 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 /// If you don't need to wait for shutdown, the returned future can be
/// ignored. /// ignored.
pub fn shutdown(&self) -> Result<impl Future<Output = ()> + '_, IdleShutdownError> { pub fn shutdown(&self) -> Result<impl Future<Output = ()> + '_, IdleShutdownError> {
shutdown_inner(&self.dispatcher_state).map(|()| async move { match shutdown_inner(&self.dispatcher_state) {
log::info!("Trying to shutdown the dispatcher..."); Ok(()) | Err(Ok(AlreadyShuttingDown)) => Ok(async move {
self.shutdown_notify_back.notified().await 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<E>(update_listener: &impl UpdateListener<E>) -> Du
shutdown_check_timeout.checked_add(MIN_SHUTDOWN_CHECK_TIMEOUT).unwrap_or(shutdown_check_timeout) 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<AlreadyShuttingDown, IdleShutdownError>> {
use ShutdownState::*; use ShutdownState::*;
let res = state.compare_exchange(Running, ShuttingDown); let res = state.compare_exchange(Running, ShuttingDown);
match res { match res {
Ok(_) | Err(ShuttingDown) => Ok(()), Ok(_) => Ok(()),
Err(Idle) => Err(IdleShutdownError), Err(ShuttingDown) => Err(Ok(AlreadyShuttingDown)),
Err(Idle) => Err(Err(IdleShutdownError)),
Err(Running) => unreachable!(), Err(Running) => unreachable!(),
} }
} }