Skip to main content

navree_queue/
drivers.rs

1//! Queue stores a module registers under a name: [`ModuleBuilderQueueExt`].
2
3use std::future::Future;
4use std::sync::Arc;
5
6use navree_config::__private::register_driver;
7use navree_config::{DriverContext, DriverSlot};
8use navree_kernel::{BoxError, ModuleBuilder};
9
10use crate::QueueStore;
11
12/// The selector `queue.driver`, its built-in drivers and the method that
13/// registers more.
14pub(crate) const SLOT: DriverSlot = DriverSlot::new(
15    "queue.driver",
16    &["database", "memory", "redis"],
17    "m.queue_store",
18    "queue store",
19);
20
21/// What the boot builds first: it holds that every registration of a queue
22/// store was checked. A provider of its own, so an app that replaces `Queue`
23/// (with a fake, in a test) still has its registrations checked.
24pub(crate) struct CheckedStores;
25
26/// Registers queue stores on a [`ModuleBuilder`]: `use
27/// navree::plugin::prelude::*;` (or this trait) brings the method.
28///
29/// ```
30/// use navree_kernel::ModuleBuilder;
31/// use navree_queue::{ModuleBuilderQueueExt, QueueStore};
32/// use serde::Deserialize;
33///
34/// /// The settings of the store: `[queue.amritsar_queue]`.
35/// #[derive(Default, Deserialize)]
36/// #[serde(default)]
37/// struct AmritsarSettings {
38///     depot: String,
39/// }
40///
41/// /// What a module's `register` calls: `open` builds your `QueueStore`
42/// /// from the settings the app configured. Selected with
43/// /// `queue.driver = "amritsar_queue"`.
44/// fn register_store<S: QueueStore>(m: &mut ModuleBuilder, open: fn(AmritsarSettings) -> S) {
45///     m.queue_store("amritsar_queue", move |ctx| {
46///         let store = ctx.settings().map(open);
47///         async move { store }
48///     });
49/// }
50/// ```
51pub trait ModuleBuilderQueueExt {
52    /// Registers a queue store under `name` (a lower-case letter followed by
53    /// up to 63 lower-case letters, digits or `_`): `queue.driver = "{name}"`
54    /// selects it, and `factory` builds it at boot from its
55    /// [`DriverContext`], whose settings are `queue.{name}`.
56    ///
57    /// A name that is not such a word, a built-in name (`database`, `memory`,
58    /// `redis`), a key of the `queue` section (`workers`) and a name two
59    /// modules register fail the boot, naming `m.queue_store` and the
60    /// module, also when another store is selected; `config:check` says the
61    /// same. A factory that fails fails the boot, naming the store and the
62    /// module. A factory's error is shown in the boot error as written, so it
63    /// must not contain a secret from the driver's settings.
64    fn queue_store<S, F, Fut>(&mut self, name: &'static str, factory: F) -> &mut Self
65    where
66        S: QueueStore,
67        F: Fn(DriverContext) -> Fut + Send + Sync + 'static,
68        Fut: Future<Output = Result<S, BoxError>> + Send + 'static;
69}
70
71impl ModuleBuilderQueueExt for ModuleBuilder {
72    fn queue_store<S, F, Fut>(&mut self, name: &'static str, factory: F) -> &mut Self
73    where
74        S: QueueStore,
75        F: Fn(DriverContext) -> Fut + Send + Sync + 'static,
76        Fut: Future<Output = Result<S, BoxError>> + Send + 'static,
77    {
78        register_driver::<dyn QueueStore, _, _>(self, &SLOT, Some("queue"), name, move |ctx| {
79            let built = factory(ctx);
80            async move {
81                built
82                    .await
83                    .map(|store| Arc::new(store) as Arc<dyn QueueStore>)
84            }
85        })
86    }
87}