Commit 11d4a7d4 by Steve Komarov

added celery documentation, switched to optional_parameter_call, minor fixes

parent 112d49d3
...@@ -80,6 +80,22 @@ the test module will be at: ...@@ -80,6 +80,22 @@ the test module will be at:
http://127.0.0.1:8000/static/index.html http://127.0.0.1:8000/static/index.html
Running periodic tasks
-------------------------------------
Periodic tasks (which are scheduled with core.decorators.cron)
rely on Celery for execution. It is the reponsability of the
client django project to ensure Celery is configured and running.
To configure, add the following to settings.py of your django
project:
from edinsights.celerysettings import *
To start celery, run from your django project
python manage.py celery worker -B
Only tasks located in files named "tasks.py" located in the main
directory of your django project or installed django app will
be scheduled.
Building on top of the framework Building on top of the framework
-------------------------------- --------------------------------
......
...@@ -21,6 +21,8 @@ from django.core.cache import cache ...@@ -21,6 +21,8 @@ from django.core.cache import cache
from django.conf import settings from django.conf import settings
from celery.task import PeriodicTask, periodic_task from celery.task import PeriodicTask, periodic_task
from util import optional_parameter_call
from util import default_optional_kwargs
import registry import registry
from registry import event_handlers, request_handlers from registry import event_handlers, request_handlers
...@@ -124,7 +126,7 @@ def memoize_query(cache_time = 60*4, timeout = 60*15, ignores = ["<class 'pymong ...@@ -124,7 +126,7 @@ def memoize_query(cache_time = 60*4, timeout = 60*15, ignores = ["<class 'pymong
# process of computing it # process of computing it
cached = cache.get(key) cached = cache.get(key)
if cached: if cached:
print "Cache hit", f.__name__, key # print "Cache hit", f.__name__, key
# If we're already computing it, wait to finish # If we're already computing it, wait to finish
# computation # computation
while cached == 'Processing': while cached == 'Processing':
...@@ -136,7 +138,7 @@ def memoize_query(cache_time = 60*4, timeout = 60*15, ignores = ["<class 'pymong ...@@ -136,7 +138,7 @@ def memoize_query(cache_time = 60*4, timeout = 60*15, ignores = ["<class 'pymong
results = cached results = cached
if not cached: if not cached:
print "Cache miss",f.__name__, key # print "Cache miss",f.__name__, key
# HACK: There's a slight race condition here, where we # HACK: There's a slight race condition here, where we
# might recompute twice. # might recompute twice.
cache.set(key, 'Processing', timeout) cache.set(key, 'Processing', timeout)
...@@ -162,14 +164,11 @@ def cron(run_every, params=None): ...@@ -162,14 +164,11 @@ def cron(run_every, params=None):
python manage.py celery worker -B --loglevel=INFO python manage.py celery worker -B --loglevel=INFO
Celery beat will automatically add tasks from files named 'tasks.py' Celery beat will automatically add tasks from files named 'tasks.py'
''' '''
def factory(f): def factory(func):
@periodic_task(run_every=run_every, name=f.__name__) @periodic_task(run_every=run_every, name=func.__name__)
def run(): def run():
from edinsights import core optional_parameter_call(func, default_optional_kwargs, params)
mongodb = core.util.get_mongo(f) return decorator(run,func)
fs = core.util.get_filesystem(f)
f(fs, mongodb, params)
return decorator(run,f)
return factory return factory
def event_property(name=None, description=None): def event_property(name=None, description=None):
......
...@@ -14,7 +14,7 @@ from celery.task import periodic_task ...@@ -14,7 +14,7 @@ from celery.task import periodic_task
@cron(run_every=timedelta(seconds=1)) @cron(run_every=timedelta(seconds=1))
def test_cron_task(*args): def test_cron_task():
""" Simple task that gets executed by the scheduler (celery beat). """ Simple task that gets executed by the scheduler (celery beat).
The test case test_cron verifies that the execution The test case test_cron verifies that the execution
has taken place. has taken place.
...@@ -28,7 +28,7 @@ def test_cron_task(*args): ...@@ -28,7 +28,7 @@ def test_cron_task(*args):
@cron(run_every=timedelta(seconds=1)) # cron decorators should go on top @cron(run_every=timedelta(seconds=1)) # cron decorators should go on top
@memoize_query(60) @memoize_query(60)
def test_cron_memoize_task(*args): def test_cron_memoize_task():
""" Simple task that gets executed by the scheduler (celery beat). """ Simple task that gets executed by the scheduler (celery beat).
The test case test_cron_and_memoize verifies that the execution The test case test_cron_and_memoize verifies that the execution
has taken place. has taken place.
......
...@@ -194,9 +194,12 @@ LOGGING = { ...@@ -194,9 +194,12 @@ LOGGING = {
} }
} }
##uncomment to provide full location of code that uses naive datetime # # By default timezone-related warnings do not display the location in code
#import warnings # # where they occurred. The code below will turn these warnings into
#warnings.filterwarnings( # # exceptions with stack trace so that one can identify the offending code.
# # Uncomment to turn timezone warnings into exceptions
# import warnings
# warnings.filterwarnings(
# 'error', r"DateTimeField received a naive datetime", # 'error', r"DateTimeField received a naive datetime",
# RuntimeWarning, r'django\.db\.models\.fields') # RuntimeWarning, r'django\.db\.models\.fields')
......
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