From 4f1f09dc4298045920d1d3ce5bbb8060ead77f84 Mon Sep 17 00:00:00 2001 From: Uday Singh Date: Mon, 9 Oct 2017 15:41:07 -0400 Subject: [PATCH] Run code formatter on lib/elixir/lib/supervisor.ex --- lib/elixir/lib/supervisor.ex | 101 ++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 42 deletions(-) diff --git a/lib/elixir/lib/supervisor.ex b/lib/elixir/lib/supervisor.ex index eff5c044224..7e52b51e6d6 100644 --- a/lib/elixir/lib/supervisor.ex +++ b/lib/elixir/lib/supervisor.ex @@ -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 @@ -532,9 +536,10 @@ 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 @@ -542,13 +547,13 @@ defmodule Supervisor do # 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. @@ -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) @@ -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} @@ -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: @@ -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 @@ -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 @@ -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 """ @@ -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: @@ -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 @@ -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}) @@ -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 @@ -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 """