Decoupled callback functions from BaseTask Class

--- Removed all callback functions from 'jobs.py' and put them in a new file '/awx/main/tasks/callback.py'
--- Modified Unit tests unit moved
--- Moved 'update_model' from jobs.py to /awx/main/utils/update_model.py
This commit is contained in:
Amol Gautam
2022-01-31 06:04:23 -05:00
parent f8e680867b
commit 443bdc1234
5 changed files with 433 additions and 350 deletions

View File

@@ -0,0 +1,40 @@
from django.db import transaction, DatabaseError
import logging
import time
logger = logging.getLogger('awx.main.tasks.utils')
def update_model(model, pk, _attempt=0, **updates):
"""Reload the model instance from the database and update the
given fields.
"""
try:
with transaction.atomic():
# Retrieve the model instance.
instance = model.objects.get(pk=pk)
# Update the appropriate fields and save the model
# instance, then return the new instance.
if updates:
update_fields = ['modified']
for field, value in updates.items():
setattr(instance, field, value)
update_fields.append(field)
if field == 'status':
update_fields.append('failed')
instance.save(update_fields=update_fields)
return instance
except DatabaseError as e:
# Log out the error to the debug logger.
logger.debug('Database error updating %s, retrying in 5 seconds (retry #%d): %s', model._meta.object_name, _attempt + 1, e)
# Attempt to retry the update, assuming we haven't already
# tried too many times.
if _attempt < 5:
time.sleep(5)
return update_model(model, pk, _attempt=_attempt + 1, **updates)
else:
logger.error('Failed to update %s after %d retries.', model._meta.object_name, _attempt)