use sqlx::error::Error; use sqlx::sqlite::SqlitePool; use crate::model::job::Job; pub async fn should_run(pool: &SqlitePool, job: Job) -> bool { let run_from = match job { Job::WeeklyReport => "date('now', 'weekday 0', '-6 days')", Job::MonthlyPayment => "date('now', 'start of month')", }; let query = format!( r#" SELECT 1 FROM jobs WHERE name = ? AND last_execution < {} "#, run_from ); let res = sqlx::query(&query).bind(job).fetch_one(pool).await; match res { Ok(_) => true, Err(Error::RowNotFound) => false, Err(err) => { error!("Error looking if job should run: {:?}", err); false } } } pub async fn actualize_last_execution(pool: &SqlitePool, job: Job) -> () { let query = r#" UPDATE jobs SET last_execution = datetime() WHERE name = ? "#; let res = sqlx::query(query).bind(job).execute(pool).await; match res { Ok(_) => (), Err(err) => { error!("Error actualizing job last execution: {:?}", err); () } } }