Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
adding logging for status exceptions so we can debug
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisclark committed Jan 13, 2014
1 parent 02ea1b3 commit 47da617
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
47 changes: 24 additions & 23 deletions health_check/backends/base.py
@@ -1,8 +1,13 @@
from django.core.files.storage import get_storage_class
from django.core.files.base import ContentFile
import logging
import datetime
import random


logger = logging.getLogger('django')


class HealthCheckStatusType(object):
unavailable = 0
working = 1
Expand All @@ -20,12 +25,10 @@ class HealthCheckException(Exception):


class ServiceUnavailable(HealthCheckException):
message = HEALTH_CHECK_STATUS_TYPE_TRANSLATOR[0]
code = 0


class ServiceReturnedUnexpectedResult(HealthCheckException):
message = HEALTH_CHECK_STATUS_TYPE_TRANSLATOR[2]
code = 2


Expand All @@ -40,6 +43,7 @@ def status(self):
try:
setattr(self, "_status", self.check_status())
except (ServiceUnavailable, ServiceReturnedUnexpectedResult) as e:
logger.exception(e)
setattr(self, "_status", e.code)

return self._status
Expand Down Expand Up @@ -78,24 +82,21 @@ def get_file_content(self):
return 'this is the healthtest file content'

def check_status(self):
try:
# write the file to the storage backend
storage = self.get_storage()
file_name = self.get_file_name()
file_content = self.get_file_content()

# save the file
file_name = storage.save(file_name, ContentFile(content=file_content))
# read the file and compare
f = storage.open(file_name)
if not storage.exists(file_name):
raise ServiceUnavailable("File does not exist")
if not f.read() == file_content:
raise ServiceUnavailable("File content doesn't match")
# delete the file and make sure it is gone
storage.delete(file_name)
if storage.exists(file_name):
raise ServiceUnavailable("File was not deleted")
return True
except Exception, e:
raise ServiceUnavailable("Unknown exception")
# write the file to the storage backend
storage = self.get_storage()
file_name = self.get_file_name()
file_content = self.get_file_content()

# save the file
file_name = storage.save(file_name, ContentFile(content=file_content))
# read the file and compare
f = storage.open(file_name)
if not storage.exists(file_name):
raise ServiceUnavailable("File does not exist")
if not f.read() == file_content:
raise ServiceUnavailable("File content doesn't match")
# delete the file and make sure it is gone
storage.delete(file_name)
if storage.exists(file_name):
raise ServiceUnavailable("File was not deleted")
return True
4 changes: 4 additions & 0 deletions health_check/health_checks.py
Expand Up @@ -6,13 +6,17 @@
from datetime import datetime, timedelta
from time import sleep
import random
import logging

from django.core.cache.backends.base import CacheKeyWarning
from django.core.cache import cache
from django.db import DatabaseError, IntegrityError
from django.conf import settings





class CacheBackendCheck(BaseHealthCheckBackend):

def check_status(self):
Expand Down

0 comments on commit 47da617

Please sign in to comment.