Created
April 12, 2020 04:07
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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