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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//! Defines actor for running periodic Redis tasks.
use crate::application::RedisManager;
use std::time::Duration;

use deadpool_redis::Pool;
use log::{error, info};

use crate::config::ServerConfig;

/// Start all background tasks that perform monitoring/cleanup.
pub fn start_monitors(redis_manager: &RedisManager, pool: Pool, config: &ServerConfig) {
    start_timeout_monitor(redis_manager.clone(), pool.clone(), config.timeout_check_interval.0);
    start_retry_monitor(redis_manager.clone(), pool.clone(), config.retry_check_interval.0);
    start_expiry_monitor(redis_manager.clone(), pool, config.expiry_check_interval.0);
}

/// Start periodic background task that checks jobs for timeouts.
fn start_timeout_monitor(redis_manager: RedisManager, pool: Pool, check_interval: Duration) {
    info!(
        "Checking job timeouts every {}",
        humantime::format_duration(check_interval)
    );
    actix_web::rt::spawn(async move {
        let mut interval = actix_web::rt::time::interval(check_interval);
        loop {
            interval.tick().await;
            match pool.get().await {
                Ok(mut conn) => {
                    if let Err(err) = redis_manager.check_job_timeouts(&mut conn).await {
                        error!("Job timeout monitoring failed: {}", err);
                    }
                },
                Err(err) => error!("Job timeout monitoring failed: {}", err),
            }
        }
    });
}

/// Start periodic background task that checks for jobs that need retrying.
fn start_retry_monitor(redis_manager: RedisManager, pool: Pool, check_interval: Duration) {
    info!(
        "Checking job retries every {}",
        humantime::format_duration(check_interval)
    );
    actix_web::rt::spawn(async move {
        let mut interval = actix_web::rt::time::interval(check_interval);
        loop {
            interval.tick().await;
            match pool.get().await {
                Ok(mut conn) => {
                    if let Err(err) = redis_manager.check_job_retries(&mut conn).await {
                        error!("Job retry monitoring failed: {}", err);
                    }
                },
                Err(err) => error!("Job timeout monitoring failed: {}", err),
            }
        }
    });
}

/// Start periodic background that checks for expired jobs and cleans them up.
fn start_expiry_monitor(redis_manager: RedisManager, pool: Pool, check_interval: Duration) {
    info!(
        "Checking job expiry every {}",
        humantime::format_duration(check_interval)
    );
    actix_web::rt::spawn(async move {
        let mut interval = actix_web::rt::time::interval(check_interval);
        loop {
            interval.tick().await;
            match pool.get().await {
                Ok(mut conn) => {
                    if let Err(err) = redis_manager.check_job_expiry(&mut conn).await {
                        error!("Job expiry monitoring failed: {}", err);
                    }
                },
                Err(err) => error!("Job timeout monitoring failed: {}", err),
            }
        }
    });
}