forked from p15670423/monkey
Add wait for mongodb
This commit is contained in:
parent
1f6c5ffecb
commit
16a470ee22
|
@ -1,5 +1,16 @@
|
||||||
from flask_pymongo import PyMongo
|
from flask_pymongo import PyMongo
|
||||||
|
from flask_pymongo import MongoClient
|
||||||
|
from pymongo.errors import ServerSelectionTimeoutError
|
||||||
|
|
||||||
__author__ = 'Barak'
|
__author__ = 'Barak'
|
||||||
|
|
||||||
mongo = PyMongo()
|
mongo = PyMongo()
|
||||||
|
|
||||||
|
|
||||||
|
def is_db_server_up(mongo_url):
|
||||||
|
client = MongoClient(mongo_url, serverSelectionTimeoutMS=100)
|
||||||
|
try:
|
||||||
|
client.server_info()
|
||||||
|
return True
|
||||||
|
except ServerSelectionTimeoutError:
|
||||||
|
return False
|
||||||
|
|
|
@ -3,6 +3,8 @@ from __future__ import print_function # In python 2.7
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
if BASE_PATH not in sys.path:
|
if BASE_PATH not in sys.path:
|
||||||
sys.path.insert(0, BASE_PATH)
|
sys.path.insert(0, BASE_PATH)
|
||||||
|
@ -10,13 +12,21 @@ if BASE_PATH not in sys.path:
|
||||||
from cc.app import init_app
|
from cc.app import init_app
|
||||||
from cc.utils import local_ip_addresses
|
from cc.utils import local_ip_addresses
|
||||||
from cc.island_config import DEFAULT_MONGO_URL, ISLAND_PORT
|
from cc.island_config import DEFAULT_MONGO_URL, ISLAND_PORT
|
||||||
|
from cc.database import is_db_server_up
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
from tornado.wsgi import WSGIContainer
|
from tornado.wsgi import WSGIContainer
|
||||||
from tornado.httpserver import HTTPServer
|
from tornado.httpserver import HTTPServer
|
||||||
from tornado.ioloop import IOLoop
|
from tornado.ioloop import IOLoop
|
||||||
|
|
||||||
app = init_app(os.environ.get('MONGO_URL', DEFAULT_MONGO_URL))
|
mongo_url = os.environ.get('MONGO_URL', DEFAULT_MONGO_URL)
|
||||||
|
|
||||||
|
while not is_db_server_up(mongo_url):
|
||||||
|
print('Waiting for MongoDB server')
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
app = init_app(mongo_url)
|
||||||
|
|
||||||
http_server = HTTPServer(WSGIContainer(app),
|
http_server = HTTPServer(WSGIContainer(app),
|
||||||
ssl_options={'certfile': os.environ.get('SERVER_CRT', 'server.crt'),
|
ssl_options={'certfile': os.environ.get('SERVER_CRT', 'server.crt'),
|
||||||
'keyfile': os.environ.get('SERVER_KEY', 'server.key')})
|
'keyfile': os.environ.get('SERVER_KEY', 'server.key')})
|
||||||
|
|
Loading…
Reference in New Issue