diff --git a/app/system/impl/native_python.py b/app/system/impl/native_python.py index 5efec14..c1a025a 100644 --- a/app/system/impl/native_python.py +++ b/app/system/impl/native_python.py @@ -15,6 +15,7 @@ from app.system.models import ( ConnectionInfo, LoginInput, RawDebugLogData, + SystemHealthInfo, SystemInfo, ) @@ -51,6 +52,9 @@ class NativePythonSystem(SystemBase): chain=lninfo.chains[0].network, ) + async def get_system_health(self, verbose: bool) -> SystemHealthInfo: + return SystemHealthInfo(healthy=True) + async def shutdown(self, reboot: bool) -> bool: logging.info("Shutdown / reboot not supported in native_python mode.") return False diff --git a/app/system/impl/raspiblitz.py b/app/system/impl/raspiblitz.py index 67667af..91dcae0 100644 --- a/app/system/impl/raspiblitz.py +++ b/app/system/impl/raspiblitz.py @@ -25,6 +25,7 @@ from app.system.models import ( ConnectionInfo, LoginInput, RawDebugLogData, + SystemHealthInfo, SystemInfo, ) @@ -74,6 +75,9 @@ class RaspiBlitzSystem(SystemBase): chain=data_chain, ) + async def get_system_health(self, verbose: bool) -> SystemHealthInfo: + return SystemHealthInfo(healthy=True) + async def shutdown(self, reboot: bool) -> bool: params = "" if reboot: diff --git a/app/system/impl/system_base.py b/app/system/impl/system_base.py index 6684025..f13b3be 100644 --- a/app/system/impl/system_base.py +++ b/app/system/impl/system_base.py @@ -1,7 +1,13 @@ from abc import abstractmethod from typing import Dict -from app.system.models import ConnectionInfo, LoginInput, RawDebugLogData, SystemInfo +from app.system.models import ( + ConnectionInfo, + LoginInput, + RawDebugLogData, + SystemHealthInfo, + SystemInfo, +) class SystemBase: @@ -9,6 +15,10 @@ class SystemBase: async def get_system_info(self) -> SystemInfo: raise NotImplementedError() + @abstractmethod + async def get_system_health(self, verbose: bool) -> SystemHealthInfo: + raise NotImplementedError() + @abstractmethod async def shutdown(self, reboot: bool) -> bool: raise NotImplementedError() diff --git a/app/system/models.py b/app/system/models.py index 04a5f0a..b42a054 100644 --- a/app/system/models.py +++ b/app/system/models.py @@ -106,3 +106,19 @@ class ConnectionInfo(BaseModel): ) cl_rest_macaroon: str = Query("", description="core lightning rest macaroon") cl_rest_onion: str = Query("", description="core lightning rest onion address") + + +class SubSystemHealthInfo(BaseModel): + name: str = Query(..., description="Name of the subsystem") + health: bool = Query(..., description="Whether this system is healthy or not") + message: str = Query( + "", description="Optional message describing the systems health" + ) + + +class SystemHealthInfo(BaseModel): + healthy: bool = Query(..., description="") + message: str = Query("", description="") + subsystems: list[SubSystemHealthInfo] = Query( + [], description="Health information of running subsystems" + ) diff --git a/app/system/router.py b/app/system/router.py index e4c021a..6e349b7 100644 --- a/app/system/router.py +++ b/app/system/router.py @@ -13,7 +13,13 @@ from app.system.docs import ( get_debug_logs_raw_summary, get_hw_info_json, ) -from app.system.models import ConnectionInfo, LoginInput, RawDebugLogData, SystemInfo +from app.system.models import ( + ConnectionInfo, + LoginInput, + RawDebugLogData, + SystemInfo, + SystemHealthInfo, +) from app.system.service import ( HW_INFO_YIELD_TIME, change_password, @@ -24,6 +30,7 @@ from app.system.service import ( login, shutdown, subscribe_hardware_info, + system_health, ) _PREFIX = "system" @@ -154,6 +161,21 @@ async def hw_info_sub(request: Request): return EventSourceResponse(subscribe_hardware_info(request)) +@router.get( + "/health", + name=f"{_PREFIX}.health", + summary="Returns info about the systems health", + dependencies=[Depends(JWTBearer())], +) +async def get_system_health( + verbose: bool = Query( + False, + description="Returns info about each subsytem running on this node if true. Currently not implemented.", + ), +) -> SystemHealthInfo: + return await system_health(verbose) + + @router.post( "/reboot", name=f"{_PREFIX}.reboot", diff --git a/app/system/service.py b/app/system/service.py index 3664ebf..3b7e48b 100644 --- a/app/system/service.py +++ b/app/system/service.py @@ -10,6 +10,7 @@ from app.system.models import ( ConnectionInfo, LoginInput, RawDebugLogData, + SystemHealthInfo, SystemInfo, ) @@ -46,6 +47,15 @@ async def get_system_info() -> SystemInfo: raise HTTPException(status.HTTP_501_NOT_IMPLEMENTED, detail=r.args[0]) +async def system_health(verbose: bool) -> SystemHealthInfo: + try: + return await system.get_system_health(verbose) + except HTTPException: + raise + except NotImplementedError as r: + raise HTTPException(status.HTTP_501_NOT_IMPLEMENTED, detail=r.args[0]) + + async def get_hardware_info() -> map: try: return await system.get_hardware_info()