MongoMapper、MongoDB 和 EventMachine

发布于 2024-11-10 01:28:36 字数 3647 浏览 3 评论 0原文

我正在使用 DaemonKit 创建 AMQP 侦听器。每当一条消息通过网络时,我都会将其记录到 MongoDB 中。我已将 Rails MongoDB 配置移过来,并且连接工作正常。当我尝试创建新模型时,Model.create 方法中出现错误。

IOError when attempting to close socket connected to localhost:27017: #<IOError: closed stream>
/Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/connection.rb:856:in `rescue in send_message_on_socket': Operation failed with the following exception: closed stream (Mongo::ConnectionFailure)
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/connection.rb:843:in `send_message_on_socket'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/connection.rb:483:in `block in receive_message'
from <internal:prelude>:10:in `synchronize'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/connection.rb:482:in `receive_message'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:407:in `block in send_initial_query'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/connection.rb:609:in `instrument'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:406:in `send_initial_query'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:371:in `refresh'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:87:in `next_document'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:248:in `each'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:267:in `to_a'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:267:in `to_a'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/plucky-0.3.8/lib/plucky/query.rb:76:in `all'
from /Volumes/External/Projects/ruby/alertsdaemons/eventmachine/libexec/eventmachine-daemon.rb:17:in `block in <top (required)>'
from /Volumes/External/Projects/ruby/alertsdaemons/eventmachine/libexec/eventmachine-daemon.rb:15:in `loop'
from /Volumes/External/Projects/ruby/alertsdaemons/eventmachine/libexec/eventmachine-daemon.rb:15:in `<top (required)>'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `block in require'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in `block in load_dependency'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in `new_constants_in'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in `load_dependency'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/daemon-kit-0.1.8.2/lib/daemon_kit/application.rb:38:in `run'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/daemon-kit-0.1.8.2/lib/daemon_kit/application.rb:21:in `exec'
from /Volumes/External/Projects/ruby/alertsdaemons/eventmachine/bin/eventmachine:12:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'

创建对象的代码:

# Sample loop to show process
loop do
  DaemonKit.logger.info "I'm running"
  message = Message.create({
    :first_name => 'Lee',
    :last_name => 'Faus'
  })
  puts message.inspect
  puts message.count
  sleep 5
end

我读过我可以分叉 mongomapper 以使 http 调用异步,但这些错误似乎来自 mongodb ruby​​ 驱动程序。非常感谢任何帮助。

I am using DaemonKit to create an AMQP listener. Whenever a message comes across the wire, I would like to log it to MongoDB. I have moved my Rails MongoDB configs over and the connection works fine. When I try to create a new model, I get an error in the Model.create method.

IOError when attempting to close socket connected to localhost:27017: #<IOError: closed stream>
/Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/connection.rb:856:in `rescue in send_message_on_socket': Operation failed with the following exception: closed stream (Mongo::ConnectionFailure)
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/connection.rb:843:in `send_message_on_socket'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/connection.rb:483:in `block in receive_message'
from <internal:prelude>:10:in `synchronize'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/connection.rb:482:in `receive_message'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:407:in `block in send_initial_query'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/connection.rb:609:in `instrument'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:406:in `send_initial_query'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:371:in `refresh'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:87:in `next_document'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:248:in `each'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:267:in `to_a'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/mongo-1.3.1/lib/mongo/cursor.rb:267:in `to_a'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/plucky-0.3.8/lib/plucky/query.rb:76:in `all'
from /Volumes/External/Projects/ruby/alertsdaemons/eventmachine/libexec/eventmachine-daemon.rb:17:in `block in <top (required)>'
from /Volumes/External/Projects/ruby/alertsdaemons/eventmachine/libexec/eventmachine-daemon.rb:15:in `loop'
from /Volumes/External/Projects/ruby/alertsdaemons/eventmachine/libexec/eventmachine-daemon.rb:15:in `<top (required)>'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `block in require'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in `block in load_dependency'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in `new_constants_in'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in `load_dependency'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/daemon-kit-0.1.8.2/lib/daemon_kit/application.rb:38:in `run'
from /Users/lfaus/.rvm/gems/ruby-1.9.2-p180/gems/daemon-kit-0.1.8.2/lib/daemon_kit/application.rb:21:in `exec'
from /Volumes/External/Projects/ruby/alertsdaemons/eventmachine/bin/eventmachine:12:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'

code for creating the object:

# Sample loop to show process
loop do
  DaemonKit.logger.info "I'm running"
  message = Message.create({
    :first_name => 'Lee',
    :last_name => 'Faus'
  })
  puts message.inspect
  puts message.count
  sleep 5
end

I had read that I could fork mongomapper to make the http calls async, but these errors seem to be coming from the mongodb ruby driver. any help is greatly appreciated.

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

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

发布评论

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

评论(1

木落 2024-11-17 01:28:36

很难说这里到底发生了什么,但我怀疑我希望你调查一下。

确保您在 POST-daemonize 配置中建立 Mongo 连接,而不是在 PRE-daemonize 配置中。 DaemonKit 在 PRE 初始化程序之后分叉,此时需要重新建立 mongo 连接。这适用于任何类型的 IO,分叉后它们都需要重新打开。

同样的逻辑也适用于守护进程项目中的 ActiveRecord。如果这是 ActiveRecord 你可以这样做:

config/pre-daemonize/database.rb

    ActiveRecord::Base.establish_connection(:foo => 'bar')

config/post-daemonize/database.rb

    ActiveRecord::Base.verify_active_connections!

我不是 mongomapper 用户,所以我不知道如何翻译上面的例子。

我最好的建议是在守护进程前阶段“ping”mongo,如果失败则出错,这样守护进程甚至不会启动,并在守护进程后配置中正确设置连接。

It is tough to say what is really happening here, but I have a suspicion that I'd like you to investigate please.

Ensure you are establishing your Mongo connection in a POST-daemonize config, instead of a PRE-daemonize config. DaemonKit forks after the PRE-initializers and the mongo connection will need to be re-established at this point in time. This goes for any IO of any kind, after forking they all need to be re-opened.

The same logic holds true for ActiveRecord in a daemonized project. If this was ActiveRecord you could do something like this:

config/pre-daemonize/database.rb

    ActiveRecord::Base.establish_connection(:foo => 'bar')

config/post-daemonize/database.rb

    ActiveRecord::Base.verify_active_connections!

I'm not a mongomapper user, so I don't know how to translate the above example.

My best advise is to "ping" mongo in PRE-daemonization phase and error out if it failed, so the daemon doesn't even start, and the properly setup the connection in a post-daemonize configuration.

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