Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue#705 #711

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 12 additions & 3 deletions src/reporter/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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():
Expand Down Expand Up @@ -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')

Expand Down
15 changes: 15 additions & 0 deletions src/translators/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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 ''
Expand Down
2 changes: 1 addition & 1 deletion timescale-container/quantumleap-db-setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down