Skip to content

Run code formatter on lib/elixir/lib/supervisor.ex #6758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 59 additions & 42 deletions lib/elixir/lib/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -506,16 +506,20 @@ defmodule Supervisor do
Callback invoked to start the supervisor and during hot code upgrades.
"""
@callback init(args :: term) ::
{:ok, {:supervisor.sup_flags, [:supervisor.child_spec]}} |
:ignore
{:ok, {:supervisor.sup_flags(), [:supervisor.child_spec()]}}
| :ignore

@typedoc "Return values of `start_link` functions"
@type on_start :: {:ok, pid} | :ignore |
{:error, {:already_started, pid} | {:shutdown, term} | term}
@type on_start ::
{:ok, pid}
| :ignore
| {:error, {:already_started, pid} | {:shutdown, term} | term}

@typedoc "Return values of `start_child` functions"
@type on_start_child :: {:ok, child} | {:ok, child, info :: term} |
{:error, {:already_started, child} | :already_present | term}
@type on_start_child ::
{:ok, child}
| {:ok, child, info :: term}
| {:error, {:already_started, child} | :already_present | term}

@type child :: pid | :undefined

Expand All @@ -532,23 +536,24 @@ defmodule Supervisor do
@type supervisor :: pid | name | {atom, node}

@typedoc "Options given to `start_link/2` and `init/2`"
@type flag :: {:strategy, strategy} |
{:max_restarts, non_neg_integer} |
{:max_seconds, pos_integer}
@type flag ::
{:strategy, strategy}
| {:max_restarts, non_neg_integer}
| {:max_seconds, pos_integer}

@typedoc "Supported strategies"
@type strategy :: :simple_one_for_one | :one_for_one | :one_for_all | :rest_for_one

# Note we have inlined all types for readability
@typedoc "The supervisor specification"
@type child_spec :: %{
required(:id) => term(),
required(:start) => {module(), function(), [term()]},
optional(:restart) => :permanent | :transient | :temporary,
optional(:shutdown) => :brutal_kill | non_neg_integer() | :infinity,
optional(:type) => :worker | :supervisor,
optional(:modules) => [module()] | :dynamic
}
required(:id) => term(),
required(:start) => {module(), function(), [term()]},
optional(:restart) => :permanent | :transient | :temporary,
optional(:shutdown) => :brutal_kill | non_neg_integer() | :infinity,
optional(:type) => :worker | :supervisor,
optional(:modules) => [module()] | :dynamic
}

@doc """
Starts a supervisor with the given children.
Expand Down Expand Up @@ -579,7 +584,7 @@ defmodule Supervisor do
process and exits not only on crashes but also if the parent process exits
with `:normal` reason.
"""
@spec start_link([:supervisor.child_spec | {module, term} | module], options) :: on_start
@spec start_link([:supervisor.child_spec() | {module, term} | module], options) :: on_start
def start_link(children, options) when is_list(children) do
{sup_opts, start_opts} = Keyword.split(options, [:strategy, :max_seconds, :max_restarts])
start_link(Supervisor.Default, {children, sup_opts}, start_opts)
Expand Down Expand Up @@ -621,11 +626,12 @@ defmodule Supervisor do
is allowed within 5 seconds. Check the `Supervisor` module for a detailed
description of the available strategies.
"""
@spec init([:supervisor.child_spec | {module, term} | module], flag) :: {:ok, tuple}
@spec init([:supervisor.child_spec() | {module, term} | module], flag) :: {:ok, tuple}
def init(children, options) when is_list(children) and is_list(options) do
unless strategy = options[:strategy] do
raise ArgumentError, "expected :strategy option to be given"
end

intensity = Keyword.get(options, :max_restarts, 3)
period = Keyword.get(options, :max_seconds, 5)
flags = %{strategy: strategy, intensity: intensity, period: period}
Expand All @@ -635,25 +641,30 @@ defmodule Supervisor do
defp init_child(module) when is_atom(module) do
init_child({module, []})
end

defp init_child({module, arg}) when is_atom(module) do
try do
module.child_spec(arg)
rescue
e in UndefinedFunctionError ->
case System.stacktrace do
case System.stacktrace() do
[{^module, :child_spec, [^arg], _} | _] ->
raise ArgumentError, child_spec_error(module)

stack ->
reraise e, stack
end
end
end

defp init_child(map) when is_map(map) do
map
end

defp init_child({_, _, _, _, _, _} = tuple) do
tuple
end

defp init_child(other) do
raise ArgumentError, """
supervisors expect each child to be one of:
Expand All @@ -663,14 +674,14 @@ defmodule Supervisor do
* a child specification as a map with at least the :id and :start fields
* or a tuple with 6 elements generated by Supervisor.Spec (deprecated)

Got: #{inspect other}
Got: #{inspect(other)}
"""
end

defp child_spec_error(module) do
if Code.ensure_loaded?(module) do
"""
The module #{inspect module} was given as a child to a supervisor
The module #{inspect(module)} was given as a child to a supervisor
but it does not implement child_spec/1.

If you own the given module, please define a child_spec/1 function
Expand All @@ -695,14 +706,14 @@ defmodule Supervisor do
child, you will have to pass a child specification as a map:

%{
id: #{inspect module},
start: {#{inspect module}, :start_link, [arg1, arg2]}
id: #{inspect(module)},
start: {#{inspect(module)}, :start_link, [arg1, arg2]}
}

See the Supervisor documentation for more information.
"""
else
"The module #{inspect module} was given as a child to a supervisor but it does not exist."
"The module #{inspect(module)} was given as a child to a supervisor but it does not exist."
end
end

Expand Down Expand Up @@ -744,17 +755,19 @@ defmodule Supervisor do
def child_spec(module_or_map, overrides)

def child_spec({_, _, _, _, _, _} = tuple, _overrides) do
raise ArgumentError, "old tuple-based child specification #{inspect tuple} " <>
"is not supported in Supervisor.child_spec/2"
raise ArgumentError,
"old tuple-based child specification #{inspect(tuple)} " <>
"is not supported in Supervisor.child_spec/2"
end

def child_spec(module_or_map, overrides) do
Enum.reduce overrides, init_child(module_or_map), fn
Enum.reduce(overrides, init_child(module_or_map), fn
{key, value}, acc when key in [:id, :start, :restart, :shutdown, :type, :modules] ->
Map.put(acc, key, value)

{key, _value}, _acc ->
raise ArgumentError, "unknown key #{inspect key} in child specification override"
end
raise ArgumentError, "unknown key #{inspect(key)} in child specification override"
end)
end

@doc """
Expand All @@ -776,17 +789,21 @@ defmodule Supervisor do
section in the `GenServer` module docs.
"""
@spec start_link(module, term) :: on_start
@spec start_link(module, term, GenServer.options) :: on_start
@spec start_link(module, term, GenServer.options()) :: on_start
def start_link(module, arg, options \\ []) when is_list(options) do
case Keyword.get(options, :name) do
nil ->
:supervisor.start_link(module, arg)

atom when is_atom(atom) ->
:supervisor.start_link({:local, atom}, module, arg)

{:global, _term} = tuple ->
:supervisor.start_link(tuple, module, arg)

{:via, via_module, _term} = tuple when is_atom(via_module) ->
:supervisor.start_link(tuple, module, arg)

other ->
raise ArgumentError, """
expected :name option to be one of:
Expand Down Expand Up @@ -833,7 +850,7 @@ defmodule Supervisor do
"""
# TODO: Once we add DynamicSupervisor, we need to enforce receiving
# a map here and deprecate the list and tuple formats.
@spec start_child(supervisor, :supervisor.child_spec | [term]) :: on_start_child
@spec start_child(supervisor, :supervisor.child_spec() | [term]) :: on_start_child
def start_child(supervisor, child_spec_or_args) do
call(supervisor, {:start_child, child_spec_or_args})
end
Expand Down Expand Up @@ -905,8 +922,7 @@ defmodule Supervisor do

This operation is not supported by `:simple_one_for_one` supervisors.
"""
@spec restart_child(supervisor, term()) ::
{:ok, child} | {:ok, child, term} | {:error, error}
@spec restart_child(supervisor, term()) :: {:ok, child} | {:ok, child, term} | {:error, error}
when error: :not_found | :simple_one_for_one | :running | :restarting | term
def restart_child(supervisor, child_id) do
call(supervisor, {:restart_child, child_id})
Expand All @@ -932,11 +948,9 @@ defmodule Supervisor do
* `modules` - as specified by the child specification

"""
@spec which_children(supervisor) ::
[{term() | :undefined,
child | :restarting,
:worker | :supervisor,
:supervisor.modules}]
@spec which_children(supervisor) :: [
{term() | :undefined, child | :restarting, :worker | :supervisor, :supervisor.modules()}
]
def which_children(supervisor) do
call(supervisor, :which_children)
end
Expand All @@ -958,11 +972,14 @@ defmodule Supervisor do
are still alive

"""
@spec count_children(supervisor) ::
%{specs: non_neg_integer, active: non_neg_integer,
supervisors: non_neg_integer, workers: non_neg_integer}
@spec count_children(supervisor) :: %{
specs: non_neg_integer,
active: non_neg_integer,
supervisors: non_neg_integer,
workers: non_neg_integer
}
def count_children(supervisor) do
call(supervisor, :count_children) |> :maps.from_list
call(supervisor, :count_children) |> :maps.from_list()
end

@doc """
Expand Down