-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchecks.py
80 lines (63 loc) · 2.27 KB
/
checks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import logging
from django.contrib.staticfiles.storage import staticfiles_storage
from django.core.cache import caches
from django.db import connections
from django.db.migrations.executor import MigrationExecutor
from . import HealthcheckFailure
# don't bother with typing on Python <3.5
try:
from typing import Optional
except ImportError:
pass
log = logging.getLogger(__name__)
def check_database(query="SELECT 1", database="default"):
# type: (str, str) -> None
"""
Run a SQL query against the specified database.
:param str query: SQL to execute
:param str database: Database alias to execute against
:return None:
"""
try:
with connections[database].cursor() as cursor:
cursor.execute(query)
except Exception:
log.exception("%s database connection failed", database)
raise HealthcheckFailure("database error")
def check_staticfile(filename):
# type: (str) -> None
"""
Verify a static file is reachable
:param str filename: static file to verify
:return None:
"""
if not staticfiles_storage.exists(filename):
log.error("Can't find %s in static files.", filename)
raise HealthcheckFailure("static files error")
def check_cache(key="django-alive", cache="default"):
# type: (str, str) -> None
"""
Fetch a cache key against the specified cache.
:param str key: Cache key to fetch (does not need to exist)
:param str cache: Cache alias to execute against
:return None:
"""
try:
caches[cache].get(key)
except Exception:
log.exception("%s cache connection failed", cache)
raise HealthcheckFailure("cache error")
def check_migrations(alias=None):
# type: (Optional[str]) -> None
"""
Verify all defined migrations have been applied
:param str alias: An optional database alias (default: check all defined databases)
"""
for db_conn in connections.all():
if alias and db_conn.alias != alias:
continue
executor = MigrationExecutor(db_conn)
plan = executor.migration_plan(executor.loader.graph.leaf_nodes())
if plan:
log.error("Migrations pending on '%s' database", db_conn.alias)
raise HealthcheckFailure("database migrations pending")