mvniekerk / tokio-cron-scheduler

Schedule tasks on Tokio using cron-like annotation
Apache License 2.0
473 stars 58 forks source link

Should have a way to deal with graceful shutdown #2

Closed dialtone closed 2 years ago

dialtone commented 3 years ago

This crate should use tokio::signal::ctrl_c() to deal with graceful shutdown, for example you could change the JobScheduler::start() function like this:

async fn run_scheduler(sched: &JobScheduler) {
    let jl = sched.clone();

    let interrupt = tokio::signal::ctrl_c().fuse();
    let jh = tokio::spawn(async move {
        loop {
            tokio::time::sleep(core::time::Duration::from_millis(500)).await;
            let mut jsl = jl.clone();
            let tick = jsl.tick();
            if let Err(e) = tick {
                eprintln!("Error on job scheduler tick {:?}", e);
                break;
            }
        }
    })
    .fuse();

    pin_mut!(interrupt, jh);
    select!(
        _r = jh => {},
        res = interrupt => {
            if let Err(e) = res {
                tracing::error!("couldn't bind to ctrl+c {:?}", e);
            }
        }
    );
}
mvniekerk commented 3 years ago

Hi @dialtone Thank you for the report. Will be investigating and thinking of something how to do it.

sp1ff commented 2 years ago

I'd be interested in a general mechanism for graceful shutdown (not just Ctrl-C). hyper offers a mechanism where you can give a service a future whose resolution signals the server to shutdown.

mvniekerk commented 2 years ago

Hi @dialtone and @sp1ff . Please check out v0.5 - this should have it now. Note also the signal feature flag that will allow the system to be notified on a ctrl+c event.

sp1ff commented 2 years ago

@mvniekerk I'm confused. If I call let h = start().unwrap(); I get a JoinHandle to a spawned task. Cool. When I call shutdown() I see the tasks are removed, but the future never completes. If I then call join!(h) it just blocks forever. Is that intentional?