diff --git a/contentcuration/contentcuration/models.py b/contentcuration/contentcuration/models.py index 775fe4414e..43513ead44 100644 --- a/contentcuration/contentcuration/models.py +++ b/contentcuration/contentcuration/models.py @@ -1701,7 +1701,7 @@ def has_changes(self): def recalculate_editors_storage(self): from contentcuration.utils.user import calculate_user_storage - for editor in self.files.values_list('uploaded_by_id', flat=True): + for editor in self.files.values_list('uploaded_by_id', flat=True).distinct(): calculate_user_storage(editor) def on_create(self): diff --git a/contentcuration/contentcuration/tasks.py b/contentcuration/contentcuration/tasks.py index f477c99d5c..7669d0dc08 100644 --- a/contentcuration/contentcuration/tasks.py +++ b/contentcuration/contentcuration/tasks.py @@ -8,7 +8,6 @@ from celery import states from celery.utils.log import get_task_logger from django.conf import settings -from django.core.cache import cache from django.core.mail import EmailMessage from django.db import IntegrityError from django.db.utils import OperationalError @@ -27,7 +26,6 @@ from contentcuration.utils.nodes import generate_diff from contentcuration.utils.publish import publish_channel from contentcuration.utils.sync import sync_channel -from contentcuration.utils.user import CACHE_USER_STORAGE_KEY from contentcuration.viewsets.sync.constants import CHANNEL from contentcuration.viewsets.sync.constants import CONTENTNODE from contentcuration.viewsets.sync.constants import COPYING_FLAG @@ -266,7 +264,6 @@ def calculate_user_storage_task(user_id): try: user = User.objects.get(pk=user_id) user.set_space_used() - cache.delete(CACHE_USER_STORAGE_KEY.format(user_id)) except User.DoesNotExist: logging.error("Tried to calculate user storage for user with id {} but they do not exist".format(user_id)) @@ -296,6 +293,7 @@ def sendcustomemails_task(subject, message, query): "export-channel": export_channel_task, "sync-channel": sync_channel_task, "get-node-diff": generatenodediff_task, + "calculate-user-storage": calculate_user_storage_task, "calculate-resource-size": calculate_resource_size_task, } diff --git a/contentcuration/contentcuration/utils/user.py b/contentcuration/contentcuration/utils/user.py index 765341154c..5bcb9d8dfc 100644 --- a/contentcuration/contentcuration/utils/user.py +++ b/contentcuration/contentcuration/utils/user.py @@ -1,12 +1,15 @@ -from django.core.cache import cache +import logging -CACHE_USER_STORAGE_KEY = "user_storage_{}" +from contentcuration.tasks import get_or_create_async_task def calculate_user_storage(user_id): - from contentcuration.tasks import calculate_user_storage_task - - key = CACHE_USER_STORAGE_KEY.format(user_id) - if key not in cache: - cache.set(key, True, timeout=600) # Invalidate after 10 minutes - calculate_user_storage_task.s(user_id).apply_async(countdown=5) + """TODO: Perhaps move this to User model to avoid unnecessary User lookups""" + from contentcuration.models import User + try: + if user_id is None: + raise User.DoesNotExist + user = User.objects.get(pk=user_id) + get_or_create_async_task('calculate-user-storage', user, user_id=user_id) + except User.DoesNotExist: + logging.error("Tried to calculate user storage for user with id {} but they do not exist".format(user_id))