开发者

MongoMapper, MongoDB and EventMachine

开发者 https://www.devze.com 2023-03-08 09:09 出处:网络
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.Wh

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/co开发者_StackOverflow社区nnection.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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消