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

Shutdown handling/signal example #14

Closed
fmorency opened this issue Mar 22, 2022 · 6 comments · Fixed by #30
Closed

Shutdown handling/signal example #14

fmorency opened this issue Mar 22, 2022 · 6 comments · Fixed by #30
Assignees

Comments

@fmorency
Copy link

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
Copy link
Owner

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

@mvniekerk mvniekerk self-assigned this Apr 3, 2022
@dialtone
Copy link

yeah this is hitting me as well

@tuck182
Copy link
Contributor

tuck182 commented Jun 23, 2022

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:

tuck182@10ac05f

@mvniekerk
Copy link
Owner

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

@dialtone
Copy link

So, could this be fixed?

@tuck182
Copy link
Contributor

tuck182 commented Jul 14, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants