首页 > 解决方案 > 主管树退出而不是重新启动子进程

问题描述

我的问题很像这里描述的Supervision tree failed to start,并且提供的解决方案似乎或多或少与我所拥有的相同,除了我的只是在子进程失败并显示以下消息时退出:** (Mix) Could not start application server: Server.start(:normal, []) returned an error: shutdown: failed to start child: Server.Gen.StartRabbit。我想知道如何让它“重新启动”(或者更确切地说重试启动,因为它实际上还没有启动)。谢谢!

defmodule Server do
  use Application

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

    children = [
      {
        GenRegistry,
        worker_module: Server.Gen.Rabbit
      },
      Server.Gen.StartRabbit,
      Plug.Cowboy.child_spec(
        scheme: :http,
        plug: Server.Router,
        options: [
          port: String.to_integer(System.get_env("PORT") || "3000"),
          dispatch: dispatch(),
          protocol_options: [idle_timeout: :infinity]
        ]
      )
    ]

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

defmodule Server.Gen.StartRabbit do
  use GenServer

  def start_link(_) do
    GenServer.start_link(__MODULE__, [], name: __MODULE__)
  end

  def init(_opts) do
    Enum.each(0..5, fn x ->
      GenRegistry.lookup_or_start(Gen.Rabbit, Integer.to_string(x), [
        %Gen.Rabbit.State{id: str_id, chan: nil}
      ])
    end)

    {:ok, %{}}
  end
end

标签: elixir

解决方案


推荐阅读