Because EventBus uses ETS for temporary storage, you must implement your own persistence logic if you need to keep event data long-term.
The recommended pattern is to subscribe a dedicated module to all event types using the [."*" ] wildcard topic pattern, then save the fetched event data to a persistent database.
Follow this pattern:
- Subscribe to
[."*" ]. - In the
process/1 callback, fetch the event data using EventBus.fetch_event/1. - Save the data to your persistent store.
- Call
EventBus.mark_as_completed/1 to notify the Observation Manager.
# 1. Subscribe to all topics
EventBus.subscribe({MyDataStore, [.".*" ]})
# 2. Implement the subscriber module
defmodule MyDataStore do
# The process/1 callback receives the event shadow
def process({topic, id} = event_shadow) do
GenServer.cast(__MODULE__, event_shadow)
:ok
end
def handle_cast({topic, id}, state) do
# 3. Fetch the actual event data from ETS
event = EventBus.fetch_event({topic, id})
# 4. Write logic to save event_data to a persistent store (e.g. Postgres)
# ...
# 5. Mark as completed so the Observation Manager can clean up ETS
EventBus.mark_as_completed({__MODULE__, {topic, id}})
{:noreply, state}
end
end
EventBus.subscribe({MyDataStore, [.".*" ]})
defmodule MyDataStore do
def process({topic, id} = event_shadow) do
GenServer.cast(__MODULE__, event_shadow)
:ok
end
def handle_cast({topic, id}, state) do
event = EventBus.fetch_event({topic, id})
# write your logic to save event_data to a persistent store
EventBus.mark_as_completed({__MODULE__, {topic, id}})
{:noreply, state}
end
end