EventBus

EventBus provides an event bus that uses the outbox pattern. Behind the scenes, it relies on Oban and ConCache.

Installation

If available in Hex, the package can be installed by adding event_bus to your list of dependencies in mix.exs:

def deps do
  [
    {:event_bus, "~> 0.1.0"}
  ]
end

Run tests

# run only once to setup the test DB
MIX_ENV=test mix test.setup

# actually run the tests
mix test

Setup

  1. Create a module that defines your event bus
  defmodule MyApp.EventBus do
    use EventBus, otp_app: :my_app
  end
  1. Add the required config for your EventBus, that is the Oban config
  config :my_app, MyApp.EventBus,
    oban: [
      engine: Oban.Engines.Basic,
      notifier: Oban.Notifiers.Postgres,
      repo: MyApp.Repo,
      plugins: [
        {Oban.Plugins.Lifeline, rescue_after: :timer.minutes(60)},
        {Oban.Plugins.Pruner, max_age: 60 * 60 * 24 * 7}
      ],
      queues: [
        event_bus: 2
      ]
    ]
  1. Create your first events
  defmodule MyApp.Events do
    use EventBus.Event

    defevent(MyEvent)
  end
  1. Create your first event handler
  defmodule MyApp.EventHandler do
    use EventBus.EventHandler,
      event_bus: MyApp.EventBus,
      events: [MyApp.Events.MyEvent]

    @impl EventBus.EventHandler
    def handle_event(%MyApp.Events.MyEvent{aggregate: %{"id" => aggregate_id}}) do
      # ... handle the event here
    end
  end
  1. Add your event bus to your supervision tree
  # add the event bus to your application children 

  def start(_type, _args) do 
    # ... 

    children = [
      # ...
      MyApp.EventBus,
      # ...
    ]

    # ...
  1. Add your event handlers to your supervision tree