Skip to content

Instantly share code, notes, and snippets.

@jmerriweather
Created December 12, 2016 22:08
Show Gist options
  • Save jmerriweather/00a23e516116d32b54da648b901f3d4c to your computer and use it in GitHub Desktop.
Save jmerriweather/00a23e516116d32b54da648b901f3d4c to your computer and use it in GitHub Desktop.
defmodule Foo do
use Application
# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec, warn: false
# Define workers and child supervisors to be supervised
children = [
# Starts a worker by calling: Foo.Worker.start_link(arg1, arg2, arg3)
worker(Foo.Service, []),
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Foo.Supervisor]
Supervisor.start_link(children, opts)
end
end
defmodule Foo.Service do
use GenServer
def start_link do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end
def init(:ok) do
{:ok, :ok}
end
def check do
GenServer.call(__MODULE__, {:check})
end
def test_args({:abc}) do
GenServer.call(__MODULE__, {:abc})
end
def handle_call({:abc}, _from, :ok) do
{:reply, :ok, :ok}
end
def handle_call({:check}, _from, :ok) do
{:reply, :ok, :ok}
end
end
Interactive Elixir (1.3.4) - press Ctrl+C to exit (type h() ENTER for help)
iex> pid = Process.whereis(Foo.Service)
#PID<0.146.0>
iex> Foo.Service.check
:ok
iex> GenServer.call(pid, {:fds})
** (exit) exited in: GenServer.call(#PID<0.146.0>, {:fds}, 5000)
** (EXIT) an exception was raised:
** (FunctionClauseError) no function clause matching in Foo.Service.handle_call/3
(foo) lib/foo_service.ex:20: Foo.Service.handle_call({:fds}, {#PID<0.147.0>, #Reference<0.0.4.491>}, :ok)
(stdlib) gen_server.erl:615: :gen_server.try_handle_call/4
(stdlib) gen_server.erl:647: :gen_server.handle_msg/5
(stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
08:06:01.326 [error] GenServer Foo.Service terminating
** (FunctionClauseError) no function clause matching in Foo.Service.handle_call/3
(foo) lib/foo_service.ex:20: Foo.Service.handle_call({:fds}, {#PID<0.147.0>, #Reference<0.0.4.491>}, :ok)
(stdlib) gen_server.erl:615: :gen_server.try_handle_call/4
(stdlib) gen_server.erl:647: :gen_server.handle_msg/5
(stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: {:fds}
State: :ok
(elixir) lib/gen_server.ex:604: GenServer.call/3
iex> pid = Process.whereis(Foo.Service)
#PID<0.152.0>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment