-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0e2c4c
commit 2b1a92b
Showing
3 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import django | ||
|
||
if django.VERSION < (3, 2): | ||
default_app_config = "health_check.contrib.db_heartbeat.apps.HealthCheckConfig" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.apps import AppConfig | ||
from health_check.plugins import plugin_dir | ||
|
||
|
||
class HealthCheckConfig(AppConfig): | ||
name = 'health_check.contrib.db_heartbeat' | ||
|
||
def ready(self): | ||
from .backends import DatabaseHeartBeatCheck | ||
plugin_dir.register(DatabaseHeartBeatCheck) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from health_check.backends import BaseHealthCheckBackend | ||
from health_check.exceptions import ServiceUnavailable, ServiceReturnedUnexpectedResult | ||
from django.db import connection | ||
|
||
|
||
class DatabaseHeartBeatCheck(BaseHealthCheckBackend): | ||
""" | ||
Health check that runs a simple SELECT 1; query to test if the database connection is alive. | ||
""" | ||
|
||
def check_status(self): | ||
try: | ||
result = None | ||
with connection.cursor() as cursor: | ||
cursor.execute("SELECT 1;") | ||
result = cursor.fetchone() | ||
|
||
if result != (1,): | ||
raise ServiceReturnedUnexpectedResult("Health Check query did not return the expected result.") | ||
except Exception as e: | ||
raise ServiceUnavailable(f"Database health check failed: {e}") |