Mongoid 3 + Heroku (MongoHQ) cause Moped::Errors::OperationFailure
Asked Answered
S

3

36

With Rails 3, after upgrading from Mongoid 2.x to Mongoid 3.x, my Heroku + MongoHQ setup stopped working. Funny thing is, that my development & test frameworks and my whole test suite passes just fine.

I suspect the problem is with my mongoid.yml file, but I've tried searching the docs, google & stackoverflow, and used all the suggested formats, including this: heroku mongohq and mongoid Mongo::ConnectionFailure or actually this: https://gist.github.com/2900804

UPDATED July 16th: This is how my mongoid.yml file looks, after trying multiple things + After what Jason from MongoHQ suggested:

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

(to my understanding, it is essentially the same as the one in the links above, except that it uses the uri; I tried the other way, splitting the MONGOHQ_URL into the separate fields as well, but it didn't help)

I've tried setting mongoid as 3.0.0rc and leaving the version blank in my Gemfile. Using the github version failed due to HTTPS certificates or something, so I didn't bother trying it multiple times.

What the action controller says is this:

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"

and when I run

heroku run console 

.. I get this:

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> 

I think I've googled everything, run through multiple github mongoid issues, read the documentation multiple times ... and I'm running out of ideas here.

Anything come to mind I should try next?

UPDATE July 16th: this is what Heroku says when I git push heroku master (After doing what Jason from MongoHQ suggested):

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 [email protected]:xxxxx.git
   b2d97xy..7b0aczy  master -> master

UPDATE 2 July 16th: did as Jason & MrKurt said (first forgot to run bundle update mongoid, but that didn't help either). Now the error changed, though:

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
Stepdame answered 15/7, 2012 at 17:37 Comment(3)
I work at MongoHQ, if you can email [email protected] we can give you direct support on this. --edit-- It looks like Mongoid isn't sending the database + collection name through properly (check the "full_collection_name" property). Which version of Mongoid 3x is this? The earlier RC had some problems with URI database configs.Pungy
1) How do I check the "full_collection_name" property? 2) For some reason I was running 3.0.0.rc without knowing –- it thought I had removed that already. That didn't change the situation, though.Stepdame
I have this error, too undefined method `[]' for nil:NilClass and it doesn't say where it comes from AT ALLOverspend
L
50

You will need to use Ruby 1.9.3. See Mongoid documentation here and Heroku documentation here on how to use 1.9.3.

I was also getting similar errors, and I changed my gemfile to include ruby '1.9.3' and everything worked as expected again.

Edit: @herb pointed out that you need the latest version of bundler for this to work (gem install bundler --pre). Otherwise, ruby '1.9.3' in your Gemfile will not work.

Lima answered 16/7, 2012 at 2:31 Comment(5)
Thank you, this solved the problem! This post alone fixed it: Multiple Ruby Version Support on Heroku. For those who come here after me, be sure to have the newest version of bundler (gem install bundler --pre), otherwise it will throw an error about the Gemfile DSL. Thank you again – this was thus far the most frustrating problem I've had with Rails/Mongodb or Heroku!Stepdame
Worked for me too. Thanks for the "gem install bundler -pre" tip. I had tried adding "ruby 1.9.3" earlier and got errors. It's frustrating how fast documentation gets out of date and how scattered it is. Thank god for StackOverflow.Ebonieebonite
Glad it worked. This was driving me crazy too. I edited the comment with information on the latest version of bundler being required, thanks for pointing that out.Lima
Wow. I had some real problems with this issue as well. Thank you for this!Eolithic
Worked for me (rails 3.2.8, mongoid ~> 3.0.0).Interface
S
2

Try adding an "options" area to the default session and add "skip_version_check: true" as one of the options.

Jason MongoHQ

Superior answered 15/7, 2012 at 23:49 Comment(4)
Updated the question after trying what you suggested. Didn't help. Error is still: 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"Stepdame
Ok, so now the error changed, from command line looks like this: 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 `[]'Stepdame
I've got the same issue. Been trying stuff for an hour. MongoLab nor MongoHQ seem to work on Heroku. They work fine locally: NoMethodError (undefined method `[]' for nil:NilClass):Ebonieebonite
Yes, my original suggestion did not fix the problem. I thought it was a possible issue with auth. See the comment about upgrading to Ruby 1.9.3. I think that is going to be the solution.Superior
C
0

As a workaround, hardcoding the uri from heroku config seems to do the trick.

ex:

production:
  sessions:
    default:
      uri: "mongodb://heroku:[email protected]:000/appyyyy"
      options:
        skip_version_check: true
        safe: true

I'm guessing the nilclass error was because <%= ENV['MONGOHQ_URL'] %> was returning nil

Cerenkov answered 16/1, 2013 at 21:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.