From 9aa39dadcaa8c75aebe4fb732db6f80b49187851 Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Fri, 30 Jun 2023 12:29:52 -0600 Subject: [PATCH] Remote.reconnect(): Use a default timeout of 30s And rewrite with safe_while. Signed-off-by: Zack Cerza --- teuthology/orchestra/remote.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/teuthology/orchestra/remote.py b/teuthology/orchestra/remote.py index af5e139f7b..64d1cbb11d 100644 --- a/teuthology/orchestra/remote.py +++ b/teuthology/orchestra/remote.py @@ -4,6 +4,7 @@ Support for paramiko remote objects. import teuthology.lock.query import teuthology.lock.util +from teuthology.contextutil import safe_while from teuthology.orchestra import run from teuthology.orchestra import connection from teuthology.orchestra import console @@ -13,7 +14,6 @@ from teuthology import misc from teuthology.exceptions import CommandFailedError from teuthology.misc import host_shortname import errno -import time import re import logging from io import BytesIO @@ -386,7 +386,7 @@ class Remote(RemoteShell): self.ssh = connection.connect(**args) return self.ssh - def reconnect(self, timeout=None, socket_timeout=None, sleep_time=30): + def reconnect(self, timeout=30, socket_timeout=None): """ Attempts to re-establish connection. Returns True for success; False for failure. @@ -395,18 +395,15 @@ class Remote(RemoteShell): self.ssh.close() if not timeout: return self._reconnect(timeout=socket_timeout) - start_time = time.time() - elapsed_time = lambda: time.time() - start_time - while elapsed_time() < timeout: - success = self._reconnect(timeout=socket_timeout) - if success: - log.info(f"Successfully reconnected to host '{self.name}'") - break - # Don't let time_remaining be < 0 - time_remaining = max(0, timeout - elapsed_time()) - sleep_val = min(time_remaining, sleep_time) - time.sleep(sleep_val) - return success + action = "reconnect to {self.shortname}" + with safe_while(action=action, timeout=timeout, increment=3, _raise=False) as proceed: + success = False + while proceed(): + success = self._reconnect(timeout=socket_timeout) + if success: + log.info(f"Successfully reconnected to host '{self.name}'") + return success + return success def _reconnect(self, timeout=None): log.info(f"Trying to reconnect to host '{self.name}'") -- 2.39.5