bgneal@627: """ bgneal@627: Celery tasks for the wiki app. bgneal@627: bgneal@627: """ bgneal@627: import datetime bgneal@627: import logging bgneal@627: import time bgneal@627: bgneal@627: from celery.task import task bgneal@627: from django.conf import settings bgneal@627: import redis bgneal@627: bgneal@627: from core.services import get_redis_connection bgneal@627: bgneal@627: bgneal@627: logger = logging.getLogger(__name__) bgneal@627: bgneal@627: bgneal@627: @task bgneal@627: def expire_cookies(): bgneal@627: """ bgneal@627: Periodically run this task to remove expired cookies from the Redis set bgneal@627: that is shared between this Django application & the MoinMoin wiki for bgneal@627: authentication. bgneal@627: bgneal@627: """ bgneal@627: now = datetime.datetime.utcnow() bgneal@627: cutoff = now - datetime.timedelta(seconds=settings.WIKI_COOKIE_AGE) bgneal@627: min_score = time.mktime(cutoff.utctimetuple()) bgneal@627: bgneal@627: conn = get_redis_connection() bgneal@627: bgneal@627: set_name = settings.WIKI_REDIS_SET bgneal@627: try: bgneal@627: count = conn.zcard(set_name) bgneal@627: except redis.RedisError: bgneal@627: logger.error("Error getting zcard") bgneal@627: return bgneal@627: bgneal@627: try: bgneal@627: removed = conn.zremrangebyscore(set_name, 0.0, min_score) bgneal@627: except redis.RedisError: bgneal@627: logger.error("Error removing by score") bgneal@627: return bgneal@627: bgneal@627: total = count - removed bgneal@627: logger.info("Expire wiki cookies: removed %d, total is now %d", bgneal@627: removed, total)