# GenServer fail safe queue

**URL:** <https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321>\
**Category:** Questions\
**Tags:** queues, genserver\
**Created:** [February 7, 2018, 10:46am UTC](https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321 "2018-02-07T10:46:02Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![quatermain](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/quatermain/32/21781_2.png) [@quatermain](https://forum.elixirforum.com/u/quatermain)\
**Post date:** [February 7, 2018, 10:55am UTC](https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321/3 "2018-02-07T10:55:10Z")

</div>

so do you think it’s good idea use something like this:

```elixir
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

```elixir
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

```

---

_[View the full topic](https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321)._
