Skip to content

Instantly share code, notes, and snippets.

get “/users/:id”,
private: %{validate: %{id: &validate_integer/1, active: &validate_boolean/1}} do
# Controller logic goes here
end
iex> Dispatcher.run d, "a", fn -> 1/0 end
:ok
iex>
17:41:39.336 [error] Process #PID<0.97.0> raised an exception
** (ArithmeticError) bad argument in arithmetic expression
:erlang./(1, 0)
lib/dispatcher.ex:27: anonymous fn/3 in Dispatcher.handle_cast/2
iex> Dispatcher.result d, "a"
{:error,
{:badarith,
defmodule Dispatcher do
use GenServer
@timeout 10000
def start() do
GenServer.start(__MODULE__, [])
end
def result(s, task_id), do: GenServer.call(s, {:result, task_id})
iex> Dispatcher.run(d, "a", fn -> 1/0 end)
:ok
iex>
17:38:19.429 [error] Process #PID<0.152.0> raised an exception
** (ArithmeticError) bad argument in arithmetic expression
:erlang./(1, 0)
lib/dispatcher.ex:27: anonymous fn/3 in Dispatcher.handle_cast/2
iex> Dispatcher.result(d, "a")
:pending
iex> Dispatcher.result(d, "a")
iex> Dispatcher.run(d, "a", fn -> :timer.sleep(12000); "done" end)
:ok
iex> Dispatcher.result(d, "a")
:pending
iex> Dispatcher.result(d, "a")
:pending
iex> Dispatcher.result(d, "a")
:timeout
iex> Dispatcher.result(d, "a")
nil
defmodule Dispatcher do
use GenServer
@timeout 10000
# .
# .
# .
# Same stuff as before
iex> Dispatcher.run(d, "a", fn -> :timer.sleep(8000); "done" end)
:ok
iex> Dispatcher.result(d, "a")
:pending
iex> Dispatcher.result(d, "a")
:pending
iex> Dispatcher.result(d, "a")
"done"
iex> Dispatcher.result(d, "a")
nil
defmodule Dispatcher do
.
.
.
# Everything until handle_call stays the same
def handle_call({:result, task_id}, _from, results) do
case Map.fetch(results, task_id) do
{:ok, :pending} -> {:reply, :pending, results}
{:ok, result} -> {:reply, result, Map.delete(results, task_id)}
@erez-rabih
erez-rabih / dispatcher-0-iex-long-running.ex
Last active January 31, 2018 20:57
dispatcher-0-long running problem
iex> Dispatcher.run(d, “c”, fn -> :timer.sleep(8000); “finished” end) 
:ok
iex> Dispatcher.result d, “c”
** (exit) exited in: GenServer.call(#PID<0.96.0>, {:result, “c”}, 5000)
 ** (EXIT) time out
 (elixir) lib/gen_server.ex:831: GenServer.call/3
@erez-rabih
erez-rabih / dispatche-0-ies.exs
Last active January 31, 2018 19:57
dispatcher-0-iex
iex> {:ok, d} = Dispatcher.start()
{:ok, #PID<0.96.0>}
iex> Dispatcher.run(d, "a", fn -> 1 + 1 end)
:ok
iex> Dispatcher.run(d, "b", fn -> 2 + 2 end)
:ok
iex> Dispatcher.result(d, "a")
2
iex> Dispatcher.result(d, "b")
4