mvniekerk / tokio-cron-scheduler

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

Shutdown on signal feature only works on unix #36

Closed jupyterkat closed 7 months ago

mvniekerk commented 1 year ago

Hi @jupyterkat Thanks for the report! I unfortunately don't have access to a Windows machine (Linux/Mac only). I'm leveraging off Tokio's signal feature. Can you perhaps check with upstream Tokio if the signal works on your platform?

jupyterkat commented 1 year ago

The unix module you're using just straight up does not exist on windows compiles. I think you've gotta use a different one for it

beeb commented 1 year ago

I'm afraid the feature doesn't work on linux for me. I enabled the signal feature, but when I hit C-c nothing happens, and I need to manually kill the process.

            let mut sched = JobScheduler::new().await?;
            let job = Job::new_async(schedule, move |_, _| {
                Box::pin(async move {
                   // do something
                })
            })?;
            sched.add(job).await?;
            sched.shutdown_on_ctrl_c();
            sched.set_shutdown_handler(Box::new(|| {
                Box::pin(async move {
                    println!("Shut down done");
                })
            }));
            sched.start().await?;
            // run forever
            loop {
                tokio::time::sleep(Duration::from_millis(500)).await;
            }
edsky commented 1 year ago

You can use use tokio::signal::windows::ctrl_c; in Tokio to implement the functionality of exiting the program with Ctrl-C.

Here's an example:

let mut signal = ctrl_c()?;
// do other things
signal.recv().await;
// stop
sched.shutdown().await?;