New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add fast exit with double ctrlc #894
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry it took so long to look at this 💀
I don't think I like the approach here. The main problem is that it only changes anything for dispatch
, while leaving dispatch_with_listener
as-is.
I think ideally you
- Select something like
self.force_shutdown
hereteloxide/crates/teloxide/src/dispatching/dispatcher.rs
Lines 345 to 353 in c7feb38
self.workers .drain() .map(|(_chat_id, worker)| worker.handle) .chain(self.default_worker.take().map(|worker| worker.handle)) .collect::<FuturesUnordered<_>>() .for_each(|res| async { res.expect("Failed to wait for a worker."); }) .await; self.force_shutdown
is justtokio::sync::oneshot::Receiver<()>
- the send half is
self.force_shutdown_sender
, has typeOption<oneshot::Sender<()>>
and can be retrivied withself.force_shutdown_sender.take()
- in
setup_ctrlc_handler_inner
teloxide/crates/teloxide/src/dispatching/dispatcher.rs
Lines 480 to 493 in c7feb38
loop { tokio::signal::ctrl_c().await.expect("Failed to listen for ^C"); match token.shutdown() { Ok(f) => { log::info!("^C received, trying to shutdown the dispatcher..."); f.await; log::info!("dispatcher is shutdown..."); } Err(_) => { log::info!("^C received, the dispatcher isn't running, ignoring the signal") } } }
you rewrite it to something like this:// pseudo-code tokio::signal::ctrl_c().await.expect("Failed to listen for ^C"); let mut count = 1; while count <= 3 { select! { r = token.shutdown() => { match r { /* the same */ }; return }, _ = ctrl_c()... => { count += 1; continue } } } force_shutdown_sender.send(());
Implemented one of approaches discussed in #711