From 5a5c510fa09c0c57a81a41f65d1a07af1a63bb61 Mon Sep 17 00:00:00 2001 From: marcsello Date: Thu, 19 Aug 2021 03:12:09 +0200 Subject: [PATCH] Refactored health checks --- storage_service/app.py | 9 +++------ storage_service/utils/__init__.py | 2 +- storage_service/utils/healthchecks.py | 18 ++++++++---------- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/storage_service/app.py b/storage_service/app.py index ecabb4e..dd5710f 100644 --- a/storage_service/app.py +++ b/storage_service/app.py @@ -3,10 +3,10 @@ import sentry_sdk from sentry_sdk.integrations.flask import FlaskIntegration from flask import Flask from werkzeug.middleware.proxy_fix import ProxyFix -from healthcheck import HealthCheck + # import stuff -from utils import register_all_error_handlers, storage, health_database_status +from utils import register_all_error_handlers, storage, register_health_checks # import views from views import ObjectView @@ -38,8 +38,6 @@ app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1) # init stuff storage.init_app(app) -health = HealthCheck() - # register error handlers register_all_error_handlers(app) @@ -58,8 +56,7 @@ tracing = FlaskTracing(initialize_tracer, True, app) for view in [ObjectView]: view.register(app, trailing_slash=False) -health.add_check(health_database_status) -app.add_url_rule("/healthz", "healthcheck", view_func=lambda: health.run()) +register_health_checks(app) # start debugging if needed if __name__ == "__main__": diff --git a/storage_service/utils/__init__.py b/storage_service/utils/__init__.py index 6c02400..2586551 100644 --- a/storage_service/utils/__init__.py +++ b/storage_service/utils/__init__.py @@ -2,4 +2,4 @@ from .require_decorators import json_required from .error_handlers import register_all_error_handlers from .storage import storage -from .healthchecks import health_database_status \ No newline at end of file +from .healthchecks import register_health_checks diff --git a/storage_service/utils/healthchecks.py b/storage_service/utils/healthchecks.py index 45cda3b..6d049e4 100644 --- a/storage_service/utils/healthchecks.py +++ b/storage_service/utils/healthchecks.py @@ -1,15 +1,7 @@ #!/usr/bin/env python3 - from utils import storage - -""" -Healthchek functions -""" - -__author__ = "@tormakris" -__copyright__ = "Copyright 2020, Birbnetes Team" -__module_name__ = "healthchecks" -__version__text__ = "1" +from healthcheck import HealthCheck +from flask import Flask def health_database_status(): @@ -21,3 +13,9 @@ def health_database_status(): output = str(e) is_database_working = False return is_database_working, output + + +def register_health_checks(app: Flask): + health = HealthCheck() + health.add_check(health_database_status) + app.add_url_rule("/healthz", "healthcheck", view_func=lambda: health.run())