Skip to main content

nest_rs_queue/
inventory.rs

1//! The per-method inventory entry — the link-time seam between the
2//! `#[processor]` macro and any backend.
3//!
4//! Type-erased on purpose: the [`JobHandler`] receives a `serde_json::Value`
5//! and deserializes to the method's job type inside the closure the macro
6//! emits. This frees the backend from naming the user's `J` and frees the
7//! inventory from carrying backend-specific function pointers.
8
9use std::any::TypeId;
10use std::future::Future;
11use std::pin::Pin;
12
13use nest_rs_core::Container;
14
15/// Wire-format version every backend wraps jobs with on push and unwraps on
16/// dispatch. Bumping it lets a `#[processor]` handler reject payloads from a
17/// newer release (rolling-deploy safety) instead of misinterpreting bytes.
18///
19/// The envelope is `{ "v": <number>, "payload": <user payload> }`. An
20/// **unversioned** value — anything that isn't an object with both `v` and
21/// `payload` keys — is treated as a legacy raw payload and decoded directly
22/// as the job type (with a warning), so jobs left in Redis from a prior
23/// deploy still drain.
24pub const WIRE_FORMAT_VERSION: u32 = 1;
25
26/// Type-erased async job handler the `#[processor]` macro emits for each
27/// `#[process]` method. Backends invoke it with a JSON payload pulled off
28/// their wire; the closure deserializes to the user's job type, resolves the
29/// provider from the container, and dispatches.
30pub type JobHandler = fn(
31    payload: serde_json::Value,
32    container: Container,
33) -> Pin<
34    Box<dyn Future<Output = Result<(), Box<dyn std::error::Error + Send + Sync>>> + Send>,
35>;
36
37/// Runtime metadata for one consumer, surfaced via `DiscoveryService` for the
38/// classic struct-level `#[processor]` form. New code uses [`ProcessMethod`]
39/// directly; this type remains for backends that consume `ProcessorMeta` via
40/// `DiscoveryService::meta::<ProcessorMeta>()`.
41pub struct ProcessorMeta {
42    pub name: &'static str,
43    pub queue: &'static str,
44    pub concurrency: usize,
45    pub retries: usize,
46    /// The type-erased handler the backend dispatches each job through.
47    pub handler: JobHandler,
48}
49
50/// Link-time inventory entry submitted by `#[processor]` for each
51/// `#[process]`-tagged method. A `JobConsumer` drains this registry at boot
52/// and filters by
53/// [`ReachableProviders`](::nest_rs_core::ReachableProviders) so a method on a
54/// provider not reachable from the app's module tree is silently skipped.
55pub struct ProcessMethod {
56    pub name: &'static str,
57    pub queue: &'static str,
58    pub concurrency: usize,
59    pub retries: usize,
60    pub provider_type_id: fn() -> TypeId,
61    pub handler: JobHandler,
62}
63
64::nest_rs_core::inventory::collect!(ProcessMethod);