forked from revsys/django-health-check
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request revsys#2 from stefanfoulis/feature/storage-check
add health check for storage backends
- Loading branch information
Showing
5 changed files
with
66 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
Empty file.
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,55 @@ | ||
#-*- coding: utf-8 -*- | ||
from django.core.files.base import ContentFile | ||
from django.core.files.storage import get_storage_class | ||
from health_check.backends.base import BaseHealthCheckBackend, HealthCheckStatusType | ||
import random | ||
import datetime | ||
|
||
|
||
class StorageHealthCheck(BaseHealthCheckBackend): | ||
""" | ||
Tests the status of a StorageBakcend. Can be extended to test any storage backend by subclassing: | ||
class MyStorageHealthCheck(StorageHealthCheck): | ||
storage = 'some.other.StorageBackend' | ||
plugin_dir.register(MyStorageHealthCheck) | ||
storage must be either a string pointing to a storage class (e.g 'django.core.files.storage.FileSystemStorage') or | ||
a Storage instance. | ||
""" | ||
storage = None | ||
|
||
def get_storage(self): | ||
if isinstance(self.storage, basestring): | ||
return get_storage_class(self.storage)() | ||
else: | ||
return self.storage | ||
|
||
def get_file_name(self): | ||
return 'health_check-%s-%s/test.txt' % (datetime.datetime.now(), random.randint(10000,99999)) | ||
|
||
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): | ||
return HealthCheckStatusType.unavailable | ||
if not f.read() == file_content: | ||
return HealthCheckStatusType.unavailable | ||
# delete the file and make sure it is gone | ||
storage.delete(file_name) | ||
if storage.exists(file_name): | ||
return HealthCheckStatusType.unavailable | ||
return HealthCheckStatusType.working | ||
except Exception: | ||
return HealthCheckStatusType.unavailable |
Empty file.
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 @@ | ||
#-*- coding: utf-8 -*- | ||
from health_check.plugins import plugin_dir | ||
from health_check_storage.base import StorageHealthCheck | ||
from django.conf import settings | ||
|
||
|
||
class DefaultFileStorageHealthCheck(StorageHealthCheck): | ||
storage = settings.DEFAULT_FILE_STORAGE | ||
|
||
plugin_dir.register(DefaultFileStorageHealthCheck) |