Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Studiedlist
Copy link
Contributor

Implemented one of approaches discussed in #711

Copy link
Member

@WaffleLapkin WaffleLapkin left a 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 here
    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 just tokio::sync::oneshot::Receiver<()>
  • the send half is self.force_shutdown_sender, has type Option<oneshot::Sender<()>> and can be retrivied with self.force_shutdown_sender.take()
  • in setup_ctrlc_handler_inner
    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(());

@WaffleLapkin WaffleLapkin added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author C-main crate: teloxide labels Sep 25, 2023
@WaffleLapkin WaffleLapkin self-assigned this Sep 25, 2023
@WaffleLapkin WaffleLapkin added the A-dispatching Area: dispatching updates (`Dispatcher`, repls) label Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-dispatching Area: dispatching updates (`Dispatcher`, repls) C-main crate: teloxide S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants