使用Elixir中的管理员启动应用程序,该程序会在出错时自动重启

时间:2016-07-25 13:22:44

标签: task elixir supervisor

我可以在elixir上运行应用程序但是如果发生错误,一切都存在。我只是希望我的应用程序在这种情况下重新启动。我正在使用主管,但甚至不知道它是否正常工作。以下是我的代码: -

mix.exs: -

def application do
[ 
  applications: [:httpotion, :zookeeper, :parallel, :poison],
  mod: {ServiceMonitor, []}
]
end

service_monitor.ex

defmodule ServiceMonitor do
  use Application

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

    children = [
        worker(ServiceMonitor.Registry.setup, []),
    ]

    supervise(children, strategy: :one_for_one)
  end
end

registry.ex

defmodule ServiceMonitor.Registry do

  def setup do
    zk = get_zk
    start(zk)
  end

  def start(zk) do
    #main code
  end
end

现在我使用以下命令运行我的应用程序: -

MIX_ENV=test mix run

在我的主代码中收到错误之前,一切运行良好。应用程序存在且永不重启。我收到的错误是: -

** (Mix) Could not start application service_monitor: exited in: ServiceMonitor.start(:normal, [])
** (EXIT) exited in: Task.await(%Task{owner: #PID<0.164.0>, pid: #PID<0.169.0>, ref: #Reference<0.0.2.1026>}, 3600000)
    ** (EXIT) an exception was raised:
        ** (MatchError) no match of right hand side value: {:error, :no_node}
            (service_monitor) lib/service_monitor/registry.ex:121: anonymous fn/3 in ServiceMonitor.Registry.remove_available_if_not_registered/3
            (elixir) lib/task/supervised.ex:89: Task.Supervised.do_apply/2
            (elixir) lib/task/supervised.ex:40: Task.Supervised.reply/5
            (stdlib) proc_lib.erl:240: :proc_lib.init_p_do_apply/3

请帮助我哪里出错了。我的要求是应用程序应在收到错误后立即自动重启。

1 个答案:

答案 0 :(得分:0)

如果ServiceMonitor是您的Supervisor模块,则应使用Supervisor而不是根据this导入Supervisor.spec

修改: 如果您仍想与您的主管联系,请尝试在代码末尾添加Supervisor.start,就像示例中一样:

defmodule KVServer do
  use Application

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

    children = [
    # worker(KVServer.Worker, [arg1, arg2, arg3])
    ]

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