Skip to content

Commit

Permalink
make it work in autocommit mode
Browse files Browse the repository at this point in the history
Signed-off-by: Seth Foster <[email protected]>
  • Loading branch information
fosterseth committed Jun 7, 2024
1 parent 2855a9c commit 336bfdc
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions awx/main/models/unified_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

# Django
from django.conf import settings
from django.db import models, connection
from django.db import models, connection, transaction
from django.core.exceptions import NON_FIELD_ERRORS
from django.utils.translation import gettext_lazy as _
from django.utils.timezone import now
Expand Down Expand Up @@ -269,13 +269,18 @@ def last_updated(self):

def update_computed_fields(self):
related_schedules = self.schedules.filter(enabled=True, next_run__isnull=False).order_by('-next_run')
# select for update will lock this row until the transaction is committed
# prevent race condition where new_next_schedule may be deleted during this transaction
new_next_schedule = related_schedules.select_for_update().first()
new_next_schedule = related_schedules.first()
if new_next_schedule:
if new_next_schedule.pk == self.next_schedule_id and new_next_schedule.next_run == self.next_job_run:
return # no-op, common for infrequent schedules
self.next_schedule = new_next_schedule

# If in a transaction, use select_for_update to lock the next schedule row, which
# prevents a race condition if new_next_schedule is deleted elsewhere during this transaction
if transaction.get_autocommit():
self.next_schedule = related_schedules.first()
else:
self.next_schedule = related_schedules.select_for_update().first()

self.next_job_run = new_next_schedule.next_run
self.save(update_fields=['next_schedule', 'next_job_run'])

Expand Down

0 comments on commit 336bfdc

Please sign in to comment.