mvniekerk / tokio-cron-scheduler

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

Shutdown handling/signal example #14

Closed fmorency closed 2 years ago

fmorency commented 2 years ago

Hi tokio-cron-schedule team! Thanks a lot for making this crate!

I am currently trying to handle a graceful shutdown of my application, but I'm having a hard time making it work properly.

The following application never stops, even after catching the Ctrl-C, unless I uncomment the std::process::exit() line. I don't understand why.

Thanks!

use tokio_cron_scheduler::{Job, JobScheduler};

#[tokio::main]
async fn main() {
    let mut sched = JobScheduler::new();

    let _ = sched.add(
        Job::new_async("1/7 * * * * *", |_uuid, _l| {
            Box::pin(async {
                println!("I run async every 7 seconds");
            })
        })
        .unwrap(),
    );

    sched.shutdown_on_ctrl_c();
    let _ = sched.set_shutdown_handler(Box::new(|| {
        Box::pin(async move {
            println!("Shut down done");
            // std::process.exit(0); // This is needed for the application to shutdown
        })
    }));

    let _ = sched.start().await;
}
mvniekerk commented 2 years ago

Hi @fmorency Thanks for the report. I'll check into it

dialtone commented 2 years ago

yeah this is hitting me as well

tuck182 commented 2 years ago

I ran into this also. The shutdown mechanism seems to block (in my debugging, it seemed to be related to not properly starting all its tokio tasks). I fixed it on my fork by simplifying the shutdown method and making it async, but I imagine that doesn't meet the design you're going for:

https://github.com/tuck182/tokio-cron-scheduler/commit/10ac05fe7e6515a782ba74aac438c10de25f8e4a

mvniekerk commented 2 years ago

Hey @tuck182 that'll work, please open a PR?

dialtone commented 2 years ago

So, could this be fixed?

tuck182 commented 2 years ago

I've been meaning to create a PR; I'll do that this weekend