Skip to main content

runledger_core/jobs/
handler.rs

1use std::sync::Arc;
2
3use async_trait::async_trait;
4use serde_json::Value;
5
6use super::identifiers::JobType;
7use super::{JobCompletion, JobContext, JobDeadLetterInfo, JobFailure};
8
9#[async_trait]
10pub trait JobHandler: Send + Sync {
11    /// Returns the stable identity used to register and route this handler.
12    ///
13    /// Implementations must return the same value for the handler's lifetime.
14    fn job_type(&self) -> JobType<'static>;
15    async fn execute(
16        &self,
17        context: JobContext,
18        payload: Value,
19    ) -> Result<JobCompletion, JobFailure>;
20
21    async fn on_dead_letter(
22        &self,
23        _context: JobContext,
24        _payload: Value,
25        _dead_letter: JobDeadLetterInfo,
26    ) {
27    }
28}
29
30pub trait JobHandlerRegistry {
31    fn register_boxed(&mut self, handler: Arc<dyn JobHandler>);
32
33    fn register<H>(&mut self, handler: H)
34    where
35        Self: Sized,
36        H: JobHandler + 'static,
37    {
38        self.register_boxed(Arc::new(handler));
39    }
40}