Skip to content

Commit c6d8515

Browse files
authored
Merge pull request #56 from openedx/bmtcril/fix_userprofilenotfound
fix: User profile not found on account creation
2 parents 99bcf4e + 9dc7496 commit c6d8515

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

CHANGELOG.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ Change Log
1414
Unreleased
1515
**********
1616

17+
0.9.5 - 2024-05-24
18+
******************
19+
20+
Fixes
21+
=====
22+
23+
* UserProfile sink now runs after the transaction is committed, preventing UserProfileNotFound errors and creation of rows in ClickHouse that don't exist in MySQL in the case of a rollback.
24+
25+
1726
0.9.4 - 2024-05-16
1827
******************
1928

platform_plugin_aspects/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
import os
66
from pathlib import Path
77

8-
__version__ = "0.9.4"
8+
__version__ = "0.9.5"
99

1010
ROOT_DIRECTORY = Path(os.path.dirname(os.path.abspath(__file__)))

platform_plugin_aspects/signals.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Signal handler functions, mapped to specific signals in apps.py.
33
"""
44

5+
from django.db import transaction
56
from django.db.models.signals import post_save
67
from django.dispatch import Signal, receiver
78

@@ -33,11 +34,9 @@ def receive_course_publish( # pylint: disable=unused-argument # pragma: no cov
3334
dump_course_to_clickhouse.delay(str(course_key))
3435

3536

36-
def on_user_profile_updated( # pylint: disable=unused-argument # pragma: no cover
37-
sender, instance, **kwargs
38-
):
37+
def on_user_profile_updated(instance):
3938
"""
40-
Receives post save signal and queues the dump job.
39+
Queues the UserProfile dump job when the parent transaction is committed.
4140
"""
4241
# import here, because signal is registered at startup, but items in tasks are not yet able to be loaded
4342
from platform_plugin_aspects.tasks import ( # pylint: disable=import-outside-toplevel
@@ -52,11 +51,26 @@ def on_user_profile_updated( # pylint: disable=unused-argument # pragma: no co
5251
)
5352

5453

54+
def on_user_profile_updated_txn(**kwargs):
55+
"""
56+
Handle user_profile saves in the middle of a transaction.
57+
58+
If this gets fired before the transaction commits, the task may try to
59+
query an id that doesn't exist yet and throw an error. This should postpone
60+
queuing the Celery task until after the transaction is committed.
61+
"""
62+
transaction.on_commit(
63+
lambda: on_user_profile_updated(kwargs["instance"])
64+
) # pragma: no cover
65+
66+
5567
# Connect the UserProfile.post_save signal handler only if we have a model to attach to.
5668
# (prevents celery errors during tests)
5769
_user_profile = get_model("user_profile")
5870
if _user_profile:
59-
post_save.connect(on_user_profile_updated, sender=_user_profile) # pragma: no cover
71+
post_save.connect(
72+
on_user_profile_updated_txn, sender=_user_profile
73+
) # pragma: no cover
6074

6175

6276
def on_externalid_saved( # pylint: disable=unused-argument # pragma: no cover

platform_plugin_aspects/xblock.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
# These moved from xblockutils to xblock in Quince, these can be removed
1818
# when we stop supporting earlier versions.
19-
try: # pragma: no-cover
19+
try: # pragma: no cover
2020
from xblock.utils.resources import ResourceLoader
2121
from xblock.utils.studio_editable import StudioEditableXBlockMixin
22-
except ImportError:
22+
except ImportError: # pragma: no cover
2323
from xblockutils.resources import ResourceLoader
2424
from xblockutils.studio_editable import StudioEditableXBlockMixin
2525

0 commit comments

Comments
 (0)