Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Move the health check's database probe into a service

Juan Denisbf5405df9eeb
diff --git a/backend/app/api/system.py b/backend/app/api/system.py
index d9e78620..197a9747 100644
--- a/backend/app/api/system.py
+++ b/backend/app/api/system.py
@@ -277,19 +277,14 @@ def health_check():
The domain and encryption details are for signed-in callers only: they
describe the deployment to anyone who asks.
"""
- from sqlalchemy import text
from flask_jwt_extended import verify_jwt_in_request
- from app import db
# Staging instances run with SERVERKIT_STAGING set. Echo it here so the
# verify step can prove WHICH instance answered and the UI can show its
# STAGING banner.
body = {'service': 'serverkit-api', 'staging': env_bool('SERVERKIT_STAGING')}
- try:
- db.session.execute(text('SELECT 1'))
- except Exception:
- db.session.rollback()
- current_app.logger.exception('Health check: database unreachable')
+ if not SystemService.database_reachable():
+ current_app.logger.error('Health check: database unreachable')
return jsonify({**body, 'status': 'unhealthy', 'database': 'unreachable'}), 503
try:
diff --git a/backend/app/services/system_service.py b/backend/app/services/system_service.py
index 2d73b479..e0ce18d8 100644
--- a/backend/app/services/system_service.py
+++ b/backend/app/services/system_service.py
@@ -11,6 +11,18 @@ from app.utils.system import run_checked, run_privileged
class SystemService:
"""Service for collecting system metrics and information."""
+ @staticmethod
+ def database_reachable():
+ """True when the panel database answers ``SELECT 1``."""
+ from sqlalchemy import text
+ from app import db
+ try:
+ db.session.execute(text('SELECT 1'))
+ return True
+ except Exception:
+ db.session.rollback()
+ return False
+
@staticmethod
def get_size(bytes_val):
"""Convert bytes to human readable format."""