mcl_core_service behaviour (mcl_core v1.0.0)
View SourceThe behaviour every mcl service implements.
Six callbacks are required: info/0, start/1, stop/1,
health/0, capabilities/0 and identity_spec/0. The optional
callbacks let a service declare mesh subscriptions and human-facing capability
descriptions. Health wiring and capability advertisement are handled by the rest
of mcl_core; a new service repository (release, Containerfile, CI workflows,
compose file) is generated with rebar3 new mcl_service from
priv/templates/mcl_service/ (see scripts/scaffold-service.sh).
Persistence is the service's own. mcl_core opens no store and starts no reckon-db
or evoq application (mcl-om#10, 0.35.0): an event-sourced service declares
reckon_db, evoq and reckon_evoq itself and opens its store in its own start/2
before mcl_core:boot/1. rebar3 new mcl_service store=1 generates that wiring
in the service's own <name>_app, reading the store's settings from
<name>_service:event_store/0. A service module that still exports the old
store_id/0 and data_dir/0 callbacks together boots without a store, and
mcl_core:boot/1 warns, naming them.
Summary
Callbacks
Capabilities this service exposes, to be advertised on the mesh. Other services find this one by these names. A capability whose map includes handler => {HandlerModule, Args} (HandlerModule implementing the macula_response behaviour) is advertised via macula_response:advertise_direct/7 -- discoverable AND directly callable, and it must also name auth, who may call it (there is no default: macula 18 refuses an advertise that names none, and mcl_core refuses the registration first, naming the capability). A capability with no handler key is written as a bare discovery record only (kept for services that advertise a capability another mechanism serves); nothing can call it, so it is not asked for a policy.
OPTIONAL. Human-facing documentation for the pubsub topics this service publishes or subscribes to -- topic name, a plain-language description, and optionally the payload shape. Distinct from subscriptions/0, which wires actual subscriber processes and carries no description. See describe_rpc_capabilities/0's own doc for how this is exposed on the mesh once exported.
OPTIONAL. Human-facing documentation for this service's RPC capabilities -- name, a plain-language description, and optionally the params a caller sends / an example payload. Distinct from capabilities/0's own dispatch-wiring metadata (name/version/handler/auth/kind), which says how to route a call, never what it does. When exported (alongside either this or describe_pubsub_capabilities/0), mcl_core:boot/1 advertises a synthetic <service-name>.describe_capabilities RPC that returns both lists live -- so a mesh consumer (e.g. a tooling catalog) can call it instead of hand-maintaining descriptions.
Snapshot of current health. Called every /health hit.
UCAN this service wants minted by the realm at boot. Until UCAN-delegation lands in realm, this is informational only.
Static metadata about the service. Reported on /health. A key of the service's own rides in the ok' body verbatim; its value must be JSON-encodable (json_value()') and its name may not be one /health' sets itself --mcl_core:boot/2' refuses a key that does.
Start the service's supervision tree. Called once on boot.
Stop the service. Called on shutdown.
OPTIONAL. Topics this service subscribes to at boot: a list of subscriptions, each a map naming the topic', themodule' implementing the macula_subscriber' behaviour, theargs' it is started with (default []'), andsources' -- WHO MAY DELIVER TO IT, which is required and has no default (macula 18, macula#147): members' (any publisher the realm admitted),{org, Organisation}' (a node that organisation delegated), {nodes, [NodeId]}' (exactly those publishers, no lookup), or{open, Reason}' -- open' as an explicit opt-out, refused unless a non-empty reason says why anyone may deliver, the wayguard => none' already reads. The subscriber's own SDK enforces it on every fact before the handler runs. A subscription under a SEALED group adds group' andadmission', which are valid only together: group' is the group's prefix, whichtopic' must be under on a segment boundary, and admission' is{Module, Function, Args}' -- called as apply/3' with exactly those arguments and nothing added -- answering{ok, Token}', the UCAN that admits this node to the group, or {error, Reason}'. mcl_core calls it once per subscribe attempt and hands the token straight tomacula:subscribe/5', so the token is in no child spec, no state and no log; a refusal starts nothing and /health' names the topic and the reason. Nothing is cached: a later epoch pull is macula's keyring's business once the group is joined. A declaration mcl_core cannot wire -- nosources' above all -- refuses the boot naming the topic, before the service's own `start/1' runs. It is read twice, before start/1 and again once the service is up, so it must be PURE: derive it from config or app env, never from state start/1 creates. mcl_core:boot/1 wires each into a supervised macula_subscriber under mcl_core_pubsub_sup once the service module's start/1 has returned. Call mcl_core_pubsub:ensure_subscriptions/1 again whenever the desired set changes at runtime (e.g. a new topic per newly-registered entity) -- it diffs against what's currently running and starts/stops only the delta, restarting exactly the subscription whose declaration changed.
Types
-type capability() :: #{name := binary(), version := pos_integer(), handler => {module(), term()}, auth => open | {callers, members | role | {nodes, [binary()]}} | handler_authorizes | {ucan_required, <<_:256>>} | {realm_member_required, <<_:256>>, binary()}, kind => response | streamer, stream_opts => #{mode => server_stream | client_stream}, handler_timeout_ms => 1..600000, limits => map(), guard => none, confidential => off | preferred | required, export => mesh}.
-type identity_spec() :: #{scope := binary(), actions := [binary()], resources := [binary()], ttl_days := pos_integer()}.
-type json_value() :: binary() | atom() | number() | [json_value()] | #{binary() | atom() => json_value()}.
Callbacks
-callback capabilities() -> [capability()].
Capabilities this service exposes, to be advertised on the mesh. Other services find this one by these names. A capability whose map includes handler => {HandlerModule, Args} (HandlerModule implementing the macula_response behaviour) is advertised via macula_response:advertise_direct/7 -- discoverable AND directly callable, and it must also name auth, who may call it (there is no default: macula 18 refuses an advertise that names none, and mcl_core refuses the registration first, naming the capability). A capability with no handler key is written as a bare discovery record only (kept for services that advertise a capability another mechanism serves); nothing can call it, so it is not asked for a policy.
-callback describe_pubsub_capabilities() -> [pubsub_capability_doc()].
OPTIONAL. Human-facing documentation for the pubsub topics this service publishes or subscribes to -- topic name, a plain-language description, and optionally the payload shape. Distinct from subscriptions/0, which wires actual subscriber processes and carries no description. See describe_rpc_capabilities/0's own doc for how this is exposed on the mesh once exported.
-callback describe_rpc_capabilities() -> [rpc_capability_doc()].
OPTIONAL. Human-facing documentation for this service's RPC capabilities -- name, a plain-language description, and optionally the params a caller sends / an example payload. Distinct from capabilities/0's own dispatch-wiring metadata (name/version/handler/auth/kind), which says how to route a call, never what it does. When exported (alongside either this or describe_pubsub_capabilities/0), mcl_core:boot/1 advertises a synthetic <service-name>.describe_capabilities RPC that returns both lists live -- so a mesh consumer (e.g. a tooling catalog) can call it instead of hand-maintaining descriptions.
-callback health() -> health().
Snapshot of current health. Called every /health hit.
-callback identity_spec() -> identity_spec().
UCAN this service wants minted by the realm at boot. Until UCAN-delegation lands in realm, this is informational only.
-callback info() -> info().
Static metadata about the service. Reported on /health. A key of the service's own rides in the ok' body verbatim; its value must be JSON-encodable (json_value()') and its name may not be one /health' sets itself --mcl_core:boot/2' refuses a key that does.
Start the service's supervision tree. Called once on boot.
-callback stop(term()) -> ok.
Stop the service. Called on shutdown.
-callback subscriptions() -> [subscription()].
OPTIONAL. Topics this service subscribes to at boot: a list of subscriptions, each a map naming the topic', themodule' implementing the macula_subscriber' behaviour, theargs' it is started with (default []'), andsources' -- WHO MAY DELIVER TO IT, which is required and has no default (macula 18, macula#147): members' (any publisher the realm admitted),{org, Organisation}' (a node that organisation delegated), {nodes, [NodeId]}' (exactly those publishers, no lookup), or{open, Reason}' -- open' as an explicit opt-out, refused unless a non-empty reason says why anyone may deliver, the wayguard => none' already reads. The subscriber's own SDK enforces it on every fact before the handler runs. A subscription under a SEALED group adds group' andadmission', which are valid only together: group' is the group's prefix, whichtopic' must be under on a segment boundary, and admission' is{Module, Function, Args}' -- called as apply/3' with exactly those arguments and nothing added -- answering{ok, Token}', the UCAN that admits this node to the group, or {error, Reason}'. mcl_core calls it once per subscribe attempt and hands the token straight tomacula:subscribe/5', so the token is in no child spec, no state and no log; a refusal starts nothing and /health' names the topic and the reason. Nothing is cached: a later epoch pull is macula's keyring's business once the group is joined. A declaration mcl_core cannot wire -- nosources' above all -- refuses the boot naming the topic, before the service's own `start/1' runs. It is read twice, before start/1 and again once the service is up, so it must be PURE: derive it from config or app env, never from state start/1 creates. mcl_core:boot/1 wires each into a supervised macula_subscriber under mcl_core_pubsub_sup once the service module's start/1 has returned. Call mcl_core_pubsub:ensure_subscriptions/1 again whenever the desired set changes at runtime (e.g. a new topic per newly-registered entity) -- it diffs against what's currently running and starts/stops only the delta, restarting exactly the subscription whose declaration changed.