Improve logging for TeePopen.timeout exceeded

This commit is contained in:
Mikhail f. Shiryaev 2023-01-23 12:50:49 +01:00
parent 4e0dd08f8c
commit f092b46025
No known key found for this signature in database
GPG Key ID: 4B02ED204C7D93F4

View File

@ -29,11 +29,13 @@ class TeePopen:
self.env = env or os.environ.copy()
self._process = None # type: Optional[Popen]
self.timeout = timeout
self.timeout_exceeded = False
def _check_timeout(self) -> None:
if self.timeout is None:
return
sleep(self.timeout)
self.timeout_exceeded = True
while self.process.poll() is None:
logging.warning(
"Killing process %s, timeout %s exceeded",
@ -62,6 +64,16 @@ class TeePopen:
def __exit__(self, exc_type, exc_value, traceback):
self.wait()
if self.timeout_exceeded:
exceeded_log = (
f"Command `{self.command}` has failed, "
f"timeout {self.timeout}s is exceeded"
)
if self.process.stdout is not None:
sys.stdout.write(exceeded_log)
self.log_file.write(exceeded_log)
self.log_file.close()
def wait(self) -> int: