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}