Files
edx-platform/common/djangoapps/entitlements/tasks.py
Kyle McCormick b626a43fa1 fix: Rename some tasks to match new import paths (#26236)
Several tasks are explicitly named as (or like)
their old, deprecated import path.

The issue here is that django-user-tasks listens for task
invocations, and attempts to import the task based on its name.
If the task name is completely wrong, user-tasks will catch
the ImportError and move on.
If the task is a valid *deprecated* import, though, then
user-tasks will choke on the raised `DeprecatedEdxPlatformImportError`.

Thus, we must rename three tasks to their new full path:
1. entitlements.expire_old_enrollments
2. third_party_auth.fetch_saml_metadata
3. student.send_activation_email

The first two are run daily, and so are safe to be
renamed in place.

The third task must be renamed using an expand-contract
pattern; otherwise, we would drop hundreds of tasks
during the App vs. Worker out-of-sync version window
that happens at deployments.
This commit is the expand phase.
2021-02-02 10:26:08 -05:00

65 lines
2.7 KiB
Python

"""
This file contains celery tasks for entitlements-related functionality.
"""
from celery import shared_task
from celery.utils.log import get_task_logger
from django.conf import settings
from edx_django_utils.monitoring import set_code_owner_attribute
from common.djangoapps.entitlements.models import CourseEntitlement
LOGGER = get_task_logger(__name__)
# Maximum number of retries before giving up on awarding credentials.
# For reference, 11 retries with exponential backoff yields a maximum waiting
# time of 2047 seconds (about 30 minutes). Setting this to None could yield
# unwanted behavior: infinite retries.
MAX_RETRIES = 11
@shared_task(bind=True, ignore_result=True)
@set_code_owner_attribute
def expire_old_entitlements(self, start, end, logid='...'):
"""
This task is designed to be called to process a bundle of entitlements
that might be expired and confirm if we can do so. This is useful when
checking if an entitlement has just been abandoned by the learner and needs
to be expired. (In the normal course of a learner using the platform, the
entitlement will expire itself. But if a learner doesn't log in... So we
run this task every now and then to clear the backlog.)
Args:
start (int): The beginning id in the database to examine
end (int): The id in the database to stop examining at (i.e. range is exclusive)
logid (str): A string to identify this task in the logs
Returns:
None
"""
LOGGER.info('Running task expire_old_entitlements %d:%d [%s]', start, end, logid)
# This query could be optimized to return a more narrow set, but at a
# complexity cost. See bug LEARNER-3451 about improving it.
entitlements = CourseEntitlement.objects.filter(expired_at__isnull=True, id__gte=start, id__lte=end)
countdown = 2 ** self.request.retries
try:
for entitlement in entitlements:
# This property request will update the expiration if necessary as
# a side effect. We could manually call update_expired_at(), but
# let's use the same API the rest of the LMS does, to mimic normal
# usage and allow the update call to be an internal detail.
if entitlement.expired_at_datetime:
LOGGER.info('Expired entitlement with id %d [%s]', entitlement.id, logid)
except Exception as exc:
LOGGER.exception('Failed to expire entitlements [%s]', logid)
# The call above is idempotent, so retry at will
raise self.retry(exc=exc, countdown=countdown, max_retries=MAX_RETRIES)
LOGGER.info('Successfully completed the task expire_old_entitlements after examining %d entries [%s]', entitlements.count(), logid)