chore: clarify transaction-related comments (#8180)

This commit is contained in:
Jennifer Richards 2024-11-08 11:55:47 +00:00 committed by GitHub
parent 8c787b54f9
commit a35aa67c70
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 9 deletions

View file

@ -117,7 +117,7 @@ def notify_events(sender, instance, **kwargs):
# start a Celery task during tests. To prevent this, don't queue a celery task if we're running # start a Celery task during tests. To prevent this, don't queue a celery task if we're running
# tests. # tests.
if settings.SERVER_MODE != "test": if settings.SERVER_MODE != "test":
# Wrap in on_commit so the delayed task cannot start until the view is done with the DB # Wrap in on_commit in case a transaction is open
transaction.on_commit( transaction.on_commit(
lambda: notify_event_to_subscribers_task.delay(event_id=instance.pk) lambda: notify_event_to_subscribers_task.delay(event_id=instance.pk)
) )

View file

@ -2334,8 +2334,8 @@ class ApprovalsTestCase(BaseSubmitTestCase):
self.assertEqual(len(Preapproval.objects.filter(name=preapproval.name)), 0) self.assertEqual(len(Preapproval.objects.filter(name=preapproval.name)), 0)
# Transaction.on_commit() requires use of TransactionTestCase, but that has a performance penalty. Replace it # Transaction.on_commit() interacts badly with TestCase's transaction behavior. Replace it
# with a no-op for testing purposes. # with a pass-through for testing purposes.
@mock.patch.object(transaction, 'on_commit', lambda x: x()) @mock.patch.object(transaction, 'on_commit', lambda x: x())
@override_settings(IDTRACKER_BASE_URL='https://datatracker.example.com') @override_settings(IDTRACKER_BASE_URL='https://datatracker.example.com')
class ApiSubmissionTests(BaseSubmitTestCase): class ApiSubmissionTests(BaseSubmitTestCase):

View file

@ -90,7 +90,8 @@ def upload_submission(request):
clear_existing_files(form) clear_existing_files(form)
save_files(form) save_files(form)
create_submission_event(request, submission, desc="Uploaded submission") create_submission_event(request, submission, desc="Uploaded submission")
# Wrap in on_commit so the delayed task cannot start until the view is done with the DB # Wrap in on_commit in case a transaction is open
# (As of 2024-11-08, this only runs in a transaction during tests)
transaction.on_commit( transaction.on_commit(
lambda: process_uploaded_submission_task.delay(submission.pk) lambda: process_uploaded_submission_task.delay(submission.pk)
) )
@ -166,7 +167,8 @@ def api_submission(request):
save_files(form) save_files(form)
create_submission_event(request, submission, desc="Uploaded submission through API") create_submission_event(request, submission, desc="Uploaded submission through API")
# Wrap in on_commit so the delayed task cannot start until the view is done with the DB # Wrap in on_commit in case a transaction is open
# (As of 2024-11-08, this only runs in a transaction during tests)
transaction.on_commit( transaction.on_commit(
lambda: process_and_accept_uploaded_submission_task.delay(submission.pk) lambda: process_and_accept_uploaded_submission_task.delay(submission.pk)
) )

View file

@ -80,25 +80,29 @@ def notify(request, org, notification):
if request.method == "POST": if request.method == "POST":
if notification == "index": if notification == "index":
log("Queuing RFC Editor index sync from notify view POST") log("Queuing RFC Editor index sync from notify view POST")
# Wrap in on_commit so the delayed task cannot start until the view is done with the DB # Wrap in on_commit in case a transaction is open
# (As of 2024-11-08, this only runs in a transaction during tests)
transaction.on_commit( transaction.on_commit(
lambda: tasks.rfc_editor_index_update_task.delay() lambda: tasks.rfc_editor_index_update_task.delay()
) )
elif notification == "queue": elif notification == "queue":
log("Queuing RFC Editor queue sync from notify view POST") log("Queuing RFC Editor queue sync from notify view POST")
# Wrap in on_commit so the delayed task cannot start until the view is done with the DB # Wrap in on_commit in case a transaction is open
# (As of 2024-11-08, this only runs in a transaction during tests)
transaction.on_commit( transaction.on_commit(
lambda: tasks.rfc_editor_queue_updates_task.delay() lambda: tasks.rfc_editor_queue_updates_task.delay()
) )
elif notification == "changes": elif notification == "changes":
log("Queuing IANA changes sync from notify view POST") log("Queuing IANA changes sync from notify view POST")
# Wrap in on_commit so the delayed task cannot start until the view is done with the DB # Wrap in on_commit in case a transaction is open
# (As of 2024-11-08, this only runs in a transaction during tests)
transaction.on_commit( transaction.on_commit(
lambda: tasks.iana_changes_update_task.delay() lambda: tasks.iana_changes_update_task.delay()
) )
elif notification == "protocols": elif notification == "protocols":
log("Queuing IANA protocols sync from notify view POST") log("Queuing IANA protocols sync from notify view POST")
# Wrap in on_commit so the delayed task cannot start until the view is done with the DB # Wrap in on_commit in case a transaction is open
# (As of 2024-11-08, this only runs in a transaction during tests)
transaction.on_commit( transaction.on_commit(
lambda: tasks.iana_protocols_update_task.delay() lambda: tasks.iana_protocols_update_task.delay()
) )