Mongoengine connecting with atlas

After connecting the working application to the local mongo, we decided to switch to Mongo Atlas. This caused manufacturing errors.

Our stack is docker -> alping 3.6 -> python 2.7.13 -> Flask -> uwsgi (2.0.17) -> nginx running on aws
flask-mongoengine-0.9.3 mongoengine-0.14.3 pymongo-3.5.1

when you start the application in staging/production, uwsgisends No replica set members found yet.

We do not know why.

We tried different connection settings connect: False, which is a lazy connection, which means not initialization, but the first request. This caused a nginx error for the resource temporarily unavailableerror in some of our applications. We had to restart several times for the application in order to finally start serving requests.

I think the problem is with pimongo and the fact that it is not fork-safe http://api.mongodb.com/python/current/faq.html?highlight=thread#id3 and uwsgiuses forks

I suspect this may be due to the way my application activates. perhaps with aginst Using PyMongo with multiprocessing, here is the application initialization code:

from app import FlaskApp
from flask import current_app

app = None

from flask_mongoengine import MongoEngine

import logging

application, app = init_flask_app(app_instance, module_name='my_module')


def init_flask_app(app_instance, **kwargs):
    app_instance.init_instance(environment_config)

    application = app_instance.get_instance()
    app = application.app
    return application, app 

# app_instance.py
import FlaskApp
def init_instance(env):
    global app
    app = FlaskApp(env)
    return app


def get_instance():
    if globals().get('app') is None:
        app = current_app.flask_app_object
    else:
        app = globals().get('app')

    assert app is not None
    return app




class FlaskApp(object):

    def __init__(self, env):
        .....

        # Initialize the DB
        self.db = Database(self.app)
        ....
        # used in app_instance.py to get the flask app object in case it None
        self.app.flask_app_object = self    

    def run_server(self):
        self.app.run(host=self.app.config['HOST'], port=self.app.config['PORT'], debug=self.app.config['DEBUG'])



class Database(object):

    def __init__(self, app):
        self.db = MongoEngine(app)

    def drop_all(self, database_name):
        logging.warn("Dropping database %s" % database_name)
        self.db.connection.drop_database(database_name)


if __name__ == '__main__':
    application.run_server()

debugging help would be appreciated!

+4
source share
1 answer

The error of your message seems clear.

Your mongo setup uses replicas. They seem to be unable to communicate with each other or are incorrectly configured.

-1
source

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


All Articles