mvniekerk / tokio-cron-scheduler

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

Can there has an api to get a job next_run_time? #32

Closed Hellager closed 1 year ago

Hellager commented 1 year ago

Thanks for your greet job, this crate works very well. I did found that there's a function time_till_next_job but it's not specified for a job, can there has an api to get a specific job's next_run_time like this get_next_run_time?

Hellager commented 1 year ago

For now, it's ok to do it like this

// after the JobScheduler start
use cron::{Schedule as Cron_Schedule};
use chrono::{Utc, DateTime};
use std::str::FromStr;

.....
let cron_expression = "1/10 * * * * *";
sched.add(Job::new("1/10 * * * * *", |uuid, l| {
    let cron_schedule = Cron_Schedule::from_str(&cron_expression).unwrap();
    let next_run_time_vec: Vec<DateTime<Utc>> = cron_schedule.upcoming(Utc).take(1)
                  .filter_map(|datetime| Some(datetime)).collect::<Vec<DateTime<Utc>>>();
    println!("cron job next run time: {}", next_run_time_vec[0].to_string());
 }).await.unwrap());
mvniekerk commented 1 year ago

Hi @Hellager Thanks for the post - I'll check into this

mvniekerk commented 1 year ago

So @Hellager Have a look https://github.com/mvniekerk/tokio-cron-scheduler/releases/tag/v0.8.1

Implemented.