Skip to content

Commit 5526455

Browse files
authored
chore: refactor python healthcheck lib (#826)
1 parent ca1d829 commit 5526455

File tree

5 files changed

+33
-134
lines changed

5 files changed

+33
-134
lines changed

hathor/healthcheck/models.py

Lines changed: 0 additions & 116 deletions
This file was deleted.

hathor/healthcheck/resources/healthcheck.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
import hathor
1+
import asyncio
2+
3+
from healthcheck import Healthcheck, HealthcheckCallbackResponse, HealthcheckInternalComponent, HealthcheckStatus
4+
25
from hathor.api_util import Resource, get_arg_default, get_args
36
from hathor.cli.openapi_files.register import register_resource
4-
from hathor.healthcheck.models import ComponentHealthCheck, ComponentType, HealthCheckStatus, ServiceHealthCheck
57
from hathor.manager import HathorManager
68
from hathor.util import json_dumpb
79

810

9-
def build_sync_health_status(manager: HathorManager) -> ComponentHealthCheck:
10-
"""Builds the sync health status object."""
11+
async def sync_healthcheck(manager: HathorManager) -> HealthcheckCallbackResponse:
1112
healthy, reason = manager.is_sync_healthy()
1213

13-
return ComponentHealthCheck(
14-
component_name='sync',
15-
component_type=ComponentType.INTERNAL,
16-
status=HealthCheckStatus.PASS if healthy else HealthCheckStatus.FAIL,
14+
return HealthcheckCallbackResponse(
15+
status=HealthcheckStatus.PASS if healthy else HealthcheckStatus.FAIL,
1716
output=reason or 'Healthy',
1817
)
1918

@@ -38,22 +37,21 @@ def render_GET(self, request):
3837
raw_args = get_args(request)
3938
strict_status_code = get_arg_default(raw_args, 'strict_status_code', '0') == '1'
4039

41-
components_health_checks = [
42-
build_sync_health_status(self.manager)
43-
]
44-
45-
health_check = ServiceHealthCheck(
46-
description=f'Hathor-core {hathor.__version__}',
47-
checks={c.component_name: [c] for c in components_health_checks},
40+
sync_component = HealthcheckInternalComponent(
41+
name='sync',
4842
)
43+
sync_component.add_healthcheck(lambda: sync_healthcheck(self.manager))
44+
45+
healthcheck = Healthcheck(name='hathor-core', components=[sync_component])
46+
status = asyncio.get_event_loop().run_until_complete(healthcheck.run())
4947

5048
if strict_status_code:
5149
request.setResponseCode(200)
5250
else:
53-
status_code = health_check.get_http_status_code()
51+
status_code = status.get_http_status_code()
5452
request.setResponseCode(status_code)
5553

56-
return json_dumpb(health_check.to_json())
54+
return json_dumpb(status.to_json())
5755

5856

5957
HealthcheckResource.openapi = {

poetry.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ hathorlib = "0.3.0"
7979
pydantic = "~1.10.13"
8080
pyyaml = "^6.0.1"
8181
typing-extensions = "~4.8.0"
82+
python-healthchecklib = "^0.1.0"
8283

8384
[tool.poetry.extras]
8485
sentry = ["sentry-sdk", "structlog-sentry"]

tests/resources/healthcheck/test_healthcheck.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def test_get_no_recent_activity(self):
3131
'checks': {
3232
'sync': [{
3333
'componentType': 'internal',
34+
'componentName': 'sync',
3435
'status': 'fail',
3536
'output': HathorManager.UnhealthinessReason.NO_RECENT_ACTIVITY,
3637
'time': ANY
@@ -53,6 +54,7 @@ def test_strict_status_code(self):
5354
'checks': {
5455
'sync': [{
5556
'componentType': 'internal',
57+
'componentName': 'sync',
5658
'status': 'fail',
5759
'output': HathorManager.UnhealthinessReason.NO_RECENT_ACTIVITY,
5860
'time': ANY
@@ -79,6 +81,7 @@ def test_get_no_connected_peer(self):
7981
'checks': {
8082
'sync': [{
8183
'componentType': 'internal',
84+
'componentName': 'sync',
8285
'status': 'fail',
8386
'output': HathorManager.UnhealthinessReason.NO_SYNCED_PEER,
8487
'time': ANY
@@ -111,6 +114,7 @@ def test_get_peer_out_of_sync(self):
111114
'checks': {
112115
'sync': [{
113116
'componentType': 'internal',
117+
'componentName': 'sync',
114118
'status': 'fail',
115119
'output': HathorManager.UnhealthinessReason.NO_SYNCED_PEER,
116120
'time': ANY
@@ -143,6 +147,7 @@ def test_get_ready(self):
143147
'checks': {
144148
'sync': [{
145149
'componentType': 'internal',
150+
'componentName': 'sync',
146151
'status': 'pass',
147152
'output': 'Healthy',
148153
'time': ANY

0 commit comments

Comments
 (0)