File tree Expand file tree Collapse file tree 3 files changed +22
-14
lines changed
Expand file tree Collapse file tree 3 files changed +22
-14
lines changed Original file line number Diff line number Diff line change @@ -118,6 +118,12 @@ class Health(enum.Enum):
118118 CRITICAL = 3
119119
120120
121+ class HealthReportingMixin :
122+ @abstractmethod
123+ async def check_health (self ) -> tuple [Health , str ]:
124+ pass
125+
126+
121127class ManagedService (ABC ):
122128 @abstractmethod
123129 async def on_start (self ):
@@ -128,9 +134,6 @@ async def on_start(self):
128134 """
129135 pass
130136
131- async def check_health (self ) -> tuple [Health , str ]:
132- return Health .UNKNOWN , "Not implemented"
133-
134137 @abstractmethod
135138 async def on_shutdown (self ):
136139 """Called during shutdown to perform cleanup tasks.
@@ -142,7 +145,7 @@ async def on_shutdown(self):
142145 pass
143146
144147
145- class BackgroundService (ManagedService ):
148+ class BackgroundService (ManagedService , HealthReportingMixin ):
146149 """Base class for long running background task wrapped in a managed
147150 service.
148151
Original file line number Diff line number Diff line change 2020
2121
2222from bbblb import migrations
23- from bbblb .services import Health , ManagedService
23+ from bbblb .services import Health , HealthReportingMixin , ManagedService
2424
2525LOG = logging .getLogger (__name__ )
2626
2727
28- class DBContext (ManagedService ):
28+ class DBContext (ManagedService , HealthReportingMixin ):
2929 engine : AsyncEngine | None = None
3030 sessionmaker : async_sessionmaker [AsyncSession ] | None = None
3131
Original file line number Diff line number Diff line change 11import asyncio
22import logging
3- from bbblb .services import BackgroundService , Health , ManagedService , ServiceRegistry
3+ from bbblb .services import (
4+ BackgroundService ,
5+ Health ,
6+ HealthReportingMixin ,
7+ ServiceRegistry ,
8+ )
49
510LOG = logging .getLogger (__name__ )
611
@@ -21,17 +26,17 @@ async def run(self):
2126
2227 for name in self .sr .started :
2328 obj = self .sr .get (name )
24- if not isinstance (obj , ManagedService ):
29+ if not isinstance (obj , HealthReportingMixin ):
2530 continue
26- status , msg = await obj .check_health ()
31+ try :
32+ status , msg = await obj .check_health ()
33+ except Exception as exc :
34+ status = Health .CRITICAL
35+ msg = f"Internal error in health check: { exc } "
2736 self .checks [name ] = (status , msg )
28- LOG .debug (f"Check: { name } { status .name } ( { msg } ) " )
37+ LOG .debug (f"[ { name } ] { status .name } { msg } " )
2938 except asyncio .CancelledError :
3039 self .checks .clear ()
3140 raise
3241 except BaseException :
3342 continue
34-
35- async def check_health (self ) -> tuple [Health , str ]:
36- # TODO
37- return await super ().check_health ()
You can’t perform that action at this time.
0 commit comments