/
event_store.ex
292 lines (234 loc) · 8.18 KB
/
event_store.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
defmodule Commanded.EventStore do
@moduledoc """
Use the event store configured for a Commanded application.
### Telemetry Events
Adds telemetry events for the following functions. Events are emitted in the form
`[:commanded, :event_store, event]` with their spannable postfixes (`start`, `stop`, `exception`)
* ack_event/3
* adapter/2
* append_to_stream/4
* delete_snapshot/2
* delete_subscription/3
* read_snapshot/2
* record_snapshot/2
* stream_forward/2
* stream_forward/3
* stream_forward/4
* subscribe/2
* subscribe_to/5
* subscribe_to/6
* unsubscribe/2
"""
alias Commanded.Application
alias Commanded.Event.Upcast
@type application :: Application.t()
@type config :: Keyword.t()
@doc """
Append one or more events to a stream atomically.
"""
def append_to_stream(application, stream_uuid, expected_version, events, opts \\ []) do
meta = %{
application: application,
stream_uuid: stream_uuid,
expected_version: expected_version
}
span(:append_to_stream, meta, fn ->
{adapter, adapter_meta} = Application.event_store_adapter(application)
if function_exported?(adapter, :append_to_stream, 5) do
adapter.append_to_stream(adapter_meta, stream_uuid, expected_version, events, opts)
else
adapter.append_to_stream(
adapter_meta,
stream_uuid,
expected_version,
events
)
end
end)
end
@doc """
Streams events from the given stream, in the order in which they were originally written.
"""
def stream_forward(application, stream_uuid, start_version \\ 0, read_batch_size \\ 1_000) do
meta = %{
application: application,
stream_uuid: stream_uuid,
start_version: start_version,
read_batch_size: read_batch_size
}
span(:stream_forward, meta, fn ->
{adapter, adapter_meta} = Application.event_store_adapter(application)
case adapter.stream_forward(
adapter_meta,
stream_uuid,
start_version,
read_batch_size
) do
{:error, _error} = error ->
error
stream ->
Upcast.upcast_event_stream(stream, additional_metadata: %{application: application})
end
end)
end
@doc """
Create a transient subscription to a single event stream.
The event store will publish any events appended to the given stream to the
`subscriber` process as an `{:events, events}` message.
The subscriber does not need to acknowledge receipt of the events.
"""
def subscribe(application, stream_uuid) do
span(:subscribe, %{application: application, stream_uuid: stream_uuid}, fn ->
{adapter, adapter_meta} = Application.event_store_adapter(application)
adapter.subscribe(adapter_meta, stream_uuid)
end)
end
@doc """
Create a persistent subscription to an event stream.
To subscribe to all events appended to any stream use `:all` as the stream
when subscribing.
The event store will remember the subscribers last acknowledged event.
Restarting the named subscription will resume from the next event following
the last seen.
Once subscribed, the subscriber process should be sent a
`{:subscribed, subscription}` message to allow it to defer initialisation
until the subscription has started.
The subscriber process will be sent all events persisted to the stream. It
will receive a `{:events, events}` message for each batch of events persisted
for a single aggregate.
The subscriber must ack each received, and successfully processed event, using
`Commanded.EventStore.ack_event/3`.
## Examples
Subscribe to all streams:
{:ok, subscription} =
Commanded.EventStore.subscribe_to(MyApp, :all, "Example", self(), :current)
Subscribe to a single stream:
{:ok, subscription} =
Commanded.EventStore.subscribe_to(MyApp, "stream1", "Example", self(), :origin)
"""
def subscribe_to(
application,
stream_uuid,
subscription_name,
subscriber,
start_from,
opts \\ []
) do
meta = %{
application: application,
stream_uuid: stream_uuid,
subscription_name: subscription_name,
subscriber: subscriber,
start_from: start_from
}
span(:subscribe_to, meta, fn ->
{adapter, adapter_meta} = Application.event_store_adapter(application)
if function_exported?(adapter, :subscribe_to, 6) do
adapter.subscribe_to(
adapter_meta,
stream_uuid,
subscription_name,
subscriber,
start_from,
opts
)
else
adapter.subscribe_to(
adapter_meta,
stream_uuid,
subscription_name,
subscriber,
start_from
)
end
end)
end
@doc """
Acknowledge receipt and successful processing of the given event received from
a subscription to an event stream.
"""
def ack_event(application, subscription, event) do
meta = %{application: application, subscription: subscription, event: event}
span(:ack_event, meta, fn ->
{adapter, adapter_meta} = Application.event_store_adapter(application)
adapter.ack_event(adapter_meta, subscription, event)
end)
end
@doc """
Unsubscribe an existing subscriber from event notifications.
This will not delete the subscription.
## Example
:ok = Commanded.EventStore.unsubscribe(MyApp, subscription)
"""
def unsubscribe(application, subscription) do
span(:unsubscribe, %{application: application, subscription: subscription}, fn ->
{adapter, adapter_meta} = Application.event_store_adapter(application)
adapter.unsubscribe(adapter_meta, subscription)
end)
end
@doc """
Delete an existing subscription.
## Example
:ok = Commanded.EventStore.delete_subscription(MyApp, :all, "Example")
"""
def delete_subscription(application, subscribe_to, handler_name) do
meta = %{application: application, subscribe_to: subscribe_to, handler_name: handler_name}
span(:delete_subscription, meta, fn ->
{adapter, adapter_meta} = Application.event_store_adapter(application)
adapter.delete_subscription(adapter_meta, subscribe_to, handler_name)
end)
end
@doc """
Read a snapshot, if available, for a given source.
"""
def read_snapshot(application, source_uuid) do
{adapter, adapter_meta} = Application.event_store_adapter(application)
span(:read_snapshot, %{application: application, source_uuid: source_uuid}, fn ->
adapter.read_snapshot(adapter_meta, source_uuid)
end)
end
@doc """
Record a snapshot of the data and metadata for a given source
"""
def record_snapshot(application, snapshot) do
{adapter, adapter_meta} = Application.event_store_adapter(application)
span(:record_snapshot, %{application: application, snapshot: snapshot}, fn ->
adapter.record_snapshot(adapter_meta, snapshot)
end)
end
@doc """
Delete a previously recorded snapshot for a given source
"""
def delete_snapshot(application, source_uuid) do
{adapter, adapter_meta} = Application.event_store_adapter(application)
span(:delete_snapshot, %{application: application, source_uuid: source_uuid}, fn ->
adapter.delete_snapshot(adapter_meta, source_uuid)
end)
end
@doc """
Get the configured event store adapter for the given application.
"""
@spec adapter(application, config) :: {module, config}
def adapter(application, config)
def adapter(application, nil) do
raise ArgumentError, "missing :event_store config for application " <> inspect(application)
end
def adapter(application, config) do
{adapter, config} = Keyword.pop(config, :adapter)
unless Code.ensure_loaded?(adapter) do
raise ArgumentError,
"event store adapter " <>
inspect(adapter) <>
" used by application " <>
inspect(application) <>
" was not compiled, ensure it is correct and it is included as a project dependency"
end
{adapter, config}
end
# TODO convert to macro
defp span(event, meta, func) do
:telemetry.span([:commanded, :event_store, event], meta, fn ->
{func.(), meta}
end)
end
end