Skip to main content

Module shutdown

Module shutdown 

Source
Expand description

Helper module that provides structures to ease the quitting process when having multiple tokio tasks.

It defines a ShutdownController, that permits to keep track of ongoing tasks, ask them to shutdown and wait for all of them to quit.

You can have it as an attribute of your plugin struct.

struct MyPlugin {
  shutdown: ShutdownController
}

You can then give a ShutdownToken to other tasks when creating them:

impl PluginInfo for MyPlugin {
    async fn start(&mut self) -> RemoteResult<()> {
        let token = self.shutdown.token();

        tokio::spawn(async move {
           token.wait().await;
           eprintln!("DEBUG shutdown asked to quit, now quitting")
        })
    }
}

On closing, calling ShutdownController::ask_shutdown will inform all tasks waiting on ShutdownToken::wait that it’s time to leave. Then we can wait for ShutdownController::wait_all_task_shutdown to complete.

impl PluginInfo for MyPlugin {
    async fn close(self) -> RemoteResult<()> {
        self.shutdown.ask_shutdown();
        self.shutdown.wait_all_task_shutdown().await;
        Ok(())
    }
}

ShutdownDelegate::handle_quit_signals permits to handle SIGHUP, SIGINT and SIGTERM by gracefully shutting down tasks.

Structs§

ShutdownController
Permits to keep track of ongoing tasks, ask them to shutdown and wait for all of them to quit. Stupid wrapper around tokio_util::sync::CancellationToken and tokio_util::task::task_tracker::TaskTracker.
ShutdownDelegate
Permits to ask for shutdown, without counting as a task that needs to be awaited.
ShutdownToken
Created by a ShutdownController. Serves two purposes: