shell_command: run_with_response fixes

Reset the command tracking data before each retry.  If the command is cancelled by the user immediately break.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2021-05-21 07:19:29 -04:00
parent 564e33bc18
commit e15ef345bc
1 changed files with 6 additions and 2 deletions

View File

@ -192,9 +192,10 @@ class ShellCommand:
log_complete: bool = True, log_complete: bool = True,
sig_idx: int = 1 sig_idx: int = 1
) -> str: ) -> str:
self._reset_command_data()
retries = max(1, retries) retries = max(1, retries)
while retries > 0: while retries > 0:
self._reset_command_data()
timed_out = False
stdout = stderr = b"" stdout = stderr = b""
if await self._create_subprocess(): if await self._create_subprocess():
assert self.proc is not None assert self.proc is not None
@ -204,6 +205,7 @@ class ShellCommand:
ret, timeout=timeout) ret, timeout=timeout)
except asyncio.TimeoutError: except asyncio.TimeoutError:
complete = False complete = False
timed_out = True
await self.proc.cancel(sig_idx) await self.proc.cancel(sig_idx)
else: else:
complete = not self.cancelled complete = not self.cancelled
@ -211,10 +213,12 @@ class ShellCommand:
logging.info(f"{self.command[0]}: {stderr.decode()}") logging.info(f"{self.command[0]}: {stderr.decode()}")
if self._check_proc_success(complete, log_complete): if self._check_proc_success(complete, log_complete):
return stdout.decode().rstrip("\n") return stdout.decode().rstrip("\n")
elif stdout: if stdout:
logging.debug( logging.debug(
f"Shell command '{self.name}' output:" f"Shell command '{self.name}' output:"
f"\n{stdout.decode()}") f"\n{stdout.decode()}")
if self.cancelled and not timed_out:
break
retries -= 1 retries -= 1
await gen.sleep(.5) await gen.sleep(.5)
raise ShellCommandError( raise ShellCommandError(