Skip to content

Commit e69fa04

Browse files
author
ivan
committed
Added health check with check connection to db adapter
1 parent eaa8cc8 commit e69fa04

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

core/db_adapter/aioredis_adapter.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,9 @@ async def _path_exists(self, path):
7070

7171
async def _on_prepare(self):
7272
pass
73+
74+
async def is_alive(self) -> bool | None:
75+
try:
76+
return self._redis.ping()
77+
except Exception as _:
78+
log("AIORedisAdapter is not alive", level="INFO")

core/db_adapter/db_adapter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ def _on_all_tries_fail(self):
9191
class AsyncDBAdapter(DBAdapter):
9292
IS_ASYNC = True
9393

94+
async def is_alive(self) -> bool | None:
95+
raise NotImplementedError
96+
9497
async def _on_all_tries_fail(self):
9598
raise
9699

core/db_adapter/ignite_adapter.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,10 @@ def _on_prepare(self):
8888

8989
async def _get_counter_name(self):
9090
return "ignite_async_adapter"
91+
92+
async def is_alive(self) -> bool | None:
93+
try:
94+
await self.connect()
95+
return True
96+
except Exception as _:
97+
log("IgniteAdapter is not alive", level="INFO")

core/db_adapter/memory_adapter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,6 @@ async def _get(self, id):
4949

5050
async def _list_dir(self, path):
5151
pass
52+
53+
async def is_alive(self) -> bool | None:
54+
return True

smart_kit/start_points/main_loop_async_http.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def __init__(self, *args, **kwargs):
2121
self.app = aiohttp.web.Application()
2222
super().__init__(*args, **kwargs)
2323
self.app.add_routes([aiohttp.web.route('*', '/health', self.get_health_check)])
24+
self.app.add_routes([aiohttp.web.route('*', '/health_db_adapter', self.get_health_db_adapter_check)])
2425
self.app.add_routes([aiohttp.web.route('*', '/{tail:.*}', self.iterate)])
2526

2627
async def async_init(self):
@@ -174,6 +175,21 @@ async def get_health_check(self, request: aiohttp.web.Request):
174175
status=status, reason=reason, data=answer,
175176
)
176177

178+
async def get_health_db_adapter_check(self, request: aiohttp.web.Request):
179+
status, reason, answer = 200, "OK", "ok"
180+
try:
181+
is_alive = await self.db_adapter.is_alive()
182+
if not is_alive:
183+
status, reason, answer = 500, "ERROR", "db adapter connection error"
184+
except NotImplementedError as _:
185+
status, reason, answer = 500, "ERROR", f"Method is not implemented in {type(self.db_adapter)}"
186+
except Exception as e:
187+
status, reason, answer = 500, "ERROR", str(e)
188+
189+
return aiohttp.web.json_response(
190+
status=status, reason=reason, data=answer,
191+
)
192+
177193
async def iterate(self, request: aiohttp.web.Request):
178194
headers = self._get_headers(request.headers)
179195
body = await request.text()

0 commit comments

Comments
 (0)