Search code examples
rubymongodbsinatramongoidunicorn

How do I fix 'NameError: uninitialized constant Mongo::ConnectionFailure


Update 2: I was able to further narrow it down to the fact that it apparently isn't loading all of Mongo's classes when I run the rake. I added puts Mongo.constants in the config/boot.rb after require 'mongo'. When I run the app, it outputs a long list including the Connection and ConnectionFailure classes. When I attempt to run the rake, it outputs a much shorter list, and those two classes are missing. What is going on here?

Update: By rescuing a generic exception, I've eliminated the NameError for Mongo::ConnectionFailure and it is now throwing a NameError for Mongo::Connection. So I guess it is a mongo load issue.

When I do puts defined? Mongo, it prints constant when I run the app and when I run the rake. However, puts defined? Mongo::Connection it prints constant when I run the app, but puts a blank line when I run the rake.

The error is the same whether I require mongo from config/boot.rb or from app.rb.


I'm new to ruby, and trying to set up an app with Sinatra, Unicorn, and Mongo/mongoid. I'm trying to run a rake task and when I run rake import:areas I get the following error:

rake aborted!
NameError: uninitialized constant Mongo::ConnectionFailure
/home/amanda/Documents/development/app-name/app.rb:7:in `rescue in block in <class:App>'

The mongo server is running, and the code passes through an earlier Mongo::Connection call (where I would expect it to throw a NameError if mongo wasn't loaded) without error. Watching the mongod console, it doesn't appear to be even hitting mongo, because there is no indication in the console logs of any connection attempt.

When I run the app itself (using heroku local or bundle exec unicorn -p $PORT -c "/path/to/unicorn.rb") it starts and listens on the appropriate port, and I can hit the root endpoint without issue. Watching the mongod console as it starts up, I can see it successfully connect to the database. I'm running the mongo gem version 1.10.2 and mongoid 3.1.6 (requirements for the codebase I'm working in).

relevant lines of the rakefile:

require "./config/boot"

namespace "import" do
    task "areas" do
        # code here
    end
end

config/boot.rb:

require 'sinatra'
require 'mongo'
require 'mongoid'
require './app'

mongoid.yml:

development:
  clients:
    default:
      database: db-name
      hosts:
        - localhost:27017
      options:
  options:

app.rb:

class App < Sinatra::Base
  configure do
    # mongo
    begin
      mongo_db = Mongo::Connection.new.db "db-name"
      set :mongo_db, mongo_db
    rescue Mongo::ConnectionFailure
      set :mongo_db, {}
    end
  end
  # more code here
end

What am I missing here? Nothing I've googled has seemed like this issue. I would expect this error to be thrown at Mongo::Connection and not at the Mongo::ConnectionFailure line.


Solution

  • It turns out the Gemfile.lock got corrupted somehow, and rake was using a newer version of mongo than heroku local was. :-/