如何捕获 EventMachine 服务器上的顶级故障?

发布于 2024-12-07 23:18:37 字数 342 浏览 0 评论 0原文

我有一个 EventMachine 服务器,正在使用 monit 进行监控。有时它会崩溃,我试图找出原因,但我不清楚如何记录所有顶级故障。我尝试了这样的代码:

begin
  EventMachine::run do
    EventMachine::start_server('0.0.0.0', PORT, MyServer)
  end
rescue Exception => e
  puts "FAILURE: #{e.class}: #{e}"
end

但这似乎没有捕获错误。我怀疑这可能是内存不足之类的问题,我正在单独跟踪,但如果可能的话,我仍然希望该服务器记录其故障的直接原因。

I have an EventMachine server that I am monitoring with monit. Sometimes it crashes, and I am trying to figure out why, but it is unclear to me how I can just log all top level failures. I tried code like this:

begin
  EventMachine::run do
    EventMachine::start_server('0.0.0.0', PORT, MyServer)
  end
rescue Exception => e
  puts "FAILURE: #{e.class}: #{e}"
end

but that does not seem to ever catch errors. I suspect it might be something like running out of memory, which I am tracking separately, but still I would like this server to log its proximate cause of failure if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霓裳挽歌倾城醉 2024-12-14 23:18:37

如果您想要一个包罗万象的错误处理程序,请尝试 EM.error_handler。 docs 中的示例:

EM.error_handler{ |e|
  puts "Error raised during event loop: #{e.message}"
}

您可能还需要更细粒度的错误处理,在这种情况下您可以使用 errback 机制(请参阅 Deferrable)。例如,您可以在您的反应器循环中使用:

EventMachine::run do
  server = EventMachine::start_server('0.0.0.0', PORT, MyServer)
  server.errback { # handle error thrown by server here  }
end

要使其工作,请在 MyServer 中包含 Deferrable,然后每当您想引发错误时,调用 fail

If you want a catch-all error handler, try EM.error_handler. Example from the docs:

EM.error_handler{ |e|
  puts "Error raised during event loop: #{e.message}"
}

You may also want more fine-grained error handling, in which case you can use the errback mechanism (see Deferrable). So for example you could have in your reactor loop:

EventMachine::run do
  server = EventMachine::start_server('0.0.0.0', PORT, MyServer)
  server.errback { # handle error thrown by server here  }
end

For this to work, include Deferrable in your MyServer, then whenever you want to raise an error, call fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文