From 57b757cb4122fbc46c2f3e07a6a75fb302985f8e Mon Sep 17 00:00:00 2001 From: marcsello Date: Mon, 26 Jul 2021 15:24:17 +0200 Subject: [PATCH] Added amqp to health check --- src/app.py | 4 +++- src/healthchecks.py | 12 ++++++++++++ src/magic_ampq.py | 7 +++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/app.py b/src/app.py index 1638ae1..532cb79 100644 --- a/src/app.py +++ b/src/app.py @@ -11,7 +11,7 @@ from db import db from marshm import ma from influxus import influx_db from resources import SampleResource, SampleParameterResource -from healthchecks import health_database_status +from healthchecks import health_database_status, ampq_connection_status import atexit @@ -70,5 +70,7 @@ api.add_resource(SampleResource, "/sample") api.add_resource(SampleParameterResource, '/sample/') health.add_check(health_database_status) +health.add_check(ampq_connection_status) + app.add_url_rule("/healthz", "healthcheck", view_func=lambda: health.run()) diff --git a/src/healthchecks.py b/src/healthchecks.py index ed614c2..3d18ea0 100644 --- a/src/healthchecks.py +++ b/src/healthchecks.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 from db import db +from magic_ampq import magic_ampq """ Healthchek functions @@ -21,3 +22,14 @@ def health_database_status(): output = str(e) is_database_working = False return is_database_working, output + + +def ampq_connection_status(): + if magic_ampq.is_healthy(): + result = True + text = "ampq connection is ok" + else: + result = False + text = "ampq connection is unhealthy" + + return result, text diff --git a/src/magic_ampq.py b/src/magic_ampq.py index 97b2c70..bb625a6 100644 --- a/src/magic_ampq.py +++ b/src/magic_ampq.py @@ -90,6 +90,13 @@ class MagicAMPQ: if tries > 10: time.sleep(2) + def is_healthy(self) -> bool: + with self._lock: + if not self._pika_channel: + return False + + return self._pika_channel.is_open and self._pika_connection.is_open + # instance to be used in the flask app magic_ampq = MagicAMPQ()