macula_publisher behaviour (macula v10.1.1)

View Source

Behaviour for supervised content publishers.

macula:publish/4 is a plain blocking call — no addressable pid to cancel it from outside. This is the missing supervised counterpart to macula_subscriber: every other primitive pair already has one on each side (macula_request/macula_response for RPC, macula_streamer/macula_stream_sink for streaming RPC, macula_feeder/macula_download for content sharing) — pubsub had only the consumer half. start_link/5,6 returns immediately with a pid, runs macula:publish/4 in a linked worker, delivers the outcome to Module:handle_published/2, and publishes pubsub.publish_started_v1 / pubsub.publish_completed_v1 mesh facts around the publish — including outcome => cancelled if the publisher is stopped before the publish resolves.

Example

   -module(status_publisher).
   -behaviour(macula_publisher).
   -export([init/1, handle_published/2]).
  
   init(Parent) -> {ok, Parent}.
  
   handle_published(Result, Parent) ->
       Parent ! {published, Result},
       {stop, normal, Parent}.
   {ok, Pid} = macula_publisher:start_link(status_publisher, Pool, Realm,
       Topic, Payload, self()).

Summary

Functions

Cancel an in-flight publish. Publishes pubsub.publish_completed_v1 with outcome => cancelled if the publish had not resolved yet.

Start a publisher. Publishes Payload on Topic via Pool.

Callbacks

handle_published/2

-callback handle_published(Result :: ok | {error, term()}, State :: term()) ->
                              {noreply, NewState :: term()} | {stop, Reason :: term(), NewState :: term()}.

init/1

-callback init(Args :: term()) -> {ok, State :: term()} | {stop, Reason :: term()}.

Functions

cancel(Pid)

-spec cancel(pid()) -> ok.

Cancel an in-flight publish. Publishes pubsub.publish_completed_v1 with outcome => cancelled if the publish had not resolved yet.

start_link(Module, Pool, Realm, Topic, Payload)

-spec start_link(module(), macula:pool(), macula:realm(), macula:topic(), term()) ->
                    {ok, pid()} | {error, term()}.

Start a publisher. Publishes Payload on Topic via Pool.

start_link(Module, Pool, Realm, Topic, Payload, Args)

-spec start_link(module(), macula:pool(), macula:realm(), macula:topic(), term(), term()) ->
                    {ok, pid()} | {error, term()}.

As start_link/5, with Args passed to Module:init/1.