Mercurial > public > sg101
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wiki/tasks.py Mon Nov 12 15:10:52 2012 -0600 @@ -0,0 +1,48 @@ +""" +Celery tasks for the wiki app. + +""" +import datetime +import logging +import time + +from celery.task import task +from django.conf import settings +import redis + +from core.services import get_redis_connection + + +logger = logging.getLogger(__name__) + + +@task +def expire_cookies(): + """ + Periodically run this task to remove expired cookies from the Redis set + that is shared between this Django application & the MoinMoin wiki for + authentication. + + """ + now = datetime.datetime.utcnow() + cutoff = now - datetime.timedelta(seconds=settings.WIKI_COOKIE_AGE) + min_score = time.mktime(cutoff.utctimetuple()) + + conn = get_redis_connection() + + set_name = settings.WIKI_REDIS_SET + try: + count = conn.zcard(set_name) + except redis.RedisError: + logger.error("Error getting zcard") + return + + try: + removed = conn.zremrangebyscore(set_name, 0.0, min_score) + except redis.RedisError: + logger.error("Error removing by score") + return + + total = count - removed + logger.info("Expire wiki cookies: removed %d, total is now %d", + removed, total)