Elixir OTP Supervisor检测到崩溃的进程时记录错误 [英] Logging errors when an Elixir OTP Supervisor detects a crashed process

查看:113
本文介绍了Elixir OTP Supervisor检测到崩溃的进程时记录错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

针对Supervisor的十六进制文档中,有一行指出:


使用此模块实现的主管具有一组标准的接口功能,并包括跟踪和错误报告功能。

A supervisor implemented using this module has a standard set of interface functions and includes functionality for tracing and error reporting.

不幸的是,我不知道该怎么做。我想要的是当 Supervisor 检测到童工崩溃并正在重新启动时触发的某种挂钩/通知/事件。主要是,我只想记录一个工作者崩溃的事实,以便可以向用户显示此错误,而就我而言,这需要将错误消息写入数据库中的字段。

Unfortunately, I cannot figure out how to do this. What I would like is some kind of hook/notification/event that is fired when a Supervisor detects that a child worker has crashed and is being restarted. Mainly, I just want to log the fact that a worker crashed so I can display this error to the user, and in my case this requires writing the error message to a field in the database.

我想念什么?

推荐答案

当孩子崩溃时出现的错误是在哪里可以传递的函数或模块?登录到BEAM中的SASL记录器,我认为这就是文档标准接口功能集的含义。请参见 http://erlang.org/doc/apps/sasl/error_logging.html http://erlang.org/doc/man/error_logger.html 有关更多详细信息。

Errors when children crash are logged to the SASL logger in BEAM and I think this is what the docs mean by "standard set of interface functions". See http://erlang.org/doc/apps/sasl/error_logging.html and http://erlang.org/doc/man/error_logger.html for some more details.

如果您使用的是:simple_one_for_one 主管来开工子弟,那便是您感兴趣的话,您可以考虑自己监视它们,而不用研究SASL内容。例如

If you're using a :simple_one_for_one supervisor to start worker children and it's those you're interested in then you could consider monitoring them yourself rather than digging into the SASL stuff. e.g.

def MyApp.SomeModule do
  use GenServer

  # ... init etc.

  def start_worker() do
    {:ok, pid} = SomeWorkerSupervisor.start_child()
    Process.monitor(pid)
  end

  def handle_info(:'DOWN', _, _, worker_pid, reason) do
    # ... something
  end
end

原因将为:


要么是进程的退出原因,要么是noproc(在创建监视器时进程或端口不存在),或者是没有连接(与被监视进程所在的节点没有连接)。

Either the exit reason of the process, noproc (process or port did not exist at the time of monitor creation), or noconnection (no connection to the node where the monitored process resides).

(来自 http://erlang.org/doc/man/erlang.html#monitor-2

这篇关于Elixir OTP Supervisor检测到崩溃的进程时记录错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆