diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index b41d37df..1a068170 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -24,6 +24,7 @@ - Replaced entity with getter (#652) - Resolved TODO in Dockerfile (#680) - Resolved TODO at src/reporter/tests/test_timescale_types.py (#667) +- Resolved TODO at src/reporter/health.py (#705) ### Bug fixes diff --git a/src/reporter/health.py b/src/reporter/health.py index 284eca2d..f87552e8 100644 --- a/src/reporter/health.py +++ b/src/reporter/health.py @@ -2,7 +2,7 @@ from geocoding.factory import get_geo_cache, is_geo_coding_available from cache.factory import get_cache, is_cache_available from translators.factory import CRATE_BACKEND, TIMESCALE_BACKEND, \ - default_backend + default_backend, secondary_backend def check_db(db=CRATE_BACKEND): @@ -20,6 +20,11 @@ def check_db(db=CRATE_BACKEND): with postgres_translator_instance() as trans: health = trans.get_health() return health + if db2 == TIMESCALE_BACKEND: + from translators.timescale import postgres_translator_instance + with postgres_translator_instance() as trans: + health = trans.get_health() + return health def check_cache(): @@ -82,14 +87,18 @@ def get_health(with_geocoder=False): # Check defaultDB (critical) db = default_backend().lower() + db2 = secondary_backend().lower() + if db2 is None: + res = _check_critical(check_db(db2), res, db2) + else: + res = _check_not_critical(check_db(db2), res, db2) try: res = _check_critical(check_db(db), res, db) + except Exception: res['status'] = 'fail' res.setdefault('details', {})[db] = 'cannot reach ' + db - # TODO add not critical check if a secondary db is configured - # Check cache (not critical) res = _check_not_critical(check_cache(), res, 'redis') diff --git a/src/translators/factory.py b/src/translators/factory.py index 15f622c5..3ff23757 100644 --- a/src/translators/factory.py +++ b/src/translators/factory.py @@ -15,6 +15,8 @@ QL_DEFAULT_DB_ENV_VAR = 'QL_DEFAULT_DB' +QL_SECONDARY_DB_ENV_VAR = 'QL_SECONDARY_DB' + def log(): return logging.getLogger(__name__) @@ -44,6 +46,19 @@ def default_backend() -> MaybeString: return env_backend or config_backend or CRATE_BACKEND +def secondary_backend() -> MaybeString: + cfg_reader = YamlReader(log=log().debug) + env_reader = EnvReader(log=log().debug) + + config = cfg_reader.from_env_file(QL_CONFIG_ENV_VAR, defaults={}) + + config_backend = maybe_string_match(config, 'secondary-backend') + + env_backend = env_reader.read(StrVar(QL_DEFAULT_DB_ENV_VAR, None)) + + return env_backend or config_backend or TIMESCALE_BACKEND + + def backend_id_for(fiware_service: str) -> str: backend = lookup_backend(fiware_service) backend = backend.strip().lower() if backend is not None else '' diff --git a/timescale-container/quantumleap-db-setup.py b/timescale-container/quantumleap-db-setup.py index 84ab0501..4b7c1027 100644 --- a/timescale-container/quantumleap-db-setup.py +++ b/timescale-container/quantumleap-db-setup.py @@ -205,7 +205,7 @@ class CreateDb: OWNER ${db_user} ENCODING 'UTF8'; -\connect ${db_name} +\\connect ${db_name} CREATE EXTENSION IF NOT EXISTS postgis CASCADE; CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;