diff --git a/requirements.txt b/requirements.txt index 21fa9bc..62de5db 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,5 @@ marshmallow Flask-Classful gunicorn sentry_sdk -flask_minio \ No newline at end of file +flask_minio +py-healthcheck \ No newline at end of file diff --git a/storage_service/app.py b/storage_service/app.py index 3725402..5dd2eb4 100644 --- a/storage_service/app.py +++ b/storage_service/app.py @@ -4,9 +4,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 +from utils import register_all_error_handlers, storage, health_database_status # import views from views import ObjectView @@ -41,6 +42,9 @@ app.config['MINIO_BUCKET_NAME'] = os.environ['MINIO_BUCKET_NAME'] app.config['MINIO_SECURE'] = os.environ.get('MINIO_SECURE', False) app.config['MINIO_REGION'] = os.environ.get('MINIO_REGION', None) +health = HealthCheck(app, "/healthz") + + # register error handlers register_all_error_handlers(app) @@ -48,6 +52,8 @@ register_all_error_handlers(app) for view in [ObjectView]: view.register(app, trailing_slash=False) +health.add_check(health_database_status) + # start debugging if needed if __name__ == "__main__": app.run(debug=True) diff --git a/storage_service/utils/__init__.py b/storage_service/utils/__init__.py index cd90b13..6c02400 100644 --- a/storage_service/utils/__init__.py +++ b/storage_service/utils/__init__.py @@ -2,3 +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 diff --git a/storage_service/utils/healthchecks.py b/storage_service/utils/healthchecks.py new file mode 100644 index 0000000..45cda3b --- /dev/null +++ b/storage_service/utils/healthchecks.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +from utils import storage + +""" +Healthchek functions +""" + +__author__ = "@tormakris" +__copyright__ = "Copyright 2020, Birbnetes Team" +__module_name__ = "healthchecks" +__version__text__ = "1" + + +def health_database_status(): + is_database_working = True + output = 'storage is ok' + try: + storage.connection.list_buckets() + except Exception as e: + output = str(e) + is_database_working = False + return is_database_working, output