Packages
eventstore
0.2.0
1.4.8
1.4.7
1.4.6
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.2
1.3.1
1.3.0
1.2.3
1.2.2
1.2.1
1.2.0
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-rc.0
0.17.0
0.16.2
0.16.1
0.16.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.11.0-rc.0
0.10.1
0.10.0
0.9.0
0.8.1
0.8.0
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.2
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.0
0.2.1
0.2.0
0.1.0
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
Event store using PostgreSQL for persistence.
Current section
Files
Jump to
Current section
Files
lib/event_store/subscriptions.ex
defmodule EventStore.Subscriptions do
@moduledoc """
Subscriptions holds state for subscribers interested in events appended to either a single stream or all streams
"""
use GenServer
require Logger
alias EventStore.Subscriptions
alias EventStore.Subscriptions.Subscription
defstruct all_stream: [], single_stream: %{}, supervisor: nil
@all_stream "$all"
def start_link do
GenServer.start_link(__MODULE__, %Subscriptions{}, name: __MODULE__)
end
def subscribe_to_stream(stream_uuid, subscription_name, subscriber) do
GenServer.call(__MODULE__, {:subscribe_to_stream, stream_uuid, subscription_name, subscriber})
end
def unsubscribe_from_stream(stream_uuid, subscription_name) do
GenServer.call(__MODULE__, {:unsubscribe_from_stream, stream_uuid, subscription_name})
end
def notify_events(stream_uuid, events) do
GenServer.cast(__MODULE__, {:notify_events, stream_uuid, events})
end
def init(%Subscriptions{} = subscriptions) do
{:ok, supervisor} = Subscriptions.Supervisor.start_link
subscriptions = %Subscriptions{subscriptions | supervisor: supervisor}
{:ok, subscriptions}
end
def handle_call({:subscribe_to_stream, stream_uuid, subscription_name, subscriber}, _from, %Subscriptions{supervisor: supervisor} = subscriptions) do
{:ok, subscription} = Subscriptions.Supervisor.subscribe_to_stream(supervisor, stream_uuid, subscription_name, subscriber)
Process.monitor(subscription)
subscriptions = case stream_uuid do
@all_stream -> append_all_stream_subscription(subscriptions, subscription)
stream_uuid -> append_single_stream_subscription(subscriptions, subscription, stream_uuid)
end
{:reply, {:ok, subscription}, subscriptions}
end
def handle_call({:unsubscribe_from_stream, stream_uuid, subscription_name}, _from, %Subscriptions{supervisor: supervisor} = subscriptions) do
{:reply, :ok, subscriptions}
end
def handle_cast({:notify_events, stream_uuid, events}, %Subscriptions{all_stream: all_stream, single_stream: single_stream} = subscriptions) do
interested_subscriptions = all_stream ++ Map.get(single_stream, stream_uuid, [])
interested_subscriptions
|> Enum.each(&Subscription.notify_events(&1, events))
{:noreply, subscriptions}
end
def handle_info({:DOWN, ref, :process, pid, reason}, %Subscriptions{all_stream: all_stream, single_stream: single_stream} = subscriptions) do
Logger.warn "subscription down due to: #{reason}"
all_stream = List.delete(all_stream, pid)
single_stream = single_stream
|> Enum.map(fn {key, value} -> {key, List.delete(value, pid)} end)
|> Map.new
{:noreply, %Subscriptions{subscriptions | all_stream: all_stream, single_stream: single_stream}}
end
defp append_all_stream_subscription(%Subscriptions{all_stream: all_stream} = subscriptions, subscription) do
%Subscriptions{subscriptions | all_stream: [subscription | all_stream]}
end
defp append_single_stream_subscription(%Subscriptions{single_stream: single_stream} = subscriptions, subscription, stream_uuid) do
{_, single_stream} = Map.get_and_update(single_stream, stream_uuid, fn current_value ->
new_value = case current_value do
nil -> [subscription]
current_value -> [subscription | current_value]
end
{current_value, new_value}
end)
%Subscriptions{subscriptions | single_stream: single_stream}
end
defp remove_all_stream_subscription(%Subscriptions{all_stream: all_stream} = subscriptions, subscription_name) do
subscriptions
end
end