Mongoid endless loop upon request

I am using mongolab and monogid on a heroku instance.

I can access my models inside the rails console and create them.

However - calling Model.first/ Model.find_or_initialize_bycauses an infinite loop

MOPED: xx:29454 COMMAND      database=admin command={:ismaster=>1} runtime: 7.8211ms
  MOPED: xx:29454 QUERY        database=heroku_zl6cgccm 

collection=calculations selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 1.6873ms
MOPED: xx:29454 QUERY        database=heroku_xx collection=calculations selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 5.3479ms
  MOPED: xx:29454 QUERY        database=heroku_xx collection=calculations selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 8.8738ms

all the way down:

collection=calculations selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 1544.7719ms
SystemStackError: stack level too deep
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/string.rb:79:in `to_bson_cstring'
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/string.rb:63:in `to_bson_key'
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/symbol.rb:58:in `to_bson_key'
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/hash.rb:45:in `block (2 levels) in to_bson'
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/hash.rb:43:in `each'
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/hash.rb:43:in `block in to_bson'
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/encodable.rb:57:in `encode_with_placeholder_and_null'
    from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/hash.rb:42:in `to_bson'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/protocol/message.rb:173:in `serialize_selector'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/protocol/message.rb:325:in `serialize'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/connection.rb:171:in `block in write'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/connection.rb:169:in `each'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/connection.rb:169:in `write'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/authenticatable.rb:64:in `login'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/operation/read.rb:55:in `block (2 levels) in execute'
    from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/node.rb:118:in `block in connection'
... 9285 levels...
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/query_cache.rb:190:in `first_with_cache'
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:199:in `block (2 levels) in first'
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:545:in `with_sorting'
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:198:in `block in first'
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:457:in `try_cache'
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:197:in `first'
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual.rb:20:in `first'
    from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/findable.rb:144:in `first'
    from (irb):10
    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
    from /app/bin/rails:8:in `require'

here is my mongoid.ymlfile:

production:
 sessions:
   default:
     uri: <%= ENV['MONGOLAB_URI'] %>

     options:

       max_retries: 30
       retry_interval: 1
       timeout: 15

       refresh_interval: 10
+4
source share
2 answers

I had a similar problem with MongoLab, with endless database queries, until I hit the stack level too deep. My client wanted a free database, so I tried to troubleshoot for 12 hours.

, , : https://devcenter.heroku.com/articles/mongolab

, : " MongoDB 3.0 ( MongoLab 3.0), Mongoid 5.x.". , .

gemfile :

gem 'mongoid', '~ > 5.0'

mongoid.yml

rails g mongoid: config

mongoid.yml "" "", Rails.

, .

+3

( ), MongoHQ Heroku, , , , .

# , ,         # . :: direct,: replica_set,: sharded. :         # .         connect:: direct

http://docs.mongodb.org/ecosystem/tutorial/ruby-mongoid-tutorial/#anatomy-of-a-mongoid-config

Mongo Compose, , .

MongoLab, , , :

options: 
  connect: :sharded

: http://docs.mongodb.org/manual/core/sharded-cluster-query-router/#broadcast-operations

+2

Source: https://habr.com/ru/post/1610286/


All Articles