summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@plataformatec.com.br>2015-10-23 14:53:40 +0200
committerJosé Valim <jose.valim@plataformatec.com.br>2015-10-23 14:53:40 +0200
commit86b6e7047803c45f0e4fabe35473df335a2b5f7c (patch)
treeebc52da77336f766559eb1ddfd7695cb3c8498b8
parentc0d561bf6ff653afa9f24c2031b9241034eb6789 (diff)
downloadelixir-86b6e7047803c45f0e4fabe35473df335a2b5f7c.tar.gz
Add Supervisor.stop
-rw-r--r--lib/elixir/lib/agent/server.ex15
-rw-r--r--lib/elixir/lib/supervisor.ex16
-rw-r--r--lib/elixir/test/elixir/supervisor_test.exs8
3 files changed, 19 insertions, 20 deletions
diff --git a/lib/elixir/lib/agent/server.ex b/lib/elixir/lib/agent/server.ex
index 3bef67e4c..c432bfed8 100644
--- a/lib/elixir/lib/agent/server.ex
+++ b/lib/elixir/lib/agent/server.ex
@@ -23,10 +23,6 @@ defmodule Agent.Server do
{:reply, :ok, run(fun, [state])}
end
- def handle_call(:stop, _from, state) do
- {:stop, :normal, :ok, state}
- end
-
def handle_call(msg, from, state) do
super(msg, from, state)
end
@@ -43,17 +39,6 @@ defmodule Agent.Server do
{:ok, run(fun, [state])}
end
- def terminate(_reason, _state) do
- # There is a race condition if the agent is
- # restarted too fast and it is registered.
- try do
- self |> :erlang.process_info(:registered_name) |> elem(1) |> Process.unregister
- rescue
- _ -> :ok
- end
- :ok
- end
-
defp initial_call(mfa) do
_ = Process.put(:"$initial_call", get_initial_call(mfa))
:ok
diff --git a/lib/elixir/lib/supervisor.ex b/lib/elixir/lib/supervisor.ex
index 9d6156ad5..231a4339e 100644
--- a/lib/elixir/lib/supervisor.ex
+++ b/lib/elixir/lib/supervisor.ex
@@ -463,6 +463,22 @@ defmodule Supervisor do
call(supervisor, :count_children) |> :maps.from_list
end
+ @doc """
+ Stops the supervisor with the given `reason`.
+
+ It returns `:ok` if the supervisor terminates with the given
+ reason, if it terminates with another reason, the call will
+ exit.
+
+ This function keeps OTP semantics regarding error reporting.
+ If the reason is any other than `:normal`, `:shutdown` or
+ `{:shutdown, _}`, an error report will be logged.
+ """
+ @spec stop(supervisor, reason :: term, timeout) :: :ok
+ def stop(supervisor, reason \\ :normal, timeout \\ 5_000) do
+ :gen.stop(supervisor, reason, timeout)
+ end
+
@compile {:inline, call: 2}
defp call(supervisor, req) do
diff --git a/lib/elixir/test/elixir/supervisor_test.exs b/lib/elixir/test/elixir/supervisor_test.exs
index 2c73a05f8..0bccbc031 100644
--- a/lib/elixir/test/elixir/supervisor_test.exs
+++ b/lib/elixir/test/elixir/supervisor_test.exs
@@ -51,8 +51,7 @@ defmodule SupervisorTest do
wait_until_registered(:dyn_stack)
assert GenServer.call(:dyn_stack, :pop) == :hello
-
- Process.exit(pid, :normal)
+ Supervisor.stop(pid)
end
test "start_link/3" do
@@ -60,7 +59,7 @@ defmodule SupervisorTest do
wait_until_registered(:stack_sup)
assert GenServer.call(:stat_stack, :pop) == :hello
- Process.exit(pid, :normal)
+ Supervisor.stop(pid)
end
test "*_child functions" do
@@ -86,8 +85,7 @@ defmodule SupervisorTest do
assert Supervisor.terminate_child(pid, Stack) == :ok
assert Supervisor.delete_child(pid, Stack) == :ok
-
- Process.exit(pid, :normal)
+ Supervisor.stop(pid)
end
defp wait_until_registered(name) do