3

I am using mongoid orm and trying to establish connection dynamically.

def establish_mongo_connection publish_detail
  Mongoid.disconnect_clients
  Mongoid.clients.clear
  Mongoid.configure do |config|
    a = config.connect_to publish_detail.server.database_name, auth_source: 'admin', auth_mech: :scram
  end
  Mongoid.clients[:default][:hosts] = "mongodb://#{publish_detail.server.database_user}:#{publish_detail.server.database_password}@#{publish_detail.server.database_connection}"
end

After establishing the connection I am doing business logic and finally saving the records (through MongoID model objects).

This whole processing and saving objects takes somewhere around 10-12 seconds.

The problem is sometimes records are saved in different mongo server than the one it was intended for.

I am running 2 puma server instances.

Locally everything works fine but misbehaves on server.

Please let me know if I m doing something wrong and if there's a more correct way of doing this.

Thanks in advance :)

waqar mirza
  • 555
  • 2
  • 15

0 Answers0