Skip to content

Commit

Permalink
Fix receptor work unit release after completion
Browse files Browse the repository at this point in the history
Fix bug introduced by #15392 that cause workunit to NOT be auto released after job completes
  • Loading branch information
TheRealHaoLiu committed Dec 2, 2024
1 parent adc2162 commit 462661b
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions awx/main/tasks/receptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,40 @@ def run(self):
res = self._run_internal(receptor_ctl)
return res
finally:
# Make sure to always release the work unit if we established it
if self.unit_id is not None and settings.RECEPTOR_RELEASE_WORK:
if settings.RECPETOR_KEEP_WORK_ON_ERROR and getattr(res, 'status', 'error') == 'error':
try:
receptor_ctl.simple_command(f"work release {self.unit_id}")
except Exception:
logger.exception(f"Error releasing work unit {self.unit_id}.")
status = getattr(res, 'status', 'error')
self._receptor_release_work(receptor_ctl, status)

def _receptor_release_work(self, receptor_ctl: ReceptorControl, status: str) -> None:
"""
Releases the work unit from Receptor if certain conditions are met.
This method checks several conditions before attempting to release the work unit:
- If `self.unit_id` is `None`, the method returns immediately.
- If the `RECEPTOR_RELEASE_WORK` setting is `False`, the method returns immediately.
- If the `RECEPTOR_KEEP_WORK_ON_ERROR` setting is `True` and the status is 'error', the method returns immediately.
If none of the above conditions are met, the method attempts to release the work unit using the Receptor control command.
If an exception occurs during the release process, it logs an error message.
Args:
receptor_ctl (ReceptorControl): The Receptor control object used to issue commands.
status (str): The status of the work unit, which may affect whether it is released.
"""

if self.unit_id is None:
logger.debug("No work unit ID to release.")
return

if settings.RECEPTOR_RELEASE_WORK is False:
logger.debug(f"RECEPTOR_RELEASE_WORK is False, not releasing work unit {self.unit_id}.")
return

if settings.RECEPTOR_KEEP_WORK_ON_ERROR and status == 'error':
logger.debug(f"RECEPTOR_KEEP_WORK_ON_ERROR is True and status is 'error', not releasing work unit {self.unit_id}.")
return

try:
logger.debug(f"Released work unit {self.unit_id}.")
receptor_ctl.simple_command(f"work release {self.unit_id}")
except Exception:
logger.exception(f"Error releasing work unit {self.unit_id}.")

def _run_internal(self, receptor_ctl):
# Create a socketpair. Where the left side will be used for writing our payload
Expand Down

0 comments on commit 462661b

Please sign in to comment.