mod waterlevel; mod weather; use std::time::Duration; use job_scheduler_ng::{Job, JobScheduler}; use rocket::tokio::{self, task, time}; use sqlx::SqlitePool; use crate::tera::Config; pub fn schedule(db: &SqlitePool, config: &Config) { let db = db.clone(); let openweathermap_key = config.openweathermap_key.clone(); tokio::task::spawn(async { waterlevel::update(&db).await.unwrap(); weather::update(&db, &openweathermap_key).await.unwrap(); let mut sched = JobScheduler::new(); // Every hour sched.add(Job::new("0 0 * * * * *".parse().unwrap(), move || { let db_clone = db.clone(); // Use block_in_place to run async code in the synchronous function; TODO: Make it // nicer one's rust (stable) support async closures task::block_in_place(|| { tokio::runtime::Handle::current().block_on(async { waterlevel::update(&db_clone).await.unwrap(); weather::update(&db_clone, &openweathermap_key) .await .unwrap(); }); }); })); let mut interval = time::interval(Duration::from_secs(60)); loop { sched.tick(); interval.tick().await; } }); }