Skip to main content

folk_api/
context.rs

1//! Dependencies handed to a plugin at boot.
2
3use std::sync::Arc;
4
5use tokio::sync::watch;
6
7use crate::executor::Executor;
8use crate::health::HealthRegistry;
9use crate::metrics::MetricsRegistry;
10use crate::role::PluginRole;
11use crate::rpc::RpcRegistrar;
12
13/// The worker pool a plugin instance is running in (phase 85).
14///
15/// In the fork-after-warm model an operator can scale subsystems independently
16/// by giving a plugin its own worker pool (`[http].workers`, `[jobs].workers`).
17/// A plugin that must be reachable from *every* pool — because other pools call
18/// into it (e.g. `jobs.push`, invoked from HTTP handlers) — declares
19/// [`Plugin::runs_in_every_pool`](crate::Plugin::runs_in_every_pool) and is
20/// booted in each one. Such a plugin uses [`primary`](Self::primary) to tell its
21/// **owning** pool (do the full work: run consumers) from pools where it exists
22/// only to expose a cross-cutting capability (push-only, no consumers).
23#[derive(Debug, Clone)]
24pub struct PoolContext {
25    /// Pool label: the plugin name for a dedicated pool, `"shared"` otherwise.
26    pub label: String,
27    /// `true` in the plugin's owning pool (its dedicated pool, or the shared
28    /// pool when it has no dedicated one); `false` where the plugin is present
29    /// only as a cross-cutting producer. Single-pool plugins (http/grpc) are
30    /// always `true` in the one pool they run in.
31    pub primary: bool,
32}
33
34/// What every plugin receives in [`Plugin::boot`](crate::Plugin::boot).
35///
36/// All fields are cheaply cloneable (`Arc` or `Receiver`).
37#[derive(Clone)]
38pub struct PluginContext {
39    /// Which process this plugin instance is booting in (phase 79).
40    ///
41    /// In the **master** the `executor` is a
42    /// [`NoopExecutor`](crate::NoopExecutor) (no PHP); in a **worker** it
43    /// dispatches into that worker's PHP runtime. A [`Placement::Both`] plugin
44    /// branches on this; single-placement plugins can ignore it.
45    ///
46    /// [`Placement::Both`]: crate::Placement::Both
47    pub role: PluginRole,
48
49    /// Send work to a PHP worker.
50    ///
51    /// Valid only when [`role`](Self::role) is [`PluginRole::Worker`]; in the
52    /// master it is a [`NoopExecutor`](crate::NoopExecutor) whose methods return
53    /// `Err`.
54    pub executor: Arc<dyn Executor>,
55
56    /// Fires `true` when the server is shutting down.
57    pub shutdown: watch::Receiver<bool>,
58
59    /// Register RPC method handlers. `None` if not available.
60    pub rpc_registrar: Option<Arc<dyn RpcRegistrar>>,
61
62    /// Register health checks. `None` if not available.
63    pub health_registry: Option<Arc<dyn HealthRegistry>>,
64
65    /// Register metrics. `None` if not available.
66    pub metrics_registry: Option<Arc<dyn MetricsRegistry>>,
67
68    /// The worker pool this instance runs in (phase 85). `None` in the master
69    /// and on the legacy single-process path (treat as "owning pool"). A plugin
70    /// that [`runs_in_every_pool`](crate::Plugin::runs_in_every_pool) reads
71    /// [`primary`](PoolContext::primary) to decide full-work vs push-only.
72    pub pool: Option<PoolContext>,
73}