Merge pull request #13675 from edx/andy/move-service-status
Move service_status from common into openedx
This commit is contained in:
3
openedx/core/djangoapps/service_status/__init__.py
Normal file
3
openedx/core/djangoapps/service_status/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
Stub for a Django app to report the status of various services
|
||||
"""
|
||||
25
openedx/core/djangoapps/service_status/tasks.py
Normal file
25
openedx/core/djangoapps/service_status/tasks.py
Normal file
@@ -0,0 +1,25 @@
|
||||
"""
|
||||
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
|
||||
48
openedx/core/djangoapps/service_status/test.py
Normal file
48
openedx/core/djangoapps/service_status/test.py
Normal file
@@ -0,0 +1,48 @@
|
||||
"""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)
|
||||
15
openedx/core/djangoapps/service_status/urls.py
Normal file
15
openedx/core/djangoapps/service_status/urls.py
Normal file
@@ -0,0 +1,15 @@
|
||||
"""
|
||||
Django URLs for service status app
|
||||
"""
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
|
||||
|
||||
urlpatterns = patterns(
|
||||
'',
|
||||
url(r'^$', 'openedx.core.djangoapps.service_status.views.index', name='status.service.index'),
|
||||
url(r'^celery/$', 'openedx.core.djangoapps.service_status.views.celery_status',
|
||||
name='status.service.celery.status'),
|
||||
url(r'^celery/ping/$', 'openedx.core.djangoapps.service_status.views.celery_ping',
|
||||
name='status.service.celery.ping'),
|
||||
)
|
||||
60
openedx/core/djangoapps/service_status/views.py
Normal file
60
openedx/core/djangoapps/service_status/views.py
Normal file
@@ -0,0 +1,60 @@
|
||||
"""
|
||||
Django Views for service status app
|
||||
"""
|
||||
|
||||
import json
|
||||
import time
|
||||
|
||||
from django.http import HttpResponse
|
||||
|
||||
from dogapi import dog_stats_api
|
||||
|
||||
from djcelery import celery
|
||||
from celery.exceptions import TimeoutError
|
||||
|
||||
from openedx.core.djangoapps.service_status.tasks import delayed_ping
|
||||
|
||||
|
||||
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 = 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