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
use std::fmt::Debug;
use std::sync::Arc;

use derive_where::derive_where;
use parking_lot::RwLock;

use crate::tasks::handle::{Handle, Id};
use crate::tasks::traits::Executable;
use crate::tasks::{Job, Keyed};

pub(crate) mod jobs;
mod managed_job;
pub(crate) use managed_job::ManagedJob;

#[cfg(test)]
mod tests;

/// A background jobs manager.
#[derive(Debug)]
#[derive_where(Clone, Default)]
pub struct Manager<Key = ()> {
    // #[derive_where(default)]
    pub(crate) jobs: Arc<RwLock<jobs::Jobs<Key>>>,
}

impl<Key> Manager<Key>
where
    Key: Clone + std::hash::Hash + Eq + Send + Sync + Debug + 'static,
{
    /// Pushes a `job` into the queue. Pushing the same job definition twice
    /// will yield two tasks in the queue.
    #[cfg(test)]
    pub fn enqueue<J: Job + 'static>(&self, job: J) -> Handle<J::Output, J::Error> {
        let mut jobs = self.jobs.write();
        jobs.enqueue(job, None, self.clone())
    }

    /// Uses [`Keyed::key`] to ensure no other job with the same `key` is
    /// currently running. If another job is already running that matches, a
    /// clone of that [`Handle`] will be returned. When the job finishes, all
    /// [`Handle`] clones will be notified with a copy of the result.
    pub fn lookup_or_enqueue<J: Keyed<Key>>(
        &self,
        job: J,
    ) -> Handle<<J as Job>::Output, <J as Job>::Error> {
        let mut jobs = self.jobs.write();
        jobs.lookup_or_enqueue(job, self.clone())
    }

    fn job_completed<T: Clone + Send + Sync + 'static, E: Send + Sync + 'static>(
        &self,
        id: Id,
        key: Option<&Key>,
        result: Result<T, E>,
    ) {
        let mut jobs = self.jobs.write();
        jobs.job_completed(id, key, result);
    }

    /// Spawns a worker. In general, you shouldn't need to call this function
    /// directly.
    pub fn spawn_worker(&self) {
        let receiver = {
            let jobs = self.jobs.read();
            jobs.queue()
        };
        std::thread::Builder::new()
            .name(String::from("bonsaidb-tasks"))
            .spawn(move || worker_thread(&receiver))
            .unwrap();
    }
}

fn worker_thread(receiver: &flume::Receiver<Box<dyn Executable>>) {
    while let Ok(mut job) = receiver.recv() {
        job.execute();
    }
}