tokio_cron_scheduler/notification/
runner.rs

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
82
83
use crate::context::Context;
#[cfg(not(feature = "has_bytes"))]
use crate::job::job_data::JobState;
#[cfg(feature = "has_bytes")]
use crate::job::job_data_prost::JobState;
use crate::job::to_code::NotificationCode;
use crate::store::NotificationStore;
use crate::JobSchedulerError;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use tokio::sync::broadcast::Receiver;
use tokio::sync::RwLock;
use tracing::error;
use uuid::Uuid;

#[derive(Default)]
pub struct NotificationRunner {}

impl NotificationRunner {
    async fn listen_for_activations(
        code: Arc<RwLock<Box<dyn NotificationCode + Send + Sync>>>,
        mut rx: Receiver<(Uuid, JobState)>,
        storage: Arc<RwLock<Box<dyn NotificationStore + Send + Sync>>>,
    ) {
        loop {
            let val = rx.recv().await;
            if let Err(e) = val {
                error!("Error receiving value {:?}", e);
                break;
            }
            let (job_id, state) = val.unwrap();
            let mut storage = storage.write().await;
            let notifications = storage
                .list_notification_guids_for_job_and_state(job_id, state)
                .await;
            if let Err(_e) = notifications {
                error!(
                    "Error getting the list of notifications guids for job {:?} and state {:?}",
                    job_id, state
                );
                continue;
            }
            let notifications = notifications.unwrap();
            let mut code = code.write().await;
            for notification_id in notifications {
                let code = code.get(notification_id).await;
                match code {
                    Ok(Some(code)) => {
                        let code = code.clone();
                        tokio::spawn(async move {
                            let mut code = code.write().await;
                            (code)(job_id, notification_id, state).await;
                        });
                    }
                    _ => {
                        error!(
                            " nCould not get notification code for {:?}",
                            notification_id
                        );
                        continue;
                    }
                }
            }
        }
    }

    pub fn init(
        &mut self,
        context: &Context,
    ) -> Pin<Box<dyn Future<Output = Result<(), JobSchedulerError>> + Send>> {
        let code = context.notification_code.clone();
        let rx = context.notify_tx.subscribe();
        let storage = context.notification_storage.clone();

        Box::pin(async move {
            tokio::spawn(NotificationRunner::listen_for_activations(
                code, rx, storage,
            ));
            Ok(())
        })
    }
}