Mongoid 3 + Heroku (MongoHQ) 导致 Moped::Errors::OperationFailure

Posted

技术标签:

【中文标题】Mongoid 3 + Heroku (MongoHQ) 导致 Moped::Errors::OperationFailure【英文标题】:Mongoid 3 + Heroku (MongoHQ) cause Moped::Errors::OperationFailure 【发布时间】:2012-07-14 16:48:11 【问题描述】:

使用 Rails 3,从 Mongoid 2.x 升级到 Mongoid 3.x 后,我的 Heroku + MongoHQ 设置停止工作。有趣的是,我的开发和测试框架以及我的整个测试套件都顺利通过了。

我怀疑问题出在我的 mongoid.yml 文件上,但我尝试搜索文档、google 和 ***,并使用了所有建议的格式,包括:heroku mongohq and mongoid Mongo::ConnectionFailure 或实际上是:https://gist.github.com/2900804

7 月 16 日更新:这是我的 mongoid.yml 文件在尝试了多种方法后的样子 + 在 MongoHQ 的 Jason 建议之后

development:
  sessions:
    default:
      database: development
      hosts:
        - localhost:27017

test:
  sessions:
    default:
      database: test
      hosts:
        - localhost:27017

production:
  sessions:
    default:
      uri: <%= ENV['MONGOHQ_URL'] %>
      options:
        skip_version_check: true
        safe: true

(据我了解,它与上面链接中的基本相同,除了它使用 uri;我尝试了另一种方式,也将 MONGOHQ_URL 拆分为单独的字段,但它没有帮助)

我尝试将 mongoid 设置为 3.0.0rc 并在我的 Gemfile 中将版本留空。由于HTTPS证书什么的,使用github版本失败,所以我没有费心尝试多次。

动作控制器是这样说的:

Moped::Errors::OperationFailure in Home#index

Showing /app/app/views/home/index.html.haml where line #2 raised:

The operation: #<Moped::Protocol::Command
  @length=68
  @request_id=4
  @response_to=0
  @op_code=2004
  @flags=[:slave_ok]
  @full_collection_name=".$cmd"
  @skip=0
  @limit=-1
  @selector=:count=>:posts, :query=>
  @fields=nil>
failed with error "db assertion failure"

当我跑步时

heroku run console 

.. 我明白了:

irb(main):052:0> Location.create!
NoMethodError: undefined method `[]' for nil:NilClass
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/node.rb:74:in `block in command'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/node.rb:522:in `[]'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/node.rb:522:in `block (3 levels) in flush'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/node.rb:521:in `map'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/node.rb:521:in `block (2 levels) in flush'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/node.rb:113:in `ensure_connected'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/node.rb:517:in `block in flush'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/node.rb:532:in `logging'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/node.rb:516:in `flush'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/node.rb:505:in `process'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/node.rb:70:in `command'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/node.rb:356:in `refresh'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/cluster.rb:101:in `block in refresh'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/cluster.rb:114:in `each'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/cluster.rb:114:in `refresh'
    from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.1/lib/moped/cluster.rb:67:in `nodes'
... 15 levels...
    from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:405:in `__run_callback'
    from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
    from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.1/lib/mongoid/callbacks.rb:98:in `run_callbacks'
    from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.1/lib/mongoid/persistence/insertion.rb:23:in `prepare'
    from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.1/lib/mongoid/persistence/operations/insert.rb:26:in `persist'
    from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.1/lib/mongoid/persistence.rb:50:in `insert'
    from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.1/lib/mongoid/persistence.rb:251:in `block in create!'
    from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.1/lib/mongoid/threaded/lifecycle.rb:173:in `_creating'
    from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.1/lib/mongoid/persistence.rb:249:in `create!'
    from (irb):52
    from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
    from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
    from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'irb(main):053:0> 

我想我已经用谷歌搜索了所有内容,处理了多个 github mongoid 问题,多次阅读了文档......我的想法已经用完了。

接下来有什么想尝试的吗?

7 月 16 日更新:这是 Heroku 在我 git push heroku master 时所说的(按照 MongoHQ 的 Jason 的建议):

git push heroku master
Counting objects: 7, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 372 bytes, done.
Total 4 (delta 3), reused 0 (delta 0)

-----> Heroku receiving push
-----> Ruby/Rails app detected
-----> Installing dependencies using Bundler version 1.2.0.pre
       Running: bundle install --without development:test --path vendor/bundle --binstubs bin/ --deployment
       Using rake (0.9.2.2)
       Using i18n (0.6.0)
       Using multi_json (1.3.6)
       Using activesupport (3.2.6)
       Using builder (3.0.0)
            # ... shortened this #
       Using libv8 (3.3.10.4)
       Using moped (1.1.2)
       Using origin (1.0.4)
       Using mongoid (3.0.0.rc)
       Using omniauth (1.1.0)
       Using quimby (0.4.5)
       Using bundler (1.2.0.pre)
       Using rails (3.2.6)
       Using therubyracer (0.10.1)
       Using thin (1.3.1)
       Your bundle is complete! It was installed into ./vendor/bundle
       Cleaning up the bundler cache.
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       Asset precompilation completed (31.24s)
-----> Rails plugin injection
       Injecting rails_log_stdout
       Injecting rails3_serve_static_assets
-----> Discovering process types
       Procfile declares types      -> (none)
       Default types for Ruby/Rails -> console, rake, web, worker
-----> Compiled slug size is 18.7MB
-----> Launching... done, v92
       http://xxxxx.herokuapp.com deployed to Heroku

To git@heroku.com:xxxxx.git
   b2d97xy..7b0aczy  master -> master

7 月 16 日 2 日更新:正如 Jason 和 MrKurt 所说的那样(首先忘记运行 bundle update mongoid,但这也无济于事)。不过,现在错误发生了变化:

NoMethodError in Home#index

Showing /app/app/views/home/index.html.haml where line #2 raised:

undefined method `[]' for nil:NilClass

Extracted source (around line #2):

    1: %h1 Most recent posts
    2: - if @posts.length > 0

【问题讨论】:

我在 MongoHQ 工作,如果您可以发送电子邮件至 support@mongohq.com,我们可以为您提供直接支持。 --edit-- 看起来 Mongoid 没有正确发送数据库 + 集合名称(检查“full_collection_name”属性)。这是哪个版本的Mongoid 3x?早期的 RC 在 URI 数据库配置方面存在一些问题。 1) 如何检查“full_collection_name”属性? 2)由于某种原因,我在不知道的情况下运行 3.0.0.rc ——它以为我已经删除了它。不过,这并没有改变这种情况。 我有这个错误,nil:NilClass 的方法 `[]' 太未定义,它根本没有说明它来自哪里 【参考方案1】:

尝试在默认会话中添加“选项”区域,并添加“skip_version_check: true”作为选项之一。

杰森 蒙古总部

【讨论】:

在尝试了您的建议后更新了问题。没有帮助。错误仍然是:操作:#<:protocol::command>:posts, :query=> @fields=nil> 失败,出现错误“db assertion failure” 好的,所以现在错误改变了,从命令行看起来像这样: Post.first NoMethodError: undefined method []' for nil:NilClass from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.1.2/lib/moped/node.rb:74:in block in command' from /app/vendor/bundle/ruby/1.9.1 /gems/moped-1.1.2/lib/moped/node.rb:522:in `[]' 我也遇到了同样的问题。试了一个小时。 MongoLab 和 MongoHQ 似乎都在 Heroku 上工作。它们在本地工作正常:NoMethodError(nil:NilClass 的未定义方法 `[]'): 是的,我最初的建议并没有解决问题。我认为这可能是身份验证的问题。请参阅有关升级到 Ruby 1.9.3 的评论。我认为这将是解决方案。【参考方案2】:

您需要使用 Ruby 1.9.3。有关如何使用 1.9.3,请参阅 Mongoid 文档 here 和 Heroku 文档 here。

我也遇到了类似的错误,我更改了我的 gemfile 以包含 ruby '1.9.3' 并且一切都再次按预期工作。

编辑: @herb 指出,您需要最新版本的 bundler 才能正常工作 (gem install bundler --pre)。否则,您的 Gemfile 中的 ruby '1.9.3' 将不起作用。

【讨论】:

谢谢,问题解决了!这个帖子单独修复了它:Multiple Ruby Version Support on Heroku. 对于那些在我之后来这里的人,请确保拥有最新版本的 bundler(gem install bundler --pre),否则会抛出有关 Gemfile DSL 的错误。再次感谢您——这是迄今为止我在使用 Rails/Mongodb 或 Heroku 时遇到的最令人沮丧的问题! 也为我工作。感谢“gem install bundler -pre”提示。我之前曾尝试添加“ruby 1.9.3”并出现错误。令人沮丧的是文档过时的速度和分散的程度。感谢上帝的 ***。 很高兴它成功了。这也让我发疯了。我编辑了评论,其中包含有关所需最新版本捆绑器的信息,感谢您指出这一点。 哇。我在这个问题上也遇到了一些实际问题。谢谢你! 为我工作(rails 3.2.8,mongoid ~> 3.0.0)。【参考方案3】:

作为一种解决方法,从 heroku 配置硬编码 uri 似乎可以解决问题。

例如:

production:
  sessions:
    default:
      uri: "mongodb://heroku:xxxx@domain.mongohq.com:000/appyyyy"
      options:
        skip_version_check: true
        safe: true

我猜 nilclass 错误是因为 返回 nil

【讨论】:

以上是关于Mongoid 3 + Heroku (MongoHQ) 导致 Moped::Errors::OperationFailure的主要内容,如果未能解决你的问题,请参考以下文章

Mongoid 在 ruby​​ 1.9.3 上失败

Mongoid 3 + Heroku (MongoHQ) 导致 Moped::Errors::OperationFailure

Heroku 中 mongoid 的奇怪异常

Heroku 的 Rails、Mongoid 和 Unicorn 配置

在 Mongo 的集合之间移动文档(通过 Mongoid)

使用 Mongoid 批量插入/更新?