clearml-server/server/database/__init__.py

75 lines
1.9 KiB
Python
Raw Normal View History

from os import getenv
from furl import furl
2019-06-10 21:24:35 +00:00
from jsonmodels import models
from jsonmodels.errors import ValidationError
from jsonmodels.fields import StringField
from mongoengine import register_connection
from mongoengine.connection import get_connection
from config import config
from .defs import Database
from .utils import get_items
log = config.logger("database")
strict = config.get("apiserver.mongo.strict", True)
2019-06-10 21:24:35 +00:00
OVERRIDE_HOST_ENV_KEY = "MONGODB_SERVICE_SERVICE_HOST"
2019-06-10 21:24:35 +00:00
_entries = []
class DatabaseEntry(models.Base):
host = StringField(required=True)
alias = StringField()
@property
def health_alias(self):
return "__health__" + self.alias
2019-06-10 21:24:35 +00:00
def initialize():
db_entries = config.get("hosts.mongo", {})
2019-06-10 21:24:35 +00:00
missing = []
log.info("Initializing database connections")
override_hostname = getenv(OVERRIDE_HOST_ENV_KEY)
if override_hostname:
log.info(f"Using override mongodb host {override_hostname}")
2019-06-10 21:24:35 +00:00
for key, alias in get_items(Database).items():
if key not in db_entries:
missing.append(key)
continue
2019-06-10 21:24:35 +00:00
entry = DatabaseEntry(alias=alias, **db_entries.get(key))
if override_hostname:
entry.host = furl(entry.host).set(host=override_hostname).url
2019-06-10 21:24:35 +00:00
try:
entry.validate()
log.info(
"Registering connection to %(alias)s (%(host)s)" % entry.to_struct()
)
2019-06-10 21:24:35 +00:00
register_connection(alias=alias, host=entry.host)
_entries.append(entry)
except ValidationError as ex:
raise Exception("Invalid database entry `%s`: %s" % (key, ex.args[0]))
2019-06-10 21:24:35 +00:00
if missing:
raise ValueError("Missing database configuration for %s" % ", ".join(missing))
2019-06-10 21:24:35 +00:00
def get_entries():
return _entries
def get_aliases():
return [entry.alias for entry in get_entries()]
def reconnect():
for entry in get_entries():
get_connection(entry.alias, reconnect=True)