Disco v0.1.3 Disco.EventConsumer behaviour View Source

The event consumer specification.

An event consumer in Disco is a module that exposes a process/1 function to handle a given set of event types. The common use cases are projections and policies.

Projections

A projection is the component that builds or updates a read model, usually optimized for queries. It’s related to the Q in CQRS pattern. In this scenario, process/1 will model the data optimizing it for read. One of the most powerful advantages is that, later in the future, you might want to build new read models, or rebuild old ones from scratch after some iteration. It will suffice to change process/1 implementation and re-process all the events from scratch.

Policies

A policy is an action to take when some event has happened. You should think carefully wether you want to either put that action in a policy or wrap it in a command. This is because somewhere in the future you might need to re-process all the events from scratch, thus the action you want to take should be repeatable without having annoying side effects.

How it works by default

This module implements a GenServer behaviour with all the callbacks to poll a Disco.EventStore through a Disco.EventStore.Client at given intervals.

Polling the Disco.EventStore is a very simple solution that offers more guarantees for consuming all the events without leaving somothing behind. By default, polling interval is set to 2000 ms, however it’s possible to set a different values globally orper-consumer. Here’s how to do it:

# config/config.exs

# set polling interval for all the event consumers
config :disco, :default_polling_interval, 3000

# set polling interval only for a specific event consumer
config :disco, MyApp.SomeEventConsumer, 10000

Define an event consumer

defmodule MyApp.SomePolicy do
  use Disco.EventConsumer,
    event_store_client: Application.get_env(:my_app, :event_store_client),
    events: ["SomethingHappened"]

  def process(%{type: "SomethingHappened", payload: payload} = event) do
    # do something with this event
    :ok
  end
end

Link to this section Summary

Functions

Defines the default callbacks to implement the Disco.EventConsumer behaviour

Link to this section Types

Link to this type error() View Source
error() :: {:error, reason :: any()}
Link to this type retry() View Source
retry() :: {:retry, reason :: any()}

Link to this section Functions

Link to this macro __using__(opts) View Source (macro)

Defines the default callbacks to implement the Disco.EventConsumer behaviour.

Options

  • :events - a list of event types to listen.
  • :event_store_client - a module that implements Disco.EventStore.Client behaviour.

Link to this section Callbacks

Link to this callback process(event) View Source
process(event :: map()) :: :ok | {:ok, result :: any()} | error() | retry()