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