Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to schedule Jobs in the future using as async/await and job_scheduler
use async_std;
use futures_timer::Delay;
use job_scheduler::{Job, JobScheduler};
use std::time::Duration;
#[async_std::main]
async fn main() {
let a = async { 1u8 };
let b = a.await;
let mut sched = JobScheduler::new();
sched.add(Job::new("* * * * * *".parse().unwrap(), || {
println!("I get executed every second!");
}));
sched.add(Job::new("1/5 * * * * *".parse().unwrap(), || {
println!("I get executed every 5 seconds!");
}));
loop {
sched.tick();
let time_till_next_job = sched.time_till_next_job();
Delay::new(time_till_next_job).await;
// Not needed
// std::thread::sleep(Duration::from_millis(50));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment