This commit is contained in:
		@@ -10,3 +10,4 @@ flask-marshmallow
 | 
				
			|||||||
paho-mqtt
 | 
					paho-mqtt
 | 
				
			||||||
flask-mqtt
 | 
					flask-mqtt
 | 
				
			||||||
psycopg2-binary
 | 
					psycopg2-binary
 | 
				
			||||||
 | 
					py-healthcheck
 | 
				
			||||||
@@ -6,6 +6,7 @@ import sentry_sdk
 | 
				
			|||||||
from sentry_sdk.integrations.flask import FlaskIntegration
 | 
					from sentry_sdk.integrations.flask import FlaskIntegration
 | 
				
			||||||
from sentry_sdk.integrations.logging import LoggingIntegration
 | 
					from sentry_sdk.integrations.logging import LoggingIntegration
 | 
				
			||||||
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
 | 
					from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
 | 
				
			||||||
 | 
					from healthcheck import HealthCheck
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from resources import *
 | 
					from resources import *
 | 
				
			||||||
from config import *
 | 
					from config import *
 | 
				
			||||||
@@ -14,6 +15,7 @@ from marshm import ma
 | 
				
			|||||||
from mqtt_flask_instance import mqtt
 | 
					from mqtt_flask_instance import mqtt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from mqtt_methods import handle_status_message
 | 
					from mqtt_methods import handle_status_message
 | 
				
			||||||
 | 
					from healthchecks import health_database_status
 | 
				
			||||||
 | 
					
 | 
				
			||||||
"""
 | 
					"""
 | 
				
			||||||
Main Flask RESTful API
 | 
					Main Flask RESTful API
 | 
				
			||||||
@@ -53,6 +55,7 @@ api = Api(app)
 | 
				
			|||||||
db.init_app(app)
 | 
					db.init_app(app)
 | 
				
			||||||
ma.init_app(app)
 | 
					ma.init_app(app)
 | 
				
			||||||
mqtt.init_app(app)
 | 
					mqtt.init_app(app)
 | 
				
			||||||
 | 
					health = HealthCheck(app, "/healthz")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
with app.app_context():
 | 
					with app.app_context():
 | 
				
			||||||
    db.create_all()
 | 
					    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(SensorOfflineResource, "/devices/{deviceid}/{sensorid}/offline")
 | 
				
			||||||
api.add_resource(SensorOnlineResource, "/devices/{deviceid}/{sensorid}/online")
 | 
					api.add_resource(SensorOnlineResource, "/devices/{deviceid}/{sensorid}/online")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					health.add_check(health_database_status)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
    app.run(
 | 
					    app.run(
 | 
				
			||||||
        debug=bool(DEBUG),
 | 
					        debug=bool(DEBUG),
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										23
									
								
								src/healthchecks.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/healthchecks.py
									
									
									
									
									
										Normal 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
 | 
				
			||||||
		Reference in New Issue
	
	Block a user