发生错误中止 EventMachine 进程
我正在创建一个后台脚本,该脚本使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您在线程内引发异常,因此应该设置 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.这里根本不需要使用 Thread.new,事实上,这样做不是线程安全的(eventmachine 本身不是线程安全的,除了 EM::Queue、EM::Channel 和 EM.schedule)。
如果你想在执行中做同步的事情,并且你必须有那个线程,那么,你需要通过 EM.schedule 调用 h.send,例如:
如果你必须以这种方式有那个线程,那么,你想要捕获您自己生成的线程中的异常。然后,您应该自行停止和关闭,或者只是在主(事件机器)线程中重新启动:
如果合适,上述模式可以轻松地枚举周期性计时器中的线程数组。
最后请注意,仅在 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:
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:
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.