Fixes #18880: Delay enqueuing of background tasks until the DB transaction has been committed (#18899)

This commit is contained in:
Jeremy Stretch 2025-03-13 12:34:12 -04:00 committed by GitHub
parent 78332d44c7
commit ed135102be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,11 +1,12 @@
import uuid import uuid
from functools import partial
import django_rq import django_rq
from django.conf import settings from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.fields import GenericForeignKey
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator
from django.db import models from django.db import models, transaction
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
@ -258,10 +259,12 @@ class Job(models.Model):
# Schedule the job to run at a specific date & time. # Schedule the job to run at a specific date & time.
elif schedule_at: elif schedule_at:
queue.enqueue_at(schedule_at, func, job_id=str(job.job_id), job=job, **kwargs) callback = partial(queue.enqueue_at, schedule_at, func, job_id=str(job.job_id), job=job, **kwargs)
transaction.on_commit(callback)
# Schedule the job to run asynchronously at this first available opportunity. # Schedule the job to run asynchronously at this first available opportunity.
else: else:
queue.enqueue(func, job_id=str(job.job_id), job=job, **kwargs) callback = partial(queue.enqueue, func, job_id=str(job.job_id), job=job, **kwargs)
transaction.on_commit(callback)
return job return job