qrush_engine/
registry.rs

1// src/registry.rs
2
3use std::collections::HashMap;
4use std::sync::Mutex;
5use futures::future::BoxFuture;
6use once_cell::sync::Lazy;
7use anyhow::Result;
8use crate::job::Job;
9
10/// Type alias for job handler functions.
11/// These handlers take a serialized JSON `String` and return a boxed `Job` trait object.
12pub type HandlerFn = fn(String) -> BoxFuture<'static, Result<Box<dyn Job>>>;
13
14/// Global job registry holding job name → handler mappings.
15pub static JOB_REGISTRY: Lazy<Mutex<HashMap<&'static str, HandlerFn>>> =
16    Lazy::new(|| Mutex::new(HashMap::new()));
17
18/// Register a job type and its deserialization handler function.
19pub fn register_job(name: &'static str, handler: HandlerFn) {
20    JOB_REGISTRY.lock().unwrap().insert(name, handler);
21}
22
23/// Get a copy of the registered job handlers.
24pub fn get_registered_jobs() -> HashMap<&'static str, HandlerFn> {
25    JOB_REGISTRY.lock().unwrap().clone()
26}
27
28/// Try to get a handler from the registry for a given job type.
29pub fn get_job_handler(name: &str) -> Option<HandlerFn> {
30    JOB_REGISTRY.lock().unwrap().get(name).copied()
31}