如何从已弃用的 Supervisor.spec 更新到新的 Supervisor.behaviour?

时间:2021-01-11 09:23:07

标签: elixir gen-server erlang-supervisor

背景

我正在尝试在我的应用程序中构建一个监督树,其中给定的 GenServer 必须监督其他 GenServer。这不是一个应用程序,只是一个简单的需要监督他人的GenServer。

为此,我主要将注意力集中在以下文章上: http://codeloveandboards.com/blog/2016/03/20/supervising-multiple-genserver-processes/

代码

上面的文章让我找到了以下代码:

defmodule A.Server do
  use Supervisor

  alias B

  def start_link, do:
    Supervisor.start_link(__MODULE__, nil, name: __MODULE__)

  def init(nil) do
    children = [B]
    supervise(children, strategy: :one_for_one)
  end
end

如您所见,我的 GenServer A 正试图监督另一个名为 B 的人。

问题

这里的问题是本示例中的所有内容都已弃用。我尝试按照说明阅读新的 Supervisor docs,特别是 start_child,我认为它是已弃用的 supervise 的正确替代品,但不幸的是,我不明白如何应用它到我的代码。

问题

如何更新我的代码,使其不使用不推荐使用的函数?

1 个答案:

答案 0 :(得分:2)

在提供示例的 Supervisor 文档中有一个专门的部分。

defmodule A.Server do
  use Supervisor

  alias B

  def start_link, do:
    Supervisor.start_link(__MODULE__, nil, name: __MODULE__)

  @impl Supervisor
  def init(_) do
    children = [
      {B, [:arg1, :arg2]} # or just `B` if `start_link/0`
    ]

    Supervisor.init(children, strategy: :one_for_one)
  end
end
相关问题