WebhooksEmitter (webhooks_emitter v0.2.0) View Source

Documentation for WebhooksEmitter.

Link to this section Summary

Functions

Attaches a new emitter to the event.

Attaches a new emitter to a list of events.

Detaches an emitter.

Emits an event, invoking all emitters attached to it.

Returns all running emitter IDs attached to events.

Pauses an emitter.

Restart an already present, but paused emitter

Checks if a given emitter ID is already started

Link to this section Functions

Link to this function

attach(emitter_id, event_name, config)

View Source

Specs

attach(
  WebhooksEmitter.Interface.emitter_id(),
  WebhooksEmitter.Interface.event_name(),
  WebhooksEmitter.Interface.emitter_config()
) :: :ok | {:error, :already_exists | :already_present}

Attaches a new emitter to the event.

emitter_id must be unique, if another emitter with the same ID already exists {:error, :already_exists} is returned.

Each emitter is a separate process that performs HTTP operations, while keeping a queue of events in order to performs retries. So events handled by the same emitter are processed sequentially. This means also that if a request fails and more events are to be handled by the emitter, the events are queued until the current delivery succeed or fails because of hitting the maximum retries. At this point the event is lost forever.

If same url needs to process many events, attach_many/3 can be used.

Link to this function

attach_many(emitter_id, event_names, config)

View Source

Specs

attach_many(
  WebhooksEmitter.Interface.emitter_id(),
  WebhooksEmitter.Interface.event_names(),
  WebhooksEmitter.Interface.emitter_config()
) :: :ok | {:error, :already_exists | :already_present}

Attaches a new emitter to a list of events.

Accepts a list of event names. Apart from that, works like attach/3.

Specs

Detaches an emitter.

Stops the process and removes it from the supervision tree, disregarding any queued message. Always returns :ok.

Link to this function

emit(event_name, payload, request_id \\ nil)

View Source

Specs

Emits an event, invoking all emitters attached to it.

Allows to set a request_id, that is inserted into the HTTP request as X-Webhooks-Delivery private header. If the request id is not set, or is nil, a new one is generated and returned as {:ok, UUID.uuid4()}. Otherwise just return the {:ok, request_id} tuple.

The map payload is parsed in order to be safe for json encoding, basically ensures that any key or value that cannot be directly converted to string is handled correctly by inspecting them.

So keys and values in the payload are converted like:

  • values as tuples are transformed to list
  • keys as tuples are transformed to list, then inspected to obtain a string
  • keys as list are inspected to obtain a string
  • refs are always inspected

Specs

list_emitters() :: [WebhooksEmitter.Interface.emitter_id()]

Returns all running emitter IDs attached to events.

Specs

Pauses an emitter.

Stops the process without removing from the supervision tree. Any queued message will be lost.

Specs

restart(WebhooksEmitter.Interface.emitter_id()) :: :ok | {:error, term()}

Restart an already present, but paused emitter

Specs

Checks if a given emitter ID is already started