Improve docs for dynamic and task supervisors

This commit is contained in:
José Valim
2018-01-28 11:55:58 -02:00
parent fb33b81696
commit 29a74f8ad4
2 changed files with 43 additions and 11 deletions
+12 -6
View File
@@ -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)
#=> %{}
+31 -5
View File
@@ -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.