Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 0 additions & 116 deletions hathor/healthcheck/models.py

This file was deleted.

32 changes: 15 additions & 17 deletions hathor/healthcheck/resources/healthcheck.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import hathor
import asyncio

from healthcheck import Healthcheck, HealthcheckCallbackResponse, HealthcheckInternalComponent, HealthcheckStatus

from hathor.api_util import Resource, get_arg_default, get_args
from hathor.cli.openapi_files.register import register_resource
from hathor.healthcheck.models import ComponentHealthCheck, ComponentType, HealthCheckStatus, ServiceHealthCheck
from hathor.manager import HathorManager
from hathor.util import json_dumpb


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

return ComponentHealthCheck(
component_name='sync',
component_type=ComponentType.INTERNAL,
status=HealthCheckStatus.PASS if healthy else HealthCheckStatus.FAIL,
return HealthcheckCallbackResponse(
status=HealthcheckStatus.PASS if healthy else HealthcheckStatus.FAIL,
output=reason or 'Healthy',
)

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

components_health_checks = [
build_sync_health_status(self.manager)
]

health_check = ServiceHealthCheck(
description=f'Hathor-core {hathor.__version__}',
checks={c.component_name: [c] for c in components_health_checks},
sync_component = HealthcheckInternalComponent(
name='sync',
)
sync_component.add_healthcheck(lambda: sync_healthcheck(self.manager))

healthcheck = Healthcheck(name='hathor-core', components=[sync_component])
status = asyncio.get_event_loop().run_until_complete(healthcheck.run())

if strict_status_code:
request.setResponseCode(200)
else:
status_code = health_check.get_http_status_code()
status_code = status.get_http_status_code()
request.setResponseCode(status_code)

return json_dumpb(health_check.to_json())
return json_dumpb(status.to_json())


HealthcheckResource.openapi = {
Expand Down
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ hathorlib = "0.3.0"
pydantic = "~1.10.13"
pyyaml = "^6.0.1"
typing-extensions = "~4.8.0"
python-healthchecklib = "^0.1.0"

[tool.poetry.extras]
sentry = ["sentry-sdk", "structlog-sentry"]
Expand Down
5 changes: 5 additions & 0 deletions tests/resources/healthcheck/test_healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def test_get_no_recent_activity(self):
'checks': {
'sync': [{
'componentType': 'internal',
'componentName': 'sync',
'status': 'fail',
'output': HathorManager.UnhealthinessReason.NO_RECENT_ACTIVITY,
'time': ANY
Expand All @@ -53,6 +54,7 @@ def test_strict_status_code(self):
'checks': {
'sync': [{
'componentType': 'internal',
'componentName': 'sync',
'status': 'fail',
'output': HathorManager.UnhealthinessReason.NO_RECENT_ACTIVITY,
'time': ANY
Expand All @@ -79,6 +81,7 @@ def test_get_no_connected_peer(self):
'checks': {
'sync': [{
'componentType': 'internal',
'componentName': 'sync',
'status': 'fail',
'output': HathorManager.UnhealthinessReason.NO_SYNCED_PEER,
'time': ANY
Expand Down Expand Up @@ -111,6 +114,7 @@ def test_get_peer_out_of_sync(self):
'checks': {
'sync': [{
'componentType': 'internal',
'componentName': 'sync',
'status': 'fail',
'output': HathorManager.UnhealthinessReason.NO_SYNCED_PEER,
'time': ANY
Expand Down Expand Up @@ -143,6 +147,7 @@ def test_get_ready(self):
'checks': {
'sync': [{
'componentType': 'internal',
'componentName': 'sync',
'status': 'pass',
'output': 'Healthy',
'time': ANY
Expand Down