film42 / sidekiq-rs

A port of sidekiq to rust using tokio
MIT License
95 stars 10 forks source link

Implement graceful shutdown using tokio cancellation tokens #38

Closed georgeboot closed 3 months ago

georgeboot commented 3 months ago

We've implemented graceful shutdown using cancellation tokens from the tokio runtime.

It allows users of this crate to optionally get the cancellation token, and send the shutdown signal to sidekiq. The implementation is such that no on-hand work will ever be cancelled. This however means that sending a SIGINT doesn't mean immediate shutdown, as currently active jobs first need to finish. As soon as the cancellation token is cancelled, no new work will be taken on by the workers.

A minimal new example for using cancellation tokens is as follows:

// Sidekiq server
let mut p = Processor::new(
    redis,
    vec!["default".to_string()],
);
p.register(DemoWorker::new());

// Retrieve the cancellation token and store it
let cancellation_token = p.get_cancellation_token();

// Start the server and get a handle to it
let handle = tokio::spawn(p.run());

match signal::ctrl_c().await {
    Ok(()) => {
        debug!("Received control-c, quitting...");
        cancellation_token.cancel();
    },
    Err(err) => {
        error!("Unable to listen for shutdown signal: {}", err);
        cancellation_token.cancel(); // still cancel?
    },
}

handle.await.unwrap();

Fixes #37

film42 commented 3 months ago

I like this. Let me take a day or so to think about it/ where else we need to cancel. (ref: https://github.com/film42/sidekiq-rs/issues/37)

georgeboot commented 3 months ago

Great, please let me know if there is anything you would like to have changed.

film42 commented 3 months ago

Thanks so much for the contribution @georgeboot ! ❤️

This was releases as v0.10.0.