Skip to content

Commit

Permalink
refactor: use StopAsyncIteration to stop retries
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Shimkus <[email protected]>
  • Loading branch information
jshimkus-rh committed Oct 30, 2023
1 parent acb6534 commit 42a4735
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
16 changes: 11 additions & 5 deletions ansible_rulebook/action/run_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ async def __call__(self):
await self._run()
await self._post_process()

async def _do_run(self) -> bool:
async def _do_run(self):
"""Performs a standalone single attempt to effect the requested
operation. In the event of success or non-retriable error raises
StopAsyncIteration exception.
"""
raise NotImplementedError

async def _run(self):
Expand All @@ -83,8 +87,9 @@ async def _run(self):
retries,
)

retry = await self._do_run()
if not retry:
try:
await self._do_run()
except StopAsyncIteration:
break

async def _pre_process(self) -> None:
Expand Down Expand Up @@ -170,7 +175,7 @@ async def __call__(self):
)
await super().__call__()

async def _do_run(self) -> bool:
async def _do_run(self):
exception = False
try:
controller_job = await self._run_job(
Expand All @@ -188,7 +193,8 @@ async def _do_run(self) -> bool:

self.controller_job = controller_job

return (not exception) and (self.controller_job["status"] == "failed")
if exception or (self.controller_job["status"] != "failed"):
raise StopAsyncIteration

async def _post_process(self) -> None:
a_log = self._make_log()
Expand Down
5 changes: 3 additions & 2 deletions ansible_rulebook/action/run_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async def _run(self):
logger.info("Calling Ansible runner")
await super()._run()

async def _do_run(self) -> bool:
async def _do_run(self):
await Runner(
self.private_data_dir,
self.host_limit,
Expand All @@ -98,7 +98,8 @@ async def _do_run(self) -> bool:
self.helper,
self._runner_args(),
)()
return self._get_latest_artifact("status") == "failed"
if self._get_latest_artifact("status") != "failed":
raise StopAsyncIteration

def _runner_args(self) -> dict:
return {"playbook": self.name, "inventory": self.inventory}
Expand Down

0 comments on commit 42a4735

Please sign in to comment.