Просмотр исходного кода

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

Jeremy Stretch 11 месяцев назад
Родитель
Сommit
ed135102be
1 измененных файлов с 6 добавлено и 3 удалено
  1. 6 3
      netbox/core/models/jobs.py

+ 6 - 3
netbox/core/models/jobs.py

@@ -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