Skip to main content

simple_queue/
handler.rs

1use std::pin::Pin;
2
3use crate::prelude::*;
4/// Required trait for job handlers
5#[allow(refining_impl_trait)]
6pub trait Handler: Send + Sync {
7    const QUEUE: &'static str;
8
9    /// Job processing function.
10    ///
11    /// While it's running, heartbeat will be sent to indicate that the job is alive.
12    ///
13    /// It contains reference to [`SimpleQueue`] (for interacting with the queue) and the [`Job`] given for processing.
14    ///
15    /// Should return [`JobResult`] indicating success or failure,
16    ///
17    /// Error result is considered a failure and will be retried,
18    /// however it is expected that handler will decide what to do with the error.
19    fn process(
20        &self,
21        queue: &SimpleQueue,
22        job: &Job,
23    ) -> impl Future<Output = Result<JobResult, BoxDynError>> + Send;
24
25    /// Returns the queue name this handler is associated with.
26    fn queue(&self) -> &'static str {
27        Self::QUEUE
28    }
29}
30
31pub(crate) type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
32pub(crate) trait DynJobHandler: Send + Sync {
33    fn process_dyn<'a>(
34        &'a self,
35        queue: &'a SimpleQueue,
36        job: &'a Job,
37    ) -> BoxFuture<'a, Result<JobResult, BoxDynError>>;
38}
39
40impl<T: Handler> DynJobHandler for T {
41    fn process_dyn<'a>(
42        &'a self,
43        queue: &'a SimpleQueue,
44        job: &'a Job,
45    ) -> BoxFuture<'a, Result<JobResult, BoxDynError>> {
46        Box::pin(self.process(queue, job))
47    }
48}