This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get “/users/:id”, | |
private: %{validate: %{id: &validate_integer/1, active: &validate_boolean/1}} do | |
# Controller logic goes here | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Dispatcher do | |
use GenServer | |
@timeout 10000 | |
# . | |
# . | |
# . | |
# Same stuff as before |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |