aboutsummaryrefslogtreecommitdiff
path: root/src/jobs/jobs.rs
blob: 3e54624125c2a8dc8d8d2cb0cff2d5836c0d8f41 (plain)
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
use sqlx::sqlite::SqlitePool;
use tera::Tera;
use tokio::time::{delay_for, Duration};

use crate::db;
use crate::jobs::weekly_report;
use crate::model::config::Config;
use crate::model::job::Job;

pub async fn start(config: Config, pool: SqlitePool, templates: Tera) -> () {
    loop {
        if db::jobs::should_run(&pool, Job::WeeklyReport).await {
            info!("Starting weekly report job");
            if weekly_report::send(&config, &pool, &templates).await {
                db::jobs::actualize_last_execution(&pool, Job::WeeklyReport)
                    .await;
            }
        }
        if db::jobs::should_run(&pool, Job::MonthlyPayment).await {
            info!("Starting monthly payment job");
            db::payments::create_monthly_payments(&pool).await;
            db::jobs::actualize_last_execution(&pool, Job::MonthlyPayment)
                .await;
        }
        // Sleeping 8 hours
        delay_for(Duration::from_secs(8 * 60 * 60)).await;
    }
}