1

I got this error at "app startup" on Heroku. On localhost all works fine.

app.rb (Sinatra)

require 'uri'
require 'sinatra'
require 'mongo'
require "bson"
require 'chartkick'
require 'omniauth-facebook'

include Mongo

use Rack::Session::Cookie
use OmniAuth::Builder do
    provider :facebook, "KEY", "SECRET", :scope => 'read_stream'
end

#### DATABASE #######
def get_connection
  return @db_connection if @db_connection
  db = URI.parse(ENV['MONGOHQ_URL'] || 'mongodb://localhost/facebook')
  db_name = db.path.gsub(/^\//, '')
  @db_connection = Mongo::Connection.new(db.host, db.port).db(db_name)
  @db_connection.authenticate(db.user, db.password) unless (db.user.nil? || db.user.nil?)
  @db_connection
end

db = get_connection

Gemfile

source 'https://rubygems.org'
ruby '2.0.0'

gem 'mongo'
gem "bson"
gem 'bson_ext'
gem "sinatra"
gem "koala", "~> 1.7.0rc1"
gem "chartkick"
gem 'omniauth-facebook'

config.ru

require './app'
run Sinatra::Application

** Heroku logs **

 Slug compilation finished
2013-09-16T12:32:07.666778+00:00 heroku[web.1]: State changed from crashed to starting
2013-09-16T12:32:11.011518+00:00 heroku[web.1]: Starting process with command `bundle exec rackup config.ru -p 21566`
2013-09-16T12:32:12.324776+00:00 app[web.1]: /app/vendor/bundle/ruby/2.0.0/gems/mongo-1.3.1/lib/mongo/cursor.rb:419:in `construct_query_message': uninitialized constant BSON::ByteBuffer (NameError)
2013-09-16T12:32:12.324776+00:00 app[web.1]:    from /app/app.rb:21:in `new'
2013-09-16T12:32:12.325160+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
2013-09-16T12:32:12.324776+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/mongo-1.3.1/lib/mongo/cursor.rb:405:in `send_initial_query'
2013-09-16T12:32:12.325003+00:00 app[web.1]:    from /app/app.rb:21:in `get_connection'
2013-09-16T12:32:12.325160+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
2013-09-16T12:32:12.324776+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/mongo-1.3.1/lib/mongo/cursor.rb:371:in `refresh'
2013-09-16T12:32:12.325003+00:00 app[web.1]:    from /app/app.rb:26:in `<top (required)>'
2013-09-16T12:32:12.325160+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
2013-09-16T12:32:12.325003+00:00 app[web.1]:    from /app/config.ru:1:in `require'
2013-09-16T12:32:12.325003+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
2013-09-16T12:32:12.324776+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/mongo-1.3.1/lib/mongo/cursor.rb:87:in `next_document'
2013-09-16T12:32:12.325003+00:00 app[web.1]:    from /app/config.ru:1:in `block in <main>'
2013-09-16T12:32:12.325160+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
2013-09-16T12:32:12.324776+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/mongo-1.3.1/lib/mongo/db.rb:498:in `command'
2013-09-16T12:32:12.325003+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
2013-09-16T12:32:12.325160+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:250:in `start'
2013-09-16T12:32:12.325160+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/bin/rackup:23:in `<main>'
2013-09-16T12:32:12.324776+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/mongo-1.3.1/lib/mongo/connection.rb:704:in `check_is_master'
2013-09-16T12:32:12.325003+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
2013-09-16T12:32:12.324776+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/mongo-1.3.1/lib/mongo/connection.rb:656:in `setup'
2013-09-16T12:32:12.325003+00:00 app[web.1]:    from /app/config.ru:in `new'
2013-09-16T12:32:12.325160+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:141:in `start'
2013-09-16T12:32:12.324776+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/mongo-1.3.1/lib/mongo/connection.rb:504:in `connect'
2013-09-16T12:32:12.325003+00:00 app[web.1]:    from /app/config.ru:in `<main>'
2013-09-16T12:32:12.325160+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/bin/rackup:4:in `<top (required)>'
2013-09-16T12:32:12.324776+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/mongo-1.3.1/lib/mongo/connection.rb:101:in `initialize'
2013-09-16T12:32:12.325003+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
2013-09-16T12:32:12.325160+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/bin/rackup:23:in `load'
2013-09-16T12:32:13.530315+00:00 heroku[web.1]: Process exited with status 1
2013-09-16T12:32:13.544931+00:00 heroku[web.1]: State changed from starting to crashed
sparkle
  • 7,530
  • 22
  • 69
  • 131
  • Is there a reason you are using ruby 2.0.0 instead of 1.9.x? Have you tried with 1.9.x? Can you check locally that you can find BSON::ByteBuffer, i.e. ruby -e 'require "bson"; puts BSON::ByteBuffer' ? And do similarly in irb in a heroku console, e.g. as described here https://devcenter.heroku.com/articles/console-bamboo ? Also, in line 21 of app.rb, the conditional reads "unless (db.user.nil? || db.user.nil?)". I imagine you meant to test db.password.nil? and not db.user.nil? twice. – jribnik Sep 19 '13 at 15:27
  • I tried. If I run ruby -e 'require "bson"; puts BSON::ByteBuffer' on my local machine I get uninitialized constant BSON::ByteBuffer. (Now using ruby 1.9.2) – sparkle Sep 20 '13 at 12:34
  • 1
    I'm getting the same error. Locally and on my deployed version. My rails app has been working fine until today. – asprotte Sep 25 '13 at 14:25
  • @asprotte Have you solved it? – sparkle Oct 23 '13 at 08:33

2 Answers2

0

The log says its a name error. I suggest you specify the correct scope. Rails has a very complicated way of looking up constants. I suggest you take a look at this:

http://urbanautomaton.com/blog/2013/08/27/rails-autoloading-hell/

and this

http://pothibo.com/2013/07/namespace-stuff-in-your-app-folder/. Hope it helps.

Hrishi
  • 7,110
  • 5
  • 27
  • 26
0

It looks like the Mongo you are using is 1.3.1 and the BSON is probably 2.0. These don't seem to work together. I'd try to put the 1.9 BSON there and see if that works.

You can do so by adding: gem "bson", "~> 1.9.2"

To your Gemfile.

Hope that helps?

Bas Vodde
  • 101
  • 4