Skip to main content

folk_api/
plugin.rs

1//! The `Plugin` trait: every plugin in Folk implements this.
2
3use anyhow::Result;
4use async_trait::async_trait;
5
6use crate::context::PluginContext;
7use crate::role::Placement;
8use crate::rpc::RpcMethodDef;
9
10/// Lifecycle of a Folk plugin.
11///
12/// At server startup, plugins are booted in registration order. At shutdown,
13/// they are stopped in reverse order. A plugin that holds long-running tasks
14/// should consider implementing [`crate::ServerPlugin`] instead — the wrapper
15/// handles the boilerplate of spawning, holding handles, and joining.
16#[async_trait]
17pub trait Plugin: Send + Sync + 'static {
18    /// Stable plugin name. Conventionally lowercase, single-word: `"http"`,
19    /// `"jobs"`, `"metrics"`. Used for log scoping, health/metric labels,
20    /// and config-section routing in custom builds.
21    fn name(&self) -> &'static str;
22
23    /// Called once at server startup. Register RPC methods, health checks,
24    /// and metrics via `ctx`, then spawn background tasks. Returning `Err`
25    /// is fatal: the server aborts startup.
26    async fn boot(&mut self, ctx: PluginContext) -> Result<()>;
27
28    /// Called during graceful shutdown. Returning `Err` is logged but does
29    /// not abort shutdown of other plugins.
30    async fn shutdown(&self) -> Result<()>;
31
32    /// Optional: the RPC methods this plugin exposes. Used for advertising
33    /// (e.g., `folk admin list-methods`). Default is empty.
34    fn rpc_methods(&self) -> Vec<RpcMethodDef> {
35        Vec::new()
36    }
37
38    /// Where this plugin runs in the fork-after-warm model (phase 79).
39    ///
40    /// The core boots the plugin only in the process(es) named here. The
41    /// default is [`Placement::WorkerOnly`] — the common case (HTTP/gRPC/jobs
42    /// dispatch PHP and live in workers). Master-only plugins (supervisors, the
43    /// metrics scrape server) override this to [`Placement::MasterOnly`].
44    fn placement(&self) -> Placement {
45        Placement::WorkerOnly
46    }
47
48    /// Whether this plugin must be booted in **every** worker pool, not just its
49    /// own (phase 85).
50    ///
51    /// Default `false`: a worker-plugin runs only in its pool (a dedicated
52    /// `[<plugin>].workers` pool, or the shared pool). `true` means the plugin
53    /// exposes a cross-cutting capability that other pools call into — e.g.
54    /// `jobs` enqueue (`jobs.push`), invoked from an HTTP handler running in a
55    /// separate `http` pool. Because Folk's RPC is process-local (zero-IPC), the
56    /// only way that call resolves is for the `jobs` plugin to also be present in
57    /// the `http` pool. Such a plugin branches on
58    /// [`PoolContext::primary`](crate::PoolContext::primary) to do its heavy work
59    /// (run consumers) only in its owning pool and stay push-only elsewhere.
60    fn runs_in_every_pool(&self) -> bool {
61        false
62    }
63}