WardrikEvents. Listener behaviour
(wardrik_events v0.1.0-rc.1)
Copy Markdown
Follows agents' events into your app.
A listener hands each event to handle/2, with the cursor past it. Save the cursor with your
work, and return it from cursor/1. The listener then carries on from there after a restart.
defmodule MyApp.Runs do
use WardrikEvents.Listener
@impl true
def handle(event, listener) do
MyApp.Repo.transaction(fn ->
MyApp.Repo.insert_all("agent_events", [MyApp.Runs.row(event)], on_conflict: :nothing)
MyApp.Cursors.put!("runs", listener.cursor)
end)
:ok
end
@impl true
def cursor(_listener), do: MyApp.Cursors.get("runs")
end
children = [{MyApp.Runs, events: MyApp.Events, types: ["wardrik.run.completed"]}]Options
:events- required. The name of yourWardrikEvents.:resources- patterns of the logs to follow.["*"], every agent, unless set.:types- the event types to take. Every type unless set. Awardrik.events.droppedalways comes through.:after- the cursor to start after, if you don't definecursor/1. What's written next unless set.
Your module's own options can sit beside these. They're in the listener's :options.
Delivery
Each event is handed over at least once.
- Save the cursor in the same transaction as your work.
- If
handle/2raises or returns anything but:ok, the listener restarts. - On restart it calls
cursor/1again, and reads after what it returns.
So drop a repeat by the event's id.
Summary
Callbacks
Returns the cursor to read after as the listener starts, or nil for what's written next. Returns :after unless defined.
Handles event. Returns :ok once it's done.
Types
Callbacks
Returns the cursor to read after as the listener starts, or nil for what's written next. Returns :after unless defined.
@callback handle(WardrikEvents.CloudEvent.t(), t()) :: :ok
Handles event. Returns :ok once it's done.