so do you think it’s good idea use something like this:
defmodule Transactions do
use GenServer, restart: :transient, shutdown: 10_000
alias Transactions.Current
def start_link(state \\ %{}) do
GenServer.start_link(__MODULE__, state, name: __MODULE__)
end
def init(state) do
perform_with_backup(&perform_something/1, Current.get_current())
{:ok, state}
end
def handle_call({:something, params}, _from, state) do
resp =
perform_with_backup(&perform_something/1, params)
{:reply, resp, state}
end
defp perform_with_backup(procedure, params) do
Current.put_current(params)
resp = procedure.(params)
Current.clean_current()
resp
end
defp perform_something(params) do
# do something
end
end
and
defmodule Transactions.Current do
use Agent
def start_link(_) do
Agent.start_link(fn -> %{} end, name: __MODULE__)
end
def put_current(%{} = new_state) do
Agent.update(__MODULE__, fn _state -> new_state end)
end
def get_current() do
Agent.get(__MODULE__, fn state -> state end)
end
def clean_current() do
Agent.update(__MODULE__, fn _state -> %{} end)
end
end


















