Heroku 中 mongoid 的奇怪异常

Posted

技术标签:

【中文标题】Heroku 中 mongoid 的奇怪异常【英文标题】:Odd exception with mongoid at Heroku 【发布时间】:2012-12-26 23:15:42 【问题描述】:

我不断收到 heroku 错误。这是一个使用 Mongoid 的 Sinatra 应用程序。虽然我也在那里托管了一个 Rails 应用程序,但它不会导致任何错误,尽管它们具有完全相同的 mongoid.yml 文件。

这里是来自 heroku 的显示异常的日志

me@ubuntu:~/dev$ heroku logs
2013-01-12T13:51:09+00:00 app[web.1]: Moped::Errors::ConnectionFailure - Could not connect to any secondary or primary nodes for replica set <Moped::Cluster nodes=[<Moped::Node resolved_address="127.0.0.1:27017">]>
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/session/context.rb:43:in `query'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/session/context.rb:104:in `with_node'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (3 levels) in route!'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/controllers/sessions_controller.rb:38:in `block in <class:App>'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:936:in `dispatch!'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `block in compile!'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `block in call!'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:261:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/bundler/gems/mongoid-c839bb60e5f9/lib/mongoid/contextual.rb:19:in `first'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `catch'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:834:in `block in route!'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `block in invoke'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/query.rb:114:in `first'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:851:in `route_eval'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:755:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-timeout-0.0.3/lib/rack/timeout.rb:16:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-timeout-0.0.3/lib/rack/timeout.rb:16:in `block in call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/ruby-1.9.3/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `[]'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `catch'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `process_route'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `call!'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.3.2/lib/rack/protection/base.rb:48:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `each'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:200:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.3.2/lib/rack/protection/xss_header.rb:27:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/bundler/gems/mongoid-c839bb60e5f9/lib/mongoid/contextual/mongo.rb:186:in `first'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.3.2/lib/rack/protection/json_csrf.rb:17:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:136:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1471:in `synchronize'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.3.2/lib/rack/protection/xss_header.rb:27:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/logger.rb:15:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:134:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:205:in `context'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/urlmap.rb:49:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.3.2/lib/rack/protection/base.rb:48:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:99:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/urlmap.rb:49:in `each'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.3.2/lib/rack/protection/path_traversal.rb:16:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:129:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.3.2/lib/rack/protection/base.rb:48:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `block in call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/exceptional-2.0.33/lib/exceptional/integration/rack.rb:21:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (2 levels) in route!'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:205:in `context'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/commonlogger.rb:20:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/head.rb:9:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cluster.rb:258:in `with_secondary'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `invoke'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:872:in `block in process_route'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `route!'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.3.2/lib/rack/protection/base.rb:48:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.5.0/lib/thin/connection.rb:81:in `block in pre_process'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.5.0/bin/thin:6:in `<top (required)>'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.5.0/lib/thin/connection.rb:79:in `pre_process'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.5.0/lib/thin/server.rb:159:in `start'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.5.0/lib/thin/backends/base.rb:63:in `start'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/bin/thin:23:in `<main>'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.5.0/lib/thin/connection.rb:79:in `catch'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/bin/thin:23:in `load'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.5.0/lib/thin/connection.rb:39:in `receive_data'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/urlmap.rb:64:in `block in call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.5.0/lib/thin/connection.rb:54:in `process'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.5.0/lib/thin/controllers/controller.rb:86:in `start'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:200:in `call'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.5.0/lib/thin/runner.rb:152:in `run!'
2013-01-12T13:41:47+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.5.0/lib/thin/runner.rb:187:in `run_command'

mongoid.yml

development:
  sessions:
    default:
      database: dev123
      hosts:
        - localhost:27017
      options:
        timeout: 20

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

宝石文件

gem "mongoid", git: "https://github.com/mongoid/mongoid.git"

【问题讨论】:

那么...有什么例外? 在原木上掠夺,就是这样。 尝试总结日志。将几行日志条目粘贴到 *** 中的一个问题上,基本上说这是我的问题,解决它”是一种非常快速的投票方式 这里的问题是 ruby​​ 异常日志是堆叠的,因此实际错误位于顶部,并且基本上之后的每一行都是标准调用堆栈跟踪。换句话说,您粘贴的日志为我们提供了除有趣的日志之外的所有详细信息。 @AustinMullins,日志顶部不是您要查找的内容吗? 【参考方案1】:

错误是应用无法连接到它期望运行的本地 mongo 实例。

如果你认为日志信息很长,你还没有看到 Java Spring 自动装配异常 =) 虽然在这种情况下,最上面的错误信息足以确定问题。

我刚刚让 Mongo HQ 在 Heroku 上使用我的 Rails 应用程序,所以我的建议是:

在您的项目目录中运行 heroku config 以检查您是否已定义 RACK_ENV=production 并且 MONGOHQ_URL 已由 Heroku 定义。

如果 MONGOHQ_URL 没有在 Heroku 上定义为环境变量,很可能你还没有运行 heroku addons:add mongohq:sandbox 来告诉 Heroku 你想要一个 Mongo HQ 实例。

如果未定义 RACK_ENV=production,只需添加: heroku config:add RACK_ENV=production

在此之后,使用 heroku restart 重新启动您的应用程序,它应该可以工作了。

Rails 应用程序(而不是 Sinatra)的仅供参考,RACK_ENV 变为 RAILS_ENV。

【讨论】:

以上是关于Heroku 中 mongoid 的奇怪异常的主要内容,如果未能解决你的问题,请参考以下文章

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

Heroku 的 Rails、Mongoid 和 Unicorn 配置

heroku mongohq 和 mongoid Mongo::ConnectionFailure

使用 mongoid 查看 MongoDB 中的现有索引

Mongoid 在 ruby​​ 1.9.3 上失败

Heroku + MEAN 堆栈错误:参数“url”必须是字符串,而不是未定义