1
use std::{any::Any, collections::HashMap, fmt::Debug, sync::Arc};
2

            
3
use flume::{Receiver, Sender};
4
use tokio::sync::oneshot;
5

            
6
use crate::jobs::{
7
    manager::{ManagedJob, Manager},
8
    task::{Handle, Id},
9
    traits::Executable,
10
    Job, Keyed,
11
};
12

            
13
pub struct Jobs<Key> {
14
    last_task_id: u64,
15
    result_senders: HashMap<Id, Vec<Box<dyn AnySender>>>,
16
    keyed_jobs: HashMap<Key, Id>,
17
    queuer: Sender<Box<dyn Executable>>,
18
    queue: Receiver<Box<dyn Executable>>,
19
}
20

            
21
impl<Key> Debug for Jobs<Key>
22
where
23
    Key: Debug,
24
{
25
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26
        f.debug_struct("Jobs")
27
            .field("last_task_id", &self.last_task_id)
28
            .field("result_senders", &self.result_senders.len())
29
            .field("keyed_jobs", &self.keyed_jobs)
30
            .field("queuer", &self.queuer)
31
            .field("queue", &self.queue)
32
            .finish()
33
    }
34
}
35

            
36
impl<Key> Default for Jobs<Key> {
37
209
    fn default() -> Self {
38
209
        let (queuer, queue) = flume::unbounded();
39
209

            
40
209
        Self {
41
209
            last_task_id: 0,
42
209
            result_senders: HashMap::new(),
43
209
            keyed_jobs: HashMap::new(),
44
209
            queuer,
45
209
            queue,
46
209
        }
47
209
    }
48
}
49

            
50
impl<Key> Jobs<Key>
51
where
52
    Key: Clone + std::hash::Hash + Eq + Send + Sync + Debug + 'static,
53
{
54
1742
    pub fn queue(&self) -> Receiver<Box<dyn Executable>> {
55
1742
        self.queue.clone()
56
1742
    }
57

            
58
158388
    pub fn enqueue<J: Job + 'static>(
59
158388
        &mut self,
60
158388
        job: J,
61
158388
        key: Option<Key>,
62
158388
        manager: Manager<Key>,
63
158388
    ) -> Handle<J::Output, J::Error, Key> {
64
158388
        self.last_task_id = self.last_task_id.wrapping_add(1);
65
158388
        let id = Id(self.last_task_id);
66
158388
        self.queuer
67
158388
            .send(Box::new(ManagedJob {
68
158388
                id,
69
158388
                job,
70
158388
                key,
71
158388
                manager: manager.clone(),
72
158388
            }))
73
158388
            .unwrap();
74
158388

            
75
158388
        self.create_new_task_handle(id, manager)
76
158388
    }
77

            
78
166651
    pub fn create_new_task_handle<T: Send + Sync + 'static, E: Send + Sync + 'static>(
79
166651
        &mut self,
80
166651
        id: Id,
81
166651
        manager: Manager<Key>,
82
166651
    ) -> Handle<T, E, Key> {
83
166651
        let (sender, receiver) = oneshot::channel();
84
166651
        let senders = self.result_senders.entry(id).or_insert_with(Vec::default);
85
166651
        senders.push(Box::new(Some(sender)));
86
166651

            
87
166651
        Handle {
88
166651
            id,
89
166651
            manager,
90
166651
            receiver,
91
166651
        }
92
166651
    }
93

            
94
115226
    pub fn lookup_or_enqueue<J: Keyed<Key>>(
95
115226
        &mut self,
96
115226
        job: J,
97
115226
        manager: Manager<Key>,
98
115226
    ) -> Handle<<J as Job>::Output, <J as Job>::Error, Key> {
99
115226
        let key = job.key();
100
115226
        if let Some(&id) = self.keyed_jobs.get(&key) {
101
8350
            self.create_new_task_handle(id, manager)
102
        } else {
103
106876
            let handle = self.enqueue(job, Some(key.clone()), manager);
104
106876
            self.keyed_jobs.insert(key, handle.id);
105
106876
            handle
106
        }
107
115226
    }
108

            
109
    pub fn job_completed<T: Clone + Send + Sync + 'static, E: Send + Sync + 'static>(
110
        &mut self,
111
        id: Id,
112
        key: Option<&Key>,
113
        result: Result<T, E>,
114
    ) {
115
158202
        if let Some(key) = key {
116
106756
            self.keyed_jobs.remove(key);
117
143522
        }
118

            
119
158202
        if let Some(senders) = self.result_senders.remove(&id) {
120
158202
            let result = result.map_err(Arc::new);
121
324689
            for mut sender_handle in senders {
122
166509
                let sender = sender_handle
123
166509
                    .as_any_mut()
124
166509
                    .downcast_mut::<Option<oneshot::Sender<Result<T, Arc<E>>>>>()
125
166509
                    .unwrap();
126
166509
                if let Some(sender) = sender.take() {
127
166487
                    drop(sender.send(result.clone()));
128
166487
                }
129
            }
130
        }
131
158180
    }
132
}
133

            
134
pub trait AnySender: Any + Send + Sync {
135
    fn as_any_mut(&mut self) -> &'_ mut dyn Any;
136
}
137

            
138
impl<T> AnySender for Option<oneshot::Sender<T>>
139
where
140
    T: Send + Sync + 'static,
141
{
142
166487
    fn as_any_mut(&mut self) -> &'_ mut dyn Any {
143
166487
        self
144
166487
    }
145
}