Skip to content

Commit

Permalink
Move special runner message into post-processing of callback fields
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCoding committed May 2, 2022
1 parent 77049f6 commit 9bcce9c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
6 changes: 5 additions & 1 deletion awx/main/tasks/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# AWX
from awx.main.redact import UriCleaner
from awx.main.constants import MINIMAL_EVENTS
from awx.main.constants import MINIMAL_EVENTS, ANSIBLE_RUNNER_NEEDS_UPDATE_MESSAGE
from awx.main.utils.update_model import update_model
from awx.main.queue import CallbackQueueDispatcher

Expand Down Expand Up @@ -52,13 +52,17 @@ def delay_update(self, skip_if_already_set=False, **kwargs):
if key in self.extra_update_fields and skip_if_already_set:
continue
elif key in self.extra_update_fields and key in ('job_explanation', 'result_traceback'):
if str(value) in self.extra_update_fields.get(key, ''):
continue # if already set, avoid duplicating messages
# In the case of these fields, we do not want to lose any prior information, so combine values
self.extra_update_fields[key] = '\n'.join([str(self.extra_update_fields[key]), str(value)])
else:
self.extra_update_fields[key] = value

def get_extra_update_fields(self):
self.extra_update_fields['emitted_events'] = self.event_ct
if 'got an unexpected keyword argument' in self.extra_update_fields.get('result_traceback', ''):
self.delay_update(result_traceback=ANSIBLE_RUNNER_NEEDS_UPDATE_MESSAGE)
return self.extra_update_fields

def event_handler(self, event_data):
Expand Down
7 changes: 0 additions & 7 deletions awx/main/tasks/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
JOB_FOLDER_PREFIX,
MAX_ISOLATED_PATH_COLON_DELIMITER,
CONTAINER_VOLUMES_MOUNT_TYPES,
ANSIBLE_RUNNER_NEEDS_UPDATE_MESSAGE,
)
from awx.main.models import (
Instance,
Expand Down Expand Up @@ -568,12 +567,6 @@ def run(self, pk, **kwargs):
except Exception:
logger.exception('{} Post run hook errored.'.format(self.instance.log_format))

# We really shouldn't get into this one but just in case....
original_traceback = self.runner_callback.get_extra_update_fields().get('result_traceback', '')
if 'got an unexpected keyword argument' in original_traceback:
if ANSIBLE_RUNNER_NEEDS_UPDATE_MESSAGE not in original_traceback:
self.runner_callback.delay_update(result_traceback=ANSIBLE_RUNNER_NEEDS_UPDATE_MESSAGE)

self.instance = self.update_model(pk)
self.instance = self.update_model(pk, status=status, select_for_update=True, **self.runner_callback.get_extra_update_fields())

Expand Down
7 changes: 1 addition & 6 deletions awx/main/tasks/receptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
parse_yaml_or_json,
cleanup_new_process,
)
from awx.main.constants import (
MAX_ISOLATED_PATH_COLON_DELIMITER,
ANSIBLE_RUNNER_NEEDS_UPDATE_MESSAGE,
)
from awx.main.constants import MAX_ISOLATED_PATH_COLON_DELIMITER

# Receptorctl
from receptorctl.socket_interface import ReceptorControl
Expand Down Expand Up @@ -377,8 +374,6 @@ def _run_internal(self, receptor_ctl):
receptor_output = b"".join(lines).decode()
if receptor_output:
self.task.runner_callback.delay_update(result_traceback=receptor_output)
if 'got an unexpected keyword argument' in receptor_output:
self.task.runner_callback.delay_update(result_traceback=ANSIBLE_RUNNER_NEEDS_UPDATE_MESSAGE)
elif detail:
self.task.runner_callback.delay_update(result_traceback=detail)
else:
Expand Down
20 changes: 20 additions & 0 deletions awx/main/tests/unit/tasks/test_runner_callback.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from awx.main.tasks.callback import RunnerCallback
from awx.main.constants import ANSIBLE_RUNNER_NEEDS_UPDATE_MESSAGE

from django.utils.translation import ugettext_lazy as _

Expand Down Expand Up @@ -30,3 +31,22 @@ def test_delay_update_failure_fields(mock_me):
rc.delay_update(result_traceback=_('2'))
rc.delay_update(result_traceback=_('3'), skip_if_already_set=True)
assert rc.extra_update_fields == {'job_explanation': '1\n2', 'result_traceback': '1\n2'}


def test_duplicate_updates(mock_me):
rc = RunnerCallback()
rc.delay_update(job_explanation='really long summary...')
rc.delay_update(job_explanation='really long summary...')
rc.delay_update(job_explanation='really long summary...')
assert rc.extra_update_fields == {'job_explanation': 'really long summary...'}


def test_special_ansible_runner_message(mock_me):
rc = RunnerCallback()
rc.delay_update(result_traceback='Traceback:\ngot an unexpected keyword argument\nFile: foo.py')
rc.delay_update(result_traceback='Traceback:\ngot an unexpected keyword argument\nFile: bar.py')
assert rc.get_extra_update_fields().get('result_traceback') == (
'Traceback:\ngot an unexpected keyword argument\nFile: foo.py\n'
'Traceback:\ngot an unexpected keyword argument\nFile: bar.py\n'
f'{ANSIBLE_RUNNER_NEEDS_UPDATE_MESSAGE}'
)

0 comments on commit 9bcce9c

Please sign in to comment.