add health endpoint
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Torma Kristóf 2020-11-19 00:30:26 +01:00
parent 5b736b2844
commit 5d5bb9cd92
3 changed files with 28 additions and 1 deletions

View File

@ -12,3 +12,4 @@ psycopg2-binary
marshmallow marshmallow
marshmallow-sqlalchemy marshmallow-sqlalchemy
flask-marshmallow flask-marshmallow
py-healthcheck

View File

@ -5,12 +5,13 @@ from flask_restful import Api
import sentry_sdk import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from healthcheck import HealthCheck
from config import * from config import *
from db import db from db import db
from marshm import ma from marshm import ma
from resources import SampleResource, SampleParameterResource from resources import SampleResource, SampleParameterResource
from healthchecks import health_database_status
""" """
Main Flask RESTful APIm Main Flask RESTful APIm
@ -42,6 +43,7 @@ app.config['FLASK_PIKA_PARAMS'] = {'host': RABBITMQ_HOST,
'port': 5672, 'port': 5672,
'virtual_host': '/'} 'virtual_host': '/'}
api = Api(app) api = Api(app)
health = HealthCheck(app, "/healthz")
db.init_app(app) db.init_app(app)
ma.init_app(app) ma.init_app(app)
@ -62,6 +64,7 @@ logger.addHandler(handler)
api.add_resource(SampleResource, "/sample") api.add_resource(SampleResource, "/sample")
api.add_resource(SampleParameterResource, '/sample/<tag>') api.add_resource(SampleParameterResource, '/sample/<tag>')
health.add_check(health_database_status)
if __name__ == "__main__": if __name__ == "__main__":
app.run( app.run(

23
src/healthchecks.py Normal file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env python3
from db import db
"""
Healthchek functions
"""
__author__ = "@tormakris"
__copyright__ = "Copyright 2020, Birbnetes Team"
__module_name__ = "healthchecks"
__version__text__ = "1"
def health_database_status():
is_database_working = True
output = 'database is ok'
try:
db.session.execute('SELECT 1')
except Exception as e:
output = str(e)
is_database_working = False
return is_database_working, output