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

This commit is contained in:
Torma Kristóf 2020-11-19 01:54:16 +01:00
parent 5427aeb39a
commit a35c1accd6
3 changed files with 29 additions and 0 deletions

View File

@ -10,3 +10,4 @@ flask-marshmallow
paho-mqtt
flask-mqtt
psycopg2-binary
py-healthcheck

View File

@ -6,6 +6,7 @@ import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from healthcheck import HealthCheck
from resources import *
from config import *
@ -14,6 +15,7 @@ from marshm import ma
from mqtt_flask_instance import mqtt
from mqtt_methods import handle_status_message
from healthchecks import health_database_status
"""
Main Flask RESTful API
@ -53,6 +55,7 @@ api = Api(app)
db.init_app(app)
ma.init_app(app)
mqtt.init_app(app)
health = HealthCheck(app, "/healthz")
with app.app_context():
db.create_all()
@ -98,6 +101,8 @@ api.add_resource(SensorResource, "/devices/{deviceid}/{sensorid}")
api.add_resource(SensorOfflineResource, "/devices/{deviceid}/{sensorid}/offline")
api.add_resource(SensorOnlineResource, "/devices/{deviceid}/{sensorid}/online")
health.add_check(health_database_status)
if __name__ == "__main__":
app.run(
debug=bool(DEBUG),

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