diff --git a/lms/celery.py b/lms/celery.py index eca05a64e1..4eff388c50 100644 --- a/lms/celery.py +++ b/lms/celery.py @@ -7,6 +7,10 @@ Taken from: https://celery.readthedocs.org/en/latest/django/first-steps-with-dja import os +from celery.signals import task_prerun +from django.dispatch import receiver +from edx_django_utils.monitoring import set_custom_attribute + # Patch the xml libs before anything else. from openedx.core.lib.safe_lxml import defuse_xml_libs @@ -17,3 +21,20 @@ defuse_xml_libs() # and then instantiate the Celery singleton. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lms.envs.production') from openedx.core.lib.celery import APP # pylint: disable=wrong-import-position,unused-import + + +@receiver(task_prerun) +def set_code_owner_on_celery_tasks(*, task, **kwargs): + """ + Sets the `code_owner` custom attribute on all Celery tasks, obviating the + need for the set_code_owner_attribute task decorator. + + ...or rather, we're not yet sure whether this works, so we're setting a + different custom attribute first. + + See https://github.com/openedx/edx-platform/issues/33179 for details. + """ + try: + set_custom_attribute("auto_celery_code_owner_module", task.__module__) + except Exception as e: # pylint: disable=broad-except + set_custom_attribute("auto_celery_code_owner_error", repr(e))