diff --git a/README.rst b/README.rst index 20f2475..0d4143a 100644 --- a/README.rst +++ b/README.rst @@ -4,6 +4,9 @@ Django Ping Django Ping is utility that provides a lightweight endpoint for availability and uptime monitoring services. It also provides hooks for testing stack components and reporting them via JSON. +Current Version: 0.3.0 +https://github.com/gpennington/django-ping + Installation ------------ @@ -101,6 +104,23 @@ Then, add that to the ``PING_CHECKS`` tuple to display:: 'foo', true } +You can send ``POST`` requests too. As an example:: + + def check_create_user(request): + from django.contrib.auth.models import User + try: + if request.method == 'POST': + username = request.GET.get('username') + u, created = User.objects.get_or_create(username=username) + if created: + return 'create_user', "User: %s has been created" % u.username + else: + return 'create_user', "User: %s already exists" % u.username + else: + return 'create_user', "User cannot be created with GET" + except: + return 'create_user', "User not created" + Included Status Checks ~~~~~~~~~~~~~~~~~~~~~~ @@ -116,6 +136,8 @@ live. **check_cache_get** - Attempts to retrieve a cached value using the current cache backend defined. +**check_celery** - Adds a task to the queue and checks for celery to complete it. + Authentication ~~~~~~~~~~~~~~ @@ -125,6 +147,17 @@ set ``PING_BASIC_AUTH`` to ``True`` in your Django settings. Provide in the request the username/password of a valid User. +Complete Settings List +~~~~~~~~~~~~~~~~~~~~~~~~~ + +Check ``ping.defaults`` for default values. + +PING_RESPONSE = "Some string" +PING_MIMETYPE = 'text/html' or valid content type +PING_DEFAULT_CHECKS = tuple of paths to check methods +PING_BASIC_AUTH = Boolean (default is False) +PING_CELERY_TIMEOUT = In seconds as integers (5 is default) + What's Next? ------------ diff --git a/ping/checks.py b/ping/checks.py index fd62e95..85e0f55 100644 --- a/ping/checks.py +++ b/ping/checks.py @@ -30,7 +30,10 @@ def checks(request): response_dict[key] = value return response_dict - + +#DEFAULT SYSTEM CHECKS + +#Database def check_database_sessions(request): from django.contrib.sessions.models import Session try: @@ -72,3 +75,34 @@ def check_cache_get(request): return 'cache_get', False +#User +def check_user_exists(request): + from django.contrib.auth.models import User + try: + username = request.GET.get('username') + u = User.objects.get(username=username) + return 'user_exists', True + except: + return 'user_exists', False + + +#Celery +def check_celery(request): + from datetime import datetime, timedelta + from time import sleep, time + from ping.tasks import sample_task + + now = time() + datetimenow = datetime.now() + expires = datetimenow + timedelta(seconds=getattr(settings, 'PING_CELERY_TIMEOUT', PING_CELERY_TIMEOUT)) + + try: + task = sample_task.apply_async(expires=expires) + while expires > datetime.now(): + if task.ready() and task.result == True: + finished = str(time() - now) + return 'celery', { 'success': True, 'time':finished } + sleep(0.25) + return 'celery', { 'success': False } + except Exception: + return 'celery', { 'success': False } diff --git a/ping/defaults.py b/ping/defaults.py index cb1f551..9f9a23f 100644 --- a/ping/defaults.py +++ b/ping/defaults.py @@ -6,4 +6,6 @@ 'ping.checks.check_database_sites', ) -PING_BASIC_AUTH = False \ No newline at end of file +PING_BASIC_AUTH = False + +PING_CELERY_TIMEOUT = 5 \ No newline at end of file diff --git a/ping/tasks.py b/ping/tasks.py new file mode 100644 index 0000000..ac2ce1a --- /dev/null +++ b/ping/tasks.py @@ -0,0 +1,5 @@ +from celery.task import task + +@task() +def sample_task(): + return True \ No newline at end of file diff --git a/ping/views.py b/ping/views.py index eeab942..82cacd4 100644 --- a/ping/views.py +++ b/ping/views.py @@ -2,11 +2,13 @@ from django.conf import settings from django.utils import simplejson from django.contrib.auth.decorators import login_required +from django.views.decorators.csrf import csrf_exempt from ping.defaults import * from ping.checks import checks from ping.decorators import http_basic_auth +@csrf_exempt @http_basic_auth def status(request): """ @@ -19,7 +21,7 @@ def status(request): if request.GET.get('checks') == 'true': response_dict = checks(request) response += "