|
| 1 | +import os |
| 2 | + |
| 3 | +import requests |
| 4 | + |
| 5 | +from lambda_app.services.v1.healthcheck import AbstractHealthCheck, HealthCheckResult |
| 6 | +from lambda_app.database.mysql import get_connection |
| 7 | +from lambda_app.database.redis import get_connection as redis_get_connection |
| 8 | +from lambda_app.events.aws.sqs import SQSEvents |
| 9 | + |
| 10 | + |
| 11 | +class SelfConnectionHealthCheck(AbstractHealthCheck): |
| 12 | + def __init__(self, logger=None, config=None): |
| 13 | + super().__init__(logger=logger, config=config) |
| 14 | + |
| 15 | + def check_health(self): |
| 16 | + result = False |
| 17 | + description = "Unable to connect" |
| 18 | + check_result = HealthCheckResult.unhealthy(description) |
| 19 | + response = None |
| 20 | + try: |
| 21 | + result = True |
| 22 | + url = os.environ["API_SERVER"] if "API_SERVER" in os.environ else None |
| 23 | + url = url + "/docs" |
| 24 | + self.logger.info("requesting url: {}".format(url)) |
| 25 | + response = requests.get(url) |
| 26 | + if response: |
| 27 | + if response.status_code == 200: |
| 28 | + result = True |
| 29 | + description = "Connection successful" |
| 30 | + else: |
| 31 | + result = False |
| 32 | + description = "Something wrong" |
| 33 | + check_result = HealthCheckResult.degraded(description) |
| 34 | + else: |
| 35 | + raise Exception("Unable to connect") |
| 36 | + except Exception as err: |
| 37 | + self.logger.error(err) |
| 38 | + |
| 39 | + if result: |
| 40 | + check_result = HealthCheckResult.healthy(description) |
| 41 | + return check_result |
| 42 | + |
| 43 | + |
| 44 | +class MysqlConnectionHealthCheck(AbstractHealthCheck): |
| 45 | + def __init__(self, logger=None, config=None, mysql_connection=None): |
| 46 | + super().__init__(logger=logger, config=config) |
| 47 | + # database connection |
| 48 | + self.mysql_connection = mysql_connection if mysql_connection is not None else get_connection() |
| 49 | + |
| 50 | + def check_health(self): |
| 51 | + result = False |
| 52 | + description = "Unable to connect" |
| 53 | + check_result = HealthCheckResult.unhealthy(description) |
| 54 | + |
| 55 | + try: |
| 56 | + if self.mysql_connection: |
| 57 | + self.mysql_connection.connect() |
| 58 | + self.mysql_connection.ping() |
| 59 | + result = True |
| 60 | + description = "Connection successful" |
| 61 | + else: |
| 62 | + raise Exception("mysql_connection is None") |
| 63 | + except Exception as err: |
| 64 | + self.logger.error(err) |
| 65 | + |
| 66 | + if result: |
| 67 | + check_result = HealthCheckResult.healthy(description) |
| 68 | + return check_result |
| 69 | + |
| 70 | + |
| 71 | +class RedisConnectionHealthCheck(AbstractHealthCheck): |
| 72 | + def __init__(self, logger=None, config=None, redis_connection=None): |
| 73 | + super().__init__(logger=logger, config=config) |
| 74 | + # database connection |
| 75 | + self.redis_connection = redis_connection if redis_connection is not None else redis_get_connection() |
| 76 | + |
| 77 | + def check_health(self): |
| 78 | + result = False |
| 79 | + description = "Unable to connect" |
| 80 | + check_result = HealthCheckResult.unhealthy(description) |
| 81 | + |
| 82 | + try: |
| 83 | + if self.redis_connection: |
| 84 | + result = self.redis_connection.set('connection', 'true') |
| 85 | + description = "Connection successful" |
| 86 | + else: |
| 87 | + raise Exception("redis_connection is None") |
| 88 | + except Exception as err: |
| 89 | + self.logger.error(err) |
| 90 | + |
| 91 | + if result: |
| 92 | + check_result = HealthCheckResult.healthy(description) |
| 93 | + return check_result |
| 94 | + |
| 95 | + |
| 96 | +class SQSConnectionHealthCheck(AbstractHealthCheck): |
| 97 | + def __init__(self, logger=None, config=None, sqs_events=None): |
| 98 | + super().__init__(logger=logger, config=config) |
| 99 | + # sqs_events connection |
| 100 | + self.sqs_events = sqs_events if sqs_events is not None else SQSEvents() |
| 101 | + |
| 102 | + def check_health(self): |
| 103 | + result = False |
| 104 | + description = "Unable to connect" |
| 105 | + check_result = HealthCheckResult.unhealthy(description) |
| 106 | + |
| 107 | + try: |
| 108 | + if self.sqs_events: |
| 109 | + connection = self.sqs_events.connect() |
| 110 | + if connection: |
| 111 | + result = True |
| 112 | + description = "Connection successful" |
| 113 | + else: |
| 114 | + raise Exception("redis_connection is None") |
| 115 | + except Exception as err: |
| 116 | + self.logger.error(err) |
| 117 | + |
| 118 | + if result: |
| 119 | + check_result = HealthCheckResult.healthy(description) |
| 120 | + return check_result |
0 commit comments