|
1 | 1 | import json
|
2 | 2 | import logging
|
| 3 | +import os.path |
| 4 | +from os import getenv |
| 5 | +from time import time |
3 | 6 |
|
4 | 7 | from django.conf import settings
|
5 | 8 | from django.http import HttpResponse, HttpResponseBadRequest
|
| 9 | +from django.shortcuts import render |
6 | 10 | from django.views.decorators.cache import never_cache
|
7 | 11 | from django.views.decorators.csrf import csrf_exempt
|
8 | 12 | from django.views.decorators.http import require_POST, require_safe
|
@@ -56,6 +60,75 @@ def geolocate(request):
|
56 | 60 | })
|
57 | 61 |
|
58 | 62 |
|
| 63 | +# file names and max seconds since last run |
| 64 | +HEALTH_FILES = ( |
| 65 | + ('download_database', 600), |
| 66 | + ('update_locales', 600), |
| 67 | +) |
| 68 | +DB_INFO_FILE = getenv('AWS_DB_JSON_DATA_FILE', 'bedrock_db_info.json') |
| 69 | +GIT_SHA = getenv('GIT_SHA') |
| 70 | +BUCKET_NAME = getenv('AWS_DB_S3_BUCKET', 'bedrock-db-dev') |
| 71 | +REGION_NAME = os.getenv('AWS_DB_REGION', 'us-west-2') |
| 72 | +S3_BASE_URL = 'https://s3-{}.amazonaws.com/{}'.format( |
| 73 | + REGION_NAME, |
| 74 | + BUCKET_NAME, |
| 75 | +) |
| 76 | + |
| 77 | + |
| 78 | +def get_db_file_url(filename): |
| 79 | + return '/'.join([S3_BASE_URL, filename]) |
| 80 | + |
| 81 | + |
| 82 | +def get_extra_server_info(): |
| 83 | + server_name = [getattr(settings, x) for x in ['HOSTNAME', 'DEIS_APP', 'DEIS_DOMAIN']] |
| 84 | + server_name = '.'.join(x for x in server_name if x) |
| 85 | + server_info = { |
| 86 | + 'name': server_name, |
| 87 | + 'git_sha': GIT_SHA, |
| 88 | + } |
| 89 | + try: |
| 90 | + with open(DB_INFO_FILE, 'r') as fp: |
| 91 | + db_info = json.load(fp) |
| 92 | + except (IOError, ValueError): |
| 93 | + pass |
| 94 | + else: |
| 95 | + db_info['last_update'] = int((time() - db_info['updated']) / 60) |
| 96 | + db_info['file_url'] = get_db_file_url(db_info['file_name']) |
| 97 | + for key, value in db_info.items(): |
| 98 | + server_info['db_%s' % key] = value |
| 99 | + |
| 100 | + return server_info |
| 101 | + |
| 102 | + |
| 103 | +@require_safe |
| 104 | +@never_cache |
| 105 | +def cron_health_check(request): |
| 106 | + results = [] |
| 107 | + check_pass = True |
| 108 | + for fname, max_time in HEALTH_FILES: |
| 109 | + fpath = '/tmp/last-run-%s' % fname |
| 110 | + try: |
| 111 | + last_check = os.path.getmtime(fpath) |
| 112 | + except OSError: |
| 113 | + check_pass = False |
| 114 | + results.append((fname, max_time, 'None', False)) |
| 115 | + continue |
| 116 | + |
| 117 | + time_since = int(time() - last_check) |
| 118 | + if time_since > max_time: |
| 119 | + task_pass = False |
| 120 | + check_pass = False |
| 121 | + else: |
| 122 | + task_pass = True |
| 123 | + |
| 124 | + results.append((fname, max_time, time_since, task_pass)) |
| 125 | + |
| 126 | + server_info = get_extra_server_info() |
| 127 | + return render(request, 'cron-health-check.html', |
| 128 | + {'results': results, 'server_info': server_info, 'success': check_pass}, |
| 129 | + status=200 if check_pass else 500) |
| 130 | + |
| 131 | + |
59 | 132 | def server_error_view(request, template_name='500.html'):
|
60 | 133 | """500 error handler that runs context processors."""
|
61 | 134 | return l10n_utils.render(request, template_name, status=500)
|
|
0 commit comments