From 29a74f8ad4e428d90cd5f8bb346da2d26bc7ca2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 28 Jan 2018 11:55:58 -0200 Subject: [PATCH] Improve docs for dynamic and task supervisors --- lib/elixir/lib/dynamic_supervisor.ex | 18 +++++++++----- lib/elixir/lib/task/supervisor.ex | 36 ++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/lib/elixir/lib/dynamic_supervisor.ex b/lib/elixir/lib/dynamic_supervisor.ex index 5077971646..987c5bbb08 100644 --- a/lib/elixir/lib/dynamic_supervisor.ex +++ b/lib/elixir/lib/dynamic_supervisor.ex @@ -11,21 +11,27 @@ defmodule DynamicSupervisor do ## Examples - A dynamic supervisor is started with no children, only with the - supervision strategy (the only strategy currently supported is - `:one_for_one`): + A dynamic supervisor is started with no children, often under a + supervisor with the supervision strategy (the only strategy currently + supported is `:one_for_one`) and a name: - {:ok, sup} = DynamicSupervisor.start_link(strategy: :one_for_one) + children = [ + {DynamicSupervisor, strategy: :one_for_one, name: MyApp.DynamicSupervisor} + ] + + Supervisor.start_link(strategy: :one_for_one) + + The options given in the child specification are documented in `start_link/1`. Once the dynamic supervisor is running, we can start children with `start_child/2`, which receives a child specification: - {:ok, agent1} = DynamicSupervisor.start_child(sup, {Agent, fn -> %{} end}) + {:ok, agent1} = DynamicSupervisor.start_child(MyApp.DynamicSupervisor, {Agent, fn -> %{} end}) Agent.update(agent1, &Map.put(&1, :key, "value")) Agent.get(agent1, & &1) #=> %{key: "value"} - {:ok, agent2} = DynamicSupervisor.start_child(sup, {Agent, fn -> %{} end}) + {:ok, agent2} = DynamicSupervisor.start_child(MyApp.DynamicSupervisor, {Agent, fn -> %{} end}) Agent.get(agent2, & &1) #=> %{} diff --git a/lib/elixir/lib/task/supervisor.ex b/lib/elixir/lib/task/supervisor.ex index f070574ae6..6c304dd506 100644 --- a/lib/elixir/lib/task/supervisor.ex +++ b/lib/elixir/lib/task/supervisor.ex @@ -5,8 +5,18 @@ defmodule Task.Supervisor do This module defines a supervisor which can be used to dynamically supervise tasks. - `start_link/1` can be used to start the supervisor. See the `Task` - module for more examples. + A task supervisor is started with no children, often under a + supervisor and a name: + + children = [ + {Task.Supervisor, name: MyApp.TaskSupervisor} + ] + + Supervisor.start_link(strategy: :one_for_one) + + The options given in the child specification are documented in `start_link/1`. + + See the `Task` module for more examples. ## Name registration @@ -32,17 +42,33 @@ defmodule Task.Supervisor do @doc """ Starts a new supervisor. - The supported options are: + ## Examples + + A task supervisor is typically started under a supervision tree using + the tuple format: + + {Task.Supervisor, name: MyApp.TaskSupervisor} + + You can also start it by calling `start_link/1` directly: + + Task.Supervisor.start_link(name: MyApp.TaskSupervisor) + + But this is recommended only for scripting and should be avoided in + production code. Generally speaking, processes should always be started + inside supervision trees. + + ## Options * `:name` - used to register a supervisor name, the supported values are described under the `Name Registration` section in the `GenServer` module docs; - * `:max_restarts`, `:max_seconds` and `:max_children` - as specified in `DynamicSupervisor`; + * `:max_restarts`, `:max_seconds` and `:max_children` - as specified in + `DynamicSupervisor`; This function could also receive `:restart` and `:shutdown` as options but those two options have been deprecated and it is now preferred to - give them directly to `start_child` and `async` when supported. + give them directly to `start_child` and `async`. """ @spec start_link([option]) :: Supervisor.on_start() # TODO: Deprecate passing restart and shutdown here on Elixir v1.8.