发生错误中止 EventMachine 进程

发布于 2024-09-19 06:21:00 字数 1130 浏览 5 评论 0原文

我正在创建一个后台脚本,该脚本使用 EventMachine 通过 WebSocket 连接到服务器。该脚本将使用 DelayedJob 或 Resque 运行。我已经能够让它与 WebSockets 服务器通信并发送消息,但是每当 EventMachine 循环中出现错误时,它都不会导致脚本崩溃 - 这是应该发生的事情(也是我需要发生的事情) 。我不必使用 EventMachine,因为我只发送 WebSocket 消息而不接收它们 - 但我希望得到任何帮助:) 谢谢!

#!/usr/bin/env ruby
require 'rubygems'
require 'eventmachine'
require 'em-http'

class Job
  include EventMachine::Deferrable

  def self.perform
    job = Job.new
    EventMachine.run {
      http = EventMachine::HttpRequest.new("ws://localhost:8080/").get :timeout => 0

      http.errback { puts "oops" }
      http.callback {
        puts "WebSocket connected!"
        http.send("Hello watcher")
      }

      http.stream { |msg| }
      job.callback { puts "done" }
      Thread.new { 
        job.execute(http)
        http.close
        EventMachine.stop
      }
    }
  end

  def execute(h)
      sleep 1
      puts "Job Runner!"
      h.send("welcome!")
      sleep 2
      asdsadsa # here I am trying to simulate an error
      sleep 1
      h.send("we are all done!")
      sleep 1
      set_deferred_status :succeeded
  end
end

Job.perform

I'm working on creating a background script that uses EventMachine to connect to a server with WebSockets. The script will be run using DelayedJob or Resque. I've been able to get it to talk to the WebSockets server and send messages, but whenever an error is raised within the EventMachine loop it doesn't crash the script - which is what should happen (and what I need to have happen). I don't have to use EventMachine as I'm only sending WebSocket messages and not receiving them - but I'd love any help on this :) thank you!

#!/usr/bin/env ruby
require 'rubygems'
require 'eventmachine'
require 'em-http'

class Job
  include EventMachine::Deferrable

  def self.perform
    job = Job.new
    EventMachine.run {
      http = EventMachine::HttpRequest.new("ws://localhost:8080/").get :timeout => 0

      http.errback { puts "oops" }
      http.callback {
        puts "WebSocket connected!"
        http.send("Hello watcher")
      }

      http.stream { |msg| }
      job.callback { puts "done" }
      Thread.new { 
        job.execute(http)
        http.close
        EventMachine.stop
      }
    }
  end

  def execute(h)
      sleep 1
      puts "Job Runner!"
      h.send("welcome!")
      sleep 2
      asdsadsa # here I am trying to simulate an error
      sleep 1
      h.send("we are all done!")
      sleep 1
      set_deferred_status :succeeded
  end
end

Job.perform

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

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

发布评论

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

评论(2

风启觞 2024-09-26 06:21:01

由于您在线程内引发异常,因此应该设置 Thread.abort_on_exception设置为 true 否则这些错误将不会正确引发。

Since you're causing an exception inside a thread, you should set Thread.abort_on_exception to true otherwise these errors will not be raised properly.

月亮邮递员 2024-09-26 06:21:01

这里根本不需要使用 Thread.new,事实上,这样做不是线程安全的(eventmachine 本身不是线程安全的,除了 EM::Queue、EM::Channel 和 EM.schedule)。

如果你想在执行中做同步的事情,并且你必须有那个线程,那么,你需要通过 EM.schedule 调用 h.send,例如:

EM.schedule { h.send("welcome!") }

如果你必须以这种方式有那个线程,那么,你想要捕获您自己生成的线程中的异常。然后,您应该自行停止和关闭,或者只是在主(事件机器)线程中重新启动:

EM.run do
  thread = Thread.new do
    raise 'boom'
  end
  EM.add_periodic_timer(0.1) { thread.join(0) }
end

如果合适,上述模式可以轻松地枚举周期性计时器中的线程数组。

最后请注意,仅在 EventMachine > 中支持异常冒泡(正确的异常报告)。 1.0,仍处于测试阶段。要在发生异常时获取可用的回溯,可以使用 gem install eventmachine --pre,或者更好的是使用 Github 存储库中的 master。

You don't need to use Thread.new here at all, in fact, it's not thread safe to do so (eventmachine itself is not thread safe, except for EM::Queue, EM::Channel and EM.schedule).

If you wanted to do synchronous things in execute, and you must have that thread, then, you'll want to call h.send via EM.schedule, for example:

EM.schedule { h.send("welcome!") }

If you must have that thread in this way, then, you want to catch exceptions from the thread you spawn yourself. You should then stop and shutdown on your own, or just raise back up in the main (eventmachine) thread:

EM.run do
  thread = Thread.new do
    raise 'boom'
  end
  EM.add_periodic_timer(0.1) { thread.join(0) }
end

The above pattern can easily just enumerate an array of threads in the periodic timer instead, if appropriate.

Finally, please note that exception bubbling (correct exception reporting) was only supported in EventMachine > 1.0, which is still in beta. To get usable backtraces when exceptions occur, either gem install eventmachine --pre, or better, use master from the Github repo.

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