Packages
eventstore
1.4.8
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/config/store.ex
defmodule EventStore.Config.Store do
@moduledoc false
# Store and read runtime configuration associated with an event store.
use GenServer
def start_link(init_arg) do
GenServer.start_link(__MODULE__, init_arg, name: __MODULE__)
end
def associate(name, pid, event_store, config)
when is_atom(name) and is_pid(pid) and is_atom(event_store) do
GenServer.call(__MODULE__, {:associate, name, pid, event_store, config})
end
@doc """
Get the configuration associated with all running event store instances.
"""
def all do
:ets.tab2list(__MODULE__)
|> Enum.map(fn {name, _pid, _ref, event_store, _config} -> {event_store, name} end)
end
@doc """
Get the configuration associated with the given event store.
"""
def get(name) when is_atom(name), do: lookup(name)
@doc """
Get the value of the config setting for the given event store.
"""
def get(name, setting) when is_atom(name) and is_atom(setting) do
lookup(name) |> Keyword.get(setting)
end
@impl GenServer
def init(_init_arg) do
table = :ets.new(__MODULE__, [:named_table, read_concurrency: true])
{:ok, table}
end
@impl GenServer
def handle_call({:associate, name, pid, event_store, config}, _from, table) do
ref = Process.monitor(pid)
true = :ets.insert(table, {name, pid, ref, event_store, config})
{:reply, :ok, table}
end
@impl GenServer
def handle_info({:DOWN, ref, _type, pid, _reason}, table) do
[[name]] = :ets.match(table, {:"$1", pid, ref, :_, :_})
:ets.delete(table, name)
{:noreply, table}
end
defp lookup(name) do
:ets.lookup_element(__MODULE__, name, 5)
rescue
ArgumentError ->
raise "could not lookup #{inspect(name)} because it was not started or it does not exist"
end
end