1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
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) => {
log::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) => log::error!("Error actualizing job last execution: {:?}", err),
}
}
|