show waterlevel for the next days
All checks were successful
CI/CD Pipeline / test (push) Successful in 9m49s
CI/CD Pipeline / deploy-staging (push) Has been skipped
CI/CD Pipeline / deploy-main (push) Has been skipped

This commit is contained in:
2024-04-30 11:59:33 +02:00
parent 3323807e46
commit 3a39315a01
11 changed files with 369 additions and 4 deletions

33
src/scheduled/mod.rs Normal file
View File

@ -0,0 +1,33 @@
mod waterlevel;
use std::time::Duration;
use job_scheduler_ng::{Job, JobScheduler};
use rocket::tokio::{self, task};
use sqlx::SqlitePool;
pub async fn schedule(db: &SqlitePool) {
let db = db.clone();
waterlevel::update(&db).await.unwrap();
tokio::task::spawn(async {
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
task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
waterlevel::update(&db_clone).await.unwrap();
});
});
}));
loop {
sched.tick();
std::thread::sleep(Duration::from_secs(60));
}
});
}