annotate wiki/tasks.py @ 679:89b240fe9297

For Django 1.5.2: import json; django.utils.simplejson is deprecated.
author Brian Neal <bgneal@gmail.com>
date Thu, 15 Aug 2013 20:14:33 -0500
parents a4300639c6e7
children aeafbf3ecebf
rev   line source
bgneal@627 1 """
bgneal@627 2 Celery tasks for the wiki app.
bgneal@627 3
bgneal@627 4 """
bgneal@627 5 import datetime
bgneal@627 6 import logging
bgneal@627 7 import time
bgneal@627 8
bgneal@627 9 from celery.task import task
bgneal@627 10 from django.conf import settings
bgneal@627 11 import redis
bgneal@627 12
bgneal@627 13 from core.services import get_redis_connection
bgneal@627 14
bgneal@627 15
bgneal@627 16 logger = logging.getLogger(__name__)
bgneal@627 17
bgneal@627 18
bgneal@627 19 @task
bgneal@627 20 def expire_cookies():
bgneal@627 21 """
bgneal@627 22 Periodically run this task to remove expired cookies from the Redis set
bgneal@627 23 that is shared between this Django application & the MoinMoin wiki for
bgneal@627 24 authentication.
bgneal@627 25
bgneal@627 26 """
bgneal@627 27 now = datetime.datetime.utcnow()
bgneal@627 28 cutoff = now - datetime.timedelta(seconds=settings.WIKI_COOKIE_AGE)
bgneal@627 29 min_score = time.mktime(cutoff.utctimetuple())
bgneal@627 30
bgneal@627 31 conn = get_redis_connection()
bgneal@627 32
bgneal@627 33 set_name = settings.WIKI_REDIS_SET
bgneal@627 34 try:
bgneal@627 35 count = conn.zcard(set_name)
bgneal@627 36 except redis.RedisError:
bgneal@627 37 logger.error("Error getting zcard")
bgneal@627 38 return
bgneal@627 39
bgneal@627 40 try:
bgneal@627 41 removed = conn.zremrangebyscore(set_name, 0.0, min_score)
bgneal@627 42 except redis.RedisError:
bgneal@627 43 logger.error("Error removing by score")
bgneal@627 44 return
bgneal@627 45
bgneal@627 46 total = count - removed
bgneal@627 47 logger.info("Expire wiki cookies: removed %d, total is now %d",
bgneal@627 48 removed, total)