Commit 882eab4b by Adam Palay Committed by alawibaba

cache site status message for 5 minutes (TNL-1614)

parent a5a303ae
...@@ -3,6 +3,7 @@ A tiny app that checks for a status message. ...@@ -3,6 +3,7 @@ A tiny app that checks for a status message.
""" """
from django.conf import settings from django.conf import settings
from django.core.cache import cache
import json import json
import logging import logging
import os import os
...@@ -25,6 +26,11 @@ def get_site_status_msg(course_id): ...@@ -25,6 +26,11 @@ def get_site_status_msg(course_id):
not allowed to break the entire site). not allowed to break the entire site).
""" """
try: try:
# first check for msg in cache
msg = cache.get('site_status_msg')
if msg is not None:
return msg
if os.path.isfile(settings.STATUS_MESSAGE_PATH): if os.path.isfile(settings.STATUS_MESSAGE_PATH):
with open(settings.STATUS_MESSAGE_PATH) as f: with open(settings.STATUS_MESSAGE_PATH) as f:
content = f.read() content = f.read()
...@@ -37,6 +43,8 @@ def get_site_status_msg(course_id): ...@@ -37,6 +43,8 @@ def get_site_status_msg(course_id):
msg = msg + "<br>" if msg else '' msg = msg + "<br>" if msg else ''
msg += status_dict[course_id] msg += status_dict[course_id]
# set msg to cache, with expiry 5 mins
cache.set('site_status_msg', msg, 60 * 5)
return msg return msg
except: except:
log.exception("Error while getting a status message.") log.exception("Error while getting a status message.")
......
from django.conf import settings from django.conf import settings
from django.core.cache import cache
from django.test import TestCase from django.test import TestCase
import os import os
from django.test.utils import override_settings from django.test.utils import override_settings
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
import ddt
from .status import get_site_status_msg from .status import get_site_status_msg
...@@ -13,6 +15,7 @@ TMP_NAME = TMP_FILE.name ...@@ -13,6 +15,7 @@ TMP_NAME = TMP_FILE.name
TMP_FILE.close() TMP_FILE.close()
@ddt.ddt
@override_settings(STATUS_MESSAGE_PATH=TMP_NAME) @override_settings(STATUS_MESSAGE_PATH=TMP_NAME)
class TestStatus(TestCase): class TestStatus(TestCase):
"""Test that the get_site_status_msg function does the right thing""" """Test that the get_site_status_msg function does the right thing"""
...@@ -64,6 +67,13 @@ class TestStatus(TestCase): ...@@ -64,6 +67,13 @@ class TestStatus(TestCase):
with open(settings.STATUS_MESSAGE_PATH, 'w') as f: with open(settings.STATUS_MESSAGE_PATH, 'w') as f:
f.write(contents) f.write(contents)
def clear_status_cache(self):
"""
Remove the cached status message, if found
"""
if cache.get('site_status_msg') is not None:
cache.delete('site_status_msg')
def remove_status_file(self): def remove_status_file(self):
"""Delete the status file if it exists""" """Delete the status file if it exists"""
if os.path.exists(settings.STATUS_MESSAGE_PATH): if os.path.exists(settings.STATUS_MESSAGE_PATH):
...@@ -72,18 +82,19 @@ class TestStatus(TestCase): ...@@ -72,18 +82,19 @@ class TestStatus(TestCase):
def tearDown(self): def tearDown(self):
self.remove_status_file() self.remove_status_file()
def test_get_site_status_msg(self): @ddt.data(*checks)
@ddt.unpack
def test_get_site_status_msg(self, json_str, exp_none, exp_toy, exp_full):
"""run the tests""" """run the tests"""
for (json_str, exp_none, exp_toy, exp_full) in self.checks:
self.remove_status_file()
self.remove_status_file() if json_str:
if json_str: self.create_status_file(json_str)
self.create_status_file(json_str)
for course_id, expected_msg in [(None, exp_none), (self.toy_id, exp_toy), (self.full_id, exp_full)]:
print "checking results for {0}".format(json_str) self.assertEqual(get_site_status_msg(course_id), expected_msg)
print "course=None:" self.assertEqual(cache.get('site_status_msg'), expected_msg)
self.assertEqual(get_site_status_msg(None), exp_none) # check that `get_site_status_msg` works as expected when the cache
print "course=toy:" # is warmed, too
self.assertEqual(get_site_status_msg(self.toy_id), exp_toy) self.assertEqual(get_site_status_msg(course_id), expected_msg)
print "course=full:" self.clear_status_cache()
self.assertEqual(get_site_status_msg(self.full_id), exp_full)
...@@ -17,7 +17,7 @@ from status.status import get_site_status_msg ...@@ -17,7 +17,7 @@ from status.status import get_site_status_msg
## Provide a hook for themes to inject branding on top. ## Provide a hook for themes to inject branding on top.
<%block name="navigation_top" /> <%block name="navigation_top" />
<%block cached="False"> <%block>
<% <%
try: try:
course_id = course.id.to_deprecated_string() course_id = course.id.to_deprecated_string()
......
...@@ -17,7 +17,7 @@ from status.status import get_site_status_msg ...@@ -17,7 +17,7 @@ from status.status import get_site_status_msg
## Provide a hook for themes to inject branding on top. ## Provide a hook for themes to inject branding on top.
<%block name="navigation_top" /> <%block name="navigation_top" />
<%block cached="False"> <%block>
<% <%
try: try:
course_id = course.id.to_deprecated_string() course_id = course.id.to_deprecated_string()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment