comparison wiki/tasks.py @ 627:a4300639c6e7

Wiki integration. Create task to delete old cookie records. Rework logic upon logout, as session will not be available. Set an attribute on the request instead.
author Brian Neal <bgneal@gmail.com>
date Mon, 12 Nov 2012 15:10:52 -0600
parents
children aeafbf3ecebf
comparison
equal deleted inserted replaced
626:a6bc1e2efa63 627:a4300639c6e7
1 """
2 Celery tasks for the wiki app.
3
4 """
5 import datetime
6 import logging
7 import time
8
9 from celery.task import task
10 from django.conf import settings
11 import redis
12
13 from core.services import get_redis_connection
14
15
16 logger = logging.getLogger(__name__)
17
18
19 @task
20 def expire_cookies():
21 """
22 Periodically run this task to remove expired cookies from the Redis set
23 that is shared between this Django application & the MoinMoin wiki for
24 authentication.
25
26 """
27 now = datetime.datetime.utcnow()
28 cutoff = now - datetime.timedelta(seconds=settings.WIKI_COOKIE_AGE)
29 min_score = time.mktime(cutoff.utctimetuple())
30
31 conn = get_redis_connection()
32
33 set_name = settings.WIKI_REDIS_SET
34 try:
35 count = conn.zcard(set_name)
36 except redis.RedisError:
37 logger.error("Error getting zcard")
38 return
39
40 try:
41 removed = conn.zremrangebyscore(set_name, 0.0, min_score)
42 except redis.RedisError:
43 logger.error("Error removing by score")
44 return
45
46 total = count - removed
47 logger.info("Expire wiki cookies: removed %d, total is now %d",
48 removed, total)