监督树未能启动

Supervision tree failing to start

我正在尝试实现类似于 this answer 中描述的内容,但在编译应用程序时遇到如下错误。

** (Mix) Could not start application workers: Workers.Application.start(:normal, []) returned an error: shutdown: failed to start child: {Workers.UrlSupervisor, 2}
    ** (EXIT) already started: #PID<0.1034.0>

我不确定我是否天生就在做一些我在这里不允许做的事情,或者我只是犯了一个小错误。

对于某些上下文,这里是主管:

defmodule Workers.Application do
  # See http://elixir-lang.org/docs/stable/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  def start(_type, _args) do
    import Supervisor.Spec, warn: false

     url_workers =  1..100 |> Enum.map(fn (i) -> supervisor(Workers.UrlSupervisor, [i], [id: {Workers.UrlSupervisor, i}, restart: :temporary]) end)
     domain_workers = 1..100 |> Enum.map(fn (i) -> supervisor(Workers.DomainSupervisor, [i], [id: {Workers.DomainSupervisor, i}, restart: :temporary]) end)

    opts = [strategy: :one_for_one, name: Workers.Supervisor]
    Supervisor.start_link(url_workers ++ domain_workers, opts)
  end
end

defmodule Workers.UrlSupervisor do
  def start_link(id) do
    import Supervisor.Spec, warn: false

    children = [worker(Task, [&Workers.Url.worker/0], [id: {Workers.Url, id}, restart: :permanent])]

    opts = [strategy: :one_for_one, name: Workers.UrlSupervisor]
    Supervisor.start_link(children, opts)
  end
end

defmodule Workers.DomainSupervisor do
  def start_link(id) do
    import Supervisor.Spec, warn: false

    children = [worker(Task, [&Workers.Domain.worker/0], [id: {Workers.Domain, id}, restart: :permanent])]

    opts = [strategy: :one_for_one, name: Workers.DomainSupervisor]
    Supervisor.start_link(children, opts)
  end
end

这是其中一名工人(他们看起来大致相同)。

defmodule Workers.Domain do
  def worker do
    case Store.Domains.pop do
      :empty ->
        IO.puts "[Domain] none found, waiting..."
        :timer.sleep(1000)
      {crawl_id, domain} ->
        IO.puts "[Domains] found a domain to check: #{domain}"
        case Core.check_domain(domain) do
          :error ->
            Utils.insert(crawl_id, domain, false)
          :registered ->
            Utils.insert(crawl_id, domain, false)
          :available ->
            Utils.insert(crawl_id, domain, true)
        end
    end
    worker()
  end
end

在您的 Workers.Application 中开始 Supervisor 时,您提供了唯一的 id,但它们也应该具有唯一的 name

尝试添加另一个关键字,例如名称::"url_supervisor_#{i}":

def start(_type, _args) do
  import Supervisor.Spec, warn: false

  url_workers =  1..100 |> Enum.map(fn (i) -> 
    supervisor(Workers.UrlSupervisor, [i],
               [id: {Workers.UrlSupervisor, i},
                name: :"url_supervisor_#{i}", # Name added here
                restart: :temporary])
  end)
  domain_workers = 1..100 |> Enum.map(fn (i) ->
    supervisor(Workers.DomainSupervisor, [i],
               [id: {Workers.DomainSupervisor, i},
                name: :"domain_supervisor_#{i}", # Name added here
                restart: :temporary])
     end)

  opts = [strategy: :one_for_one, name: Workers.Supervisor]
  Supervisor.start_link(url_workers ++ domain_workers, opts)
end