tokio_cron_scheduler/job/
deleter.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
84
85
86
87
88
89
90
use crate::context::Context;
use crate::store::MetaDataStorage;
use crate::JobSchedulerError;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use tokio::sync::broadcast::{Receiver, Sender};
use tokio::sync::RwLock;
use tracing::error;
use uuid::Uuid;

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

impl JobDeleter {
    async fn listen_to_removals(
        storage: Arc<RwLock<Box<dyn MetaDataStorage + Send + Sync>>>,
        mut rx: Receiver<Uuid>,
        tx_deleted: Sender<Result<Uuid, (JobSchedulerError, Option<Uuid>)>>,
    ) {
        loop {
            let val = rx.recv().await;
            if let Err(e) = val {
                error!("Error receiving value {:?}", e);
                break;
            }
            let uuid = val.unwrap();
            {
                let mut storage = storage.write().await;
                let delete = storage.delete(uuid).await;
                if let Err(e) = delete {
                    error!("Error deleting {:?}", e);
                    if let Err(e) = tx_deleted.send(Err((e, Some(uuid)))) {
                        error!("Error sending delete error {:?}", e);
                    }
                    continue;
                }
            }
            if let Err(e) = tx_deleted.send(Ok(uuid)) {
                error!("Error sending error {:?}", e);
            }
        }
    }

    pub fn init(
        &mut self,
        context: &Context,
    ) -> Pin<Box<dyn Future<Output = Result<(), JobSchedulerError>> + Send + Sync>> {
        let rx = context.job_delete_tx.subscribe();
        let tx_deleted = context.job_deleted_tx.clone();
        let storage = context.metadata_storage.clone();

        Box::pin(async move {
            tokio::spawn(JobDeleter::listen_to_removals(storage, rx, tx_deleted));
            Ok(())
        })
    }

    pub async fn remove(context: &Context, job_id: &Uuid) -> Result<(), JobSchedulerError> {
        let delete = context.job_delete_tx.clone();
        let mut deleted = context.job_deleted_tx.subscribe();

        let job_id = *job_id;
        tokio::spawn(async move {
            if let Err(e) = delete.send(job_id) {
                error!("Error sending delete id {:?}", e);
            }
        });
        while let Ok(deleted) = deleted.recv().await {
            match deleted {
                Ok(uuid) => {
                    if uuid == job_id {
                        return Ok(());
                    } else {
                        continue;
                    }
                }
                Err((e, Some(uuid))) => {
                    if uuid == job_id {
                        return Err(e);
                    } else {
                        continue;
                    }
                }
                _ => continue,
            }
        }
        Err(JobSchedulerError::RemoveShutdownNotifier)
    }
}