Skip to content

Commit e8c04f9

Browse files
committed
Make health checks optional for ManagedService
1 parent dfd7b2a commit e8c04f9

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

bbblb/services/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff 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+
121127
class 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

bbblb/services/db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020

2121

2222
from bbblb import migrations
23-
from bbblb.services import Health, ManagedService
23+
from bbblb.services import Health, HealthReportingMixin, ManagedService
2424

2525
LOG = 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

bbblb/services/health.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import asyncio
22
import 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

510
LOG = 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()

0 commit comments

Comments
 (0)