Skip to content

Instantly share code, notes, and snippets.

@MarcoPolo
Created April 12, 2020 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcoPolo/9c31e8ee7adf040008f3ff5528e6ee25 to your computer and use it in GitHub Desktop.
Save MarcoPolo/9c31e8ee7adf040008f3ff5528e6ee25 to your computer and use it in GitHub Desktop.
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