2
0
mirror of https://github.com/inventree/InvenTree.git synced 2026-07-04 14:10:52 +00:00

System Health Checks (#12193)

* Add worker health check invoke task

* Increase frequency of heartbeat task

* Adjust default threshold for worker health check

* Add server_health invoke func
This commit is contained in:
Oliver
2026-06-18 12:46:46 +10:00
committed by GitHub
parent c126e2b0af
commit 4b29032c6e
3 changed files with 82 additions and 13 deletions
+3 -6
View File
@@ -188,13 +188,10 @@ class InvenTreeConfig(AppConfig):
@ignore_ready_warning
def add_heartbeat(self):
"""Ensure there is at least one background task in the queue."""
import django_q.models
try:
if django_q.models.OrmQ.objects.count() == 0:
InvenTree.tasks.offload_task(
InvenTree.tasks.heartbeat, force_async=True, group='heartbeat'
)
InvenTree.tasks.offload_task(
InvenTree.tasks.heartbeat, force_async=True, group='heartbeat'
)
except AppRegistryNotReady: # pragma: no cover
pass
except Exception:
+16 -7
View File
@@ -438,21 +438,30 @@ def scheduled_task(
@tracer.start_as_current_span('heartbeat')
@scheduled_task(ScheduledTask.MINUTES, 5)
@scheduled_task(ScheduledTask.MINUTES, 1)
def heartbeat():
"""Simple task which runs at 5 minute intervals, so we can determine that the background worker is actually running.
(There is probably a less "hacky" way of achieving this)?
"""
"""Simple task which runs at 1 minute intervals, so we can determine that the background worker is actually running."""
try:
from django_q.models import OrmQ, Success
except AppRegistryNotReady: # pragma: no cover
logger.info('Could not perform heartbeat task - App registry not ready')
return
threshold = timezone.now() - timedelta(minutes=30)
# Write a timestamp file so that health checks can verify worker liveness
# without needing to start a full Django process.
import tempfile
from pathlib import Path
# Delete heartbeat results more than half an hour old,
try:
Path(tempfile.gettempdir()).joinpath('inventree_worker_heartbeat').write_text(
str(timezone.now().timestamp())
)
except Exception:
pass
threshold = timezone.now() - timedelta(minutes=15)
# Delete heartbeat results more than 15 minutes old,
# otherwise they just create extra noise
heartbeats = Success.objects.filter(
func='InvenTree.tasks.heartbeat', started__lte=threshold