Move service_status from common into openedx
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
"""
|
||||
Stub for a Django app to report the status of various services
|
||||
"""
|
||||
@@ -1,25 +0,0 @@
|
||||
"""
|
||||
Django Celery tasks for service status app
|
||||
"""
|
||||
|
||||
import time
|
||||
|
||||
from dogapi import dog_stats_api
|
||||
|
||||
from djcelery import celery
|
||||
|
||||
|
||||
@celery.task
|
||||
@dog_stats_api.timed('status.service.celery.pong')
|
||||
def delayed_ping(value, delay):
|
||||
"""A simple tasks that replies to a message after a especified amount
|
||||
of seconds.
|
||||
"""
|
||||
if value == 'ping':
|
||||
result = 'pong'
|
||||
else:
|
||||
result = 'got: {0}'.format(value)
|
||||
|
||||
time.sleep(delay)
|
||||
|
||||
return result
|
||||
@@ -1,48 +0,0 @@
|
||||
"""Test for async task service status"""
|
||||
|
||||
from django.utils import unittest
|
||||
from django.test.client import Client
|
||||
from django.core.urlresolvers import reverse
|
||||
import json
|
||||
|
||||
|
||||
class CeleryConfigTest(unittest.TestCase):
|
||||
"""
|
||||
Test that we can get a response from Celery
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
"""
|
||||
Create a django test client
|
||||
"""
|
||||
super(CeleryConfigTest, self).setUp()
|
||||
self.client = Client()
|
||||
self.ping_url = reverse('status.service.celery.ping')
|
||||
|
||||
def test_ping(self):
|
||||
"""
|
||||
Try to ping celery.
|
||||
"""
|
||||
|
||||
# Access the service status page, which starts a delayed
|
||||
# asynchronous task
|
||||
response = self.client.get(self.ping_url)
|
||||
|
||||
# HTTP response should be successful
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
# Expect to get a JSON-serialized dict with
|
||||
# task and time information
|
||||
result_dict = json.loads(response.content)
|
||||
|
||||
# Was it successful?
|
||||
self.assertTrue(result_dict['success'])
|
||||
|
||||
# We should get a "pong" message back
|
||||
self.assertEqual(result_dict['value'], "pong")
|
||||
|
||||
# We don't know the other dict values exactly,
|
||||
# but we can assert that they take the right form
|
||||
self.assertIsInstance(result_dict['task_id'], unicode)
|
||||
self.assertIsInstance(result_dict['time'], float)
|
||||
self.assertGreater(result_dict['time'], 0.0)
|
||||
@@ -1,15 +0,0 @@
|
||||
"""
|
||||
Django URLs for service status app
|
||||
"""
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
|
||||
|
||||
urlpatterns = patterns(
|
||||
'',
|
||||
url(r'^$', 'service_status.views.index', name='status.service.index'),
|
||||
url(r'^celery/$', 'service_status.views.celery_status',
|
||||
name='status.service.celery.status'),
|
||||
url(r'^celery/ping/$', 'service_status.views.celery_ping',
|
||||
name='status.service.celery.ping'),
|
||||
)
|
||||
@@ -1,59 +0,0 @@
|
||||
"""
|
||||
Django Views for service status app
|
||||
"""
|
||||
|
||||
import json
|
||||
import time
|
||||
|
||||
from django.http import HttpResponse
|
||||
|
||||
from dogapi import dog_stats_api
|
||||
|
||||
from service_status import tasks
|
||||
from djcelery import celery
|
||||
from celery.exceptions import TimeoutError
|
||||
|
||||
|
||||
def index(_):
|
||||
"""
|
||||
An empty view
|
||||
"""
|
||||
return HttpResponse()
|
||||
|
||||
|
||||
@dog_stats_api.timed('status.service.celery.status')
|
||||
def celery_status(_):
|
||||
"""
|
||||
A view that returns Celery stats
|
||||
"""
|
||||
stats = celery.control.inspect().stats() or {}
|
||||
return HttpResponse(json.dumps(stats, indent=4),
|
||||
content_type="application/json")
|
||||
|
||||
|
||||
@dog_stats_api.timed('status.service.celery.ping')
|
||||
def celery_ping(_):
|
||||
"""
|
||||
A Simple view that checks if Celery can process a simple task
|
||||
"""
|
||||
start = time.time()
|
||||
result = tasks.delayed_ping.apply_async(('ping', 0.1))
|
||||
task_id = result.id
|
||||
|
||||
# Wait until we get the result
|
||||
try:
|
||||
value = result.get(timeout=4.0)
|
||||
success = True
|
||||
except TimeoutError:
|
||||
value = None
|
||||
success = False
|
||||
|
||||
output = {
|
||||
'success': success,
|
||||
'task_id': task_id,
|
||||
'value': value,
|
||||
'time': time.time() - start,
|
||||
}
|
||||
|
||||
return HttpResponse(json.dumps(output, indent=4),
|
||||
content_type="application/json")
|
||||
Reference in New Issue
Block a user