Browse Source

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

Jeremy Stretch 11 months ago
parent
commit
ed135102be
1 changed files with 6 additions and 3 deletions
  1. 6 3
      netbox/core/models/jobs.py

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

@@ -1,11 +1,12 @@
 import uuid
+from functools import partial
 
 import django_rq
 from django.conf import settings
 from django.contrib.contenttypes.fields import GenericForeignKey
 from django.core.exceptions import ValidationError
 from django.core.validators import MinValueValidator
-from django.db import models
+from django.db import models, transaction
 from django.urls import reverse
 from django.utils import timezone
 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.
         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.
         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