Skip to content

Commit

Permalink
Merge pull request #199 from IvanZotin/main
Browse files Browse the repository at this point in the history
Added health_db_adapter endpoint
  • Loading branch information
SyrexMinus authored Jun 18, 2024
2 parents eaa8cc8 + 7eb77fb commit 55adb8e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
10 changes: 8 additions & 2 deletions core/db_adapter/aioredis_adapter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import copy
import importlib
import typing
from typing import Optional

from core.db_adapter import error
from core.db_adapter.db_adapter import AsyncDBAdapter
Expand All @@ -13,7 +13,7 @@ def __init__(self, config=None):
super().__init__(config)
self.aioredis = importlib.import_module("redis", "asyncio")
redis_type = self.aioredis.Redis
self._redis: typing.Optional[redis_type] = None
self._redis: Optional[redis_type] = None

try:
del self.config["type"]
Expand Down Expand Up @@ -70,3 +70,9 @@ async def _path_exists(self, path):

async def _on_prepare(self):
pass

async def is_alive(self) -> Optional[bool]:
try:
return self._redis.ping()
except Exception:
log("AIORedisAdapter is not alive", level="INFO")
4 changes: 4 additions & 0 deletions core/db_adapter/db_adapter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding: utf-8
import asyncio
from typing import Optional

import core.logging.logger_constants as log_const
from core.logging.logger_utils import log
Expand Down Expand Up @@ -91,6 +92,9 @@ def _on_all_tries_fail(self):
class AsyncDBAdapter(DBAdapter):
IS_ASYNC = True

async def is_alive(self) -> Optional[bool]:
raise NotImplementedError

async def _on_all_tries_fail(self):
raise

Expand Down
7 changes: 7 additions & 0 deletions core/db_adapter/ignite_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,10 @@ def _on_prepare(self):

async def _get_counter_name(self):
return "ignite_async_adapter"

async def is_alive(self) -> Optional[bool]:
try:
await self.connect()
return True
except Exception:
log("IgniteAdapter is not alive", level="INFO")
4 changes: 4 additions & 0 deletions core/db_adapter/memory_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from copy import copy
from cachetools import TTLCache
from core.db_adapter import error
from typing import Optional


class MemoryAdapter(AsyncDBAdapter):
Expand Down Expand Up @@ -49,3 +50,6 @@ async def _get(self, id):

async def _list_dir(self, path):
pass

async def is_alive(self) -> Optional[bool]:
return True
16 changes: 16 additions & 0 deletions smart_kit/start_points/main_loop_async_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self, *args, **kwargs):
self.app = aiohttp.web.Application()
super().__init__(*args, **kwargs)
self.app.add_routes([aiohttp.web.route('*', '/health', self.get_health_check)])
self.app.add_routes([aiohttp.web.route('*', '/health_db_adapter', self.get_health_db_adapter_check)])
self.app.add_routes([aiohttp.web.route('*', '/{tail:.*}', self.iterate)])

async def async_init(self):
Expand Down Expand Up @@ -174,6 +175,21 @@ async def get_health_check(self, request: aiohttp.web.Request):
status=status, reason=reason, data=answer,
)

async def get_health_db_adapter_check(self, request: aiohttp.web.Request):
status, reason, answer = 200, "OK", "ok"
try:
is_alive = await self.db_adapter.is_alive()
if not is_alive:
status, reason, answer = 500, "ERROR", "db adapter connection error"
except NotImplementedError:
status, reason, answer = 500, "ERROR", f"Method is not implemented in {type(self.db_adapter)}"
except Exception as e:
status, reason, answer = 500, "ERROR", str(e)

return aiohttp.web.json_response(
status=status, reason=reason, data=answer,
)

async def iterate(self, request: aiohttp.web.Request):
headers = self._get_headers(request.headers)
body = await request.text()
Expand Down

0 comments on commit 55adb8e

Please sign in to comment.