Commit bee0e50f by Steve Komarov

added not NotInCacheError

parent 776748b3
......@@ -101,7 +101,8 @@ def query(category = None, name = None, description = None, args = None):
return query_factory
class NotInCacheError(Exception):
pass
def mq_force_memoize(func):
"""
......@@ -232,7 +233,7 @@ def memoize_query(cache_time = 60*4, timeout = 60*15, ignores = ["<class 'pymong
# print "Forcing retrieve %s %s " % (f.__name__, key)
results = get_from_cache_if_possible(f, key)
if not results:
raise KeyError('key %s not found in cache' % key) # TODO better exception class?
raise NotInCacheError('key %s not found in cache' % key)
return results
decfun = decorator(opmode_default,f)
......
# This module provides tests for periodic tasks using core.decorators.cron
from edinsights.core.decorators import view, mq_force_retrieve
from edinsights.core.decorators import view, mq_force_retrieve, NotInCacheError
from edinsights.periodic.tasks import big_computation, big_computation_withfm
@view()
......@@ -10,4 +10,12 @@ def big_computation_visualizer():
@view()
def big_computation_visualizer_withfm():
return "<html>%s</html>" % mq_force_retrieve(big_computation_withfm)()
try:
# returns instantly, does not perform computation if results are not
# in cache
result = mq_force_retrieve(big_computation_withfm)()
except NotInCacheError:
result = "The big computation has not been performed yet"
# alternatively you can display a "please wait" message
# and run big_computation_withfm() without force_retrieve
return "<html>%s</html>" % result
......@@ -56,7 +56,7 @@ class SimpleTest(TestCase):
"""
truncate_tempfile('test_cron_task_counter')
run_celery_beat(seconds=3,verbose=True)
run_celery_beat(seconds=3,verbose=False)
# verify number of calls and time of last call
ncalls, last_call = count_timestamps('test_cron_task_counter')
......@@ -74,7 +74,7 @@ class SimpleTest(TestCase):
# clear the cache from any previous executions of this test
cache.delete('test_cron_memoize_unique_cache_key')
run_celery_beat(seconds=3,verbose=True)
run_celery_beat(seconds=3,verbose=False)
ncalls, last_call = count_timestamps('test_cron_memoize_task')
self.assertEqual(ncalls,1) # after the first call all subsequent calls should be cached
self.assertAlmostEqual(last_call, time.time(), delta=100)
......@@ -91,7 +91,7 @@ class SimpleTest(TestCase):
# delete cache from previous executions of this unit test
cache.delete('big_computation_key')
run_celery_beat(seconds=3, verbose=True)
run_celery_beat(seconds=3, verbose=False)
ncalls_before, lastcall_before = count_timestamps('big_computation_counter')
self.assertEqual(ncalls_before,1) # after the first call all subsequent calls should be cached
......@@ -120,7 +120,7 @@ class SimpleTest(TestCase):
truncate_tempfile('big_computation_withfm_counter')
cache.delete('big_computation_key_withfm')
run_celery_beat(seconds=3, verbose=True)
run_celery_beat(seconds=3, verbose=False)
ncalls_before, lastcall_before = count_timestamps('big_computation_withfm_counter')
self.assertGreaterEqual(ncalls_before,2)
......
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