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;
#[derive(Debug)]
#[derive_where(Clone, Default)]
pub struct Manager<Key = ()> {
pub(crate) jobs: Arc<RwLock<jobs::Jobs<Key>>>,
}
impl<Key> Manager<Key>
where
Key: Clone + std::hash::Hash + Eq + Send + Sync + Debug + 'static,
{
#[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())
}
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);
}
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();
}
}