Skip to content

Commit 920d173

Browse files
committed
[OMCSession*] align all usages of timeout to the same structure
1 parent caaac47 commit 920d173

1 file changed

Lines changed: 52 additions & 44 deletions

File tree

OMPython/OMCSession.py

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -727,18 +727,21 @@ def sendExpression(self, command: str, parsed: bool = True) -> Any:
727727

728728
logger.debug("sendExpression(%r, parsed=%r)", command, parsed)
729729

730+
MAX_RETRIES = 50
730731
attempts = 0
731-
while True:
732+
while attempts < MAX_RETRIES:
733+
attempts += 1
734+
732735
try:
733736
self.omc_zmq.send_string(str(command), flags=zmq.NOBLOCK)
734737
break
735738
except zmq.error.Again:
736739
pass
737-
attempts += 1
738-
if attempts >= 50:
739-
raise OMCSessionException(f"No connection with OMC (timeout={self._timeout}). "
740-
f"Log-file says: \n{self.omc_process.get_log()}")
741-
time.sleep(self._timeout / 50.0)
740+
time.sleep(self._timeout / MAX_RETRIES)
741+
else:
742+
logger.error(f"Docker did not start. Log-file says:\n{self.omc_process.get_log()}")
743+
raise OMCSessionException(f"No connection with OMC (timeout={self._timeout}).")
744+
742745
if command == "quit()":
743746
self.omc_zmq.close()
744747
self.omc_zmq = None
@@ -1030,25 +1033,23 @@ def _omc_port_get(self) -> str:
10301033
port = None
10311034

10321035
# See if the omc server is running
1036+
MAX_RETRIES = 80
10331037
attempts = 0
1034-
while True:
1035-
omc_portfile_path = self._get_portfile_path()
1038+
while attempts < MAX_RETRIES:
1039+
attempts += 1
10361040

1041+
omc_portfile_path = self._get_portfile_path()
10371042
if omc_portfile_path is not None and omc_portfile_path.is_file():
10381043
# Read the port file
10391044
with open(file=omc_portfile_path, mode='r', encoding="utf-8") as f_p:
10401045
port = f_p.readline()
10411046
break
1042-
10431047
if port is not None:
10441048
break
1045-
1046-
attempts += 1
1047-
if attempts == 80.0:
1048-
raise OMCSessionException(f"OMC Server did not start (timeout={self._timeout}). "
1049-
f"Could not open file {omc_portfile_path}. "
1050-
f"Log-file says:\n{self.get_log()}")
1051-
time.sleep(self._timeout / 80.0)
1049+
time.sleep(self._timeout / MAX_RETRIES)
1050+
else:
1051+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1052+
raise OMCSessionException(f"OMC Server did not start (timeout={self._timeout}).")
10521053

10531054
logger.info(f"Local OMC Server is up and running at ZMQ port {port} "
10541055
f"pid={self._omc_process.pid if isinstance(self._omc_process, subprocess.Popen) else '?'}")
@@ -1130,7 +1131,11 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]:
11301131
raise NotImplementedError("Docker not supported on win32!")
11311132

11321133
docker_process = None
1133-
for _ in range(0, 40):
1134+
MAX_RETRIES = 40
1135+
attempts = 0
1136+
while attempts < MAX_RETRIES:
1137+
attempts += 1
1138+
11341139
docker_top = subprocess.check_output(["docker", "top", docker_cid]).decode().strip()
11351140
docker_process = None
11361141
for line in docker_top.split("\n"):
@@ -1141,10 +1146,12 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]:
11411146
except psutil.NoSuchProcess as ex:
11421147
raise OMCSessionException(f"Could not find PID {docker_top} - "
11431148
"is this a docker instance spawned without --pid=host?") from ex
1144-
11451149
if docker_process is not None:
11461150
break
1147-
time.sleep(self._timeout / 40.0)
1151+
time.sleep(self._timeout / MAX_RETRIES)
1152+
else:
1153+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1154+
raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}).")
11481155

11491156
return docker_process
11501157

@@ -1166,8 +1173,11 @@ def _omc_port_get(self) -> str:
11661173
raise OMCSessionException(f"Invalid docker container ID: {self._docker_container_id}")
11671174

11681175
# See if the omc server is running
1176+
MAX_RETRIES = 80
11691177
attempts = 0
1170-
while True:
1178+
while attempts < MAX_RETRIES:
1179+
attempts += 1
1180+
11711181
omc_portfile_path = self._get_portfile_path()
11721182
if omc_portfile_path is not None:
11731183
try:
@@ -1178,16 +1188,12 @@ def _omc_port_get(self) -> str:
11781188
port = output.decode().strip()
11791189
except subprocess.CalledProcessError:
11801190
pass
1181-
11821191
if port is not None:
11831192
break
1184-
1185-
attempts += 1
1186-
if attempts == 80.0:
1187-
raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}). "
1188-
f"Could not open port file {omc_portfile_path}. "
1189-
f"Log-file says:\n{self.get_log()}")
1190-
time.sleep(self._timeout / 80.0)
1193+
time.sleep(self._timeout / MAX_RETRIES)
1194+
else:
1195+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1196+
raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}).")
11911197

11921198
logger.info(f"Docker based OMC Server is up and running at port {port}")
11931199

@@ -1355,25 +1361,28 @@ def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen, str]:
13551361
raise OMCSessionException(f"Invalid content for docker container ID file path: {docker_cid_file}")
13561362

13571363
docker_cid = None
1358-
for _ in range(0, 40):
1364+
MAX_RETRIES = 40
1365+
attempts = 0
1366+
while attempts < MAX_RETRIES:
1367+
attempts += 1
1368+
13591369
try:
13601370
with open(file=docker_cid_file, mode="r", encoding="utf-8") as fh:
13611371
docker_cid = fh.read().strip()
13621372
except IOError:
13631373
pass
1364-
if docker_cid:
1374+
if docker_cid is not None:
13651375
break
1366-
time.sleep(self._timeout / 40.0)
1367-
1368-
if docker_cid is None:
1376+
time.sleep(self._timeout / MAX_RETRIES)
1377+
else:
13691378
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
13701379
raise OMCSessionException(f"Docker did not start (timeout={self._timeout} might be too short "
13711380
"especially if you did not docker pull the image before this command).")
13721381

13731382
docker_process = self._docker_process_get(docker_cid=docker_cid)
13741383
if docker_process is None:
1375-
raise OMCSessionException(f"Docker top did not contain omc process {self._random_string}. "
1376-
f"Log-file says:\n{self.get_log()}")
1384+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1385+
raise OMCSessionException(f"Docker top did not contain omc process {self._random_string}.")
13771386

13781387
return omc_process, docker_process, docker_cid
13791388

@@ -1529,8 +1538,11 @@ def _omc_port_get(self) -> str:
15291538
port = None
15301539

15311540
# See if the omc server is running
1541+
MAX_RETRIES = 80
15321542
attempts = 0
1533-
while True:
1543+
while attempts < MAX_RETRIES:
1544+
attempts += 1
1545+
15341546
try:
15351547
omc_portfile_path = self._get_portfile_path()
15361548
if omc_portfile_path is not None:
@@ -1541,16 +1553,12 @@ def _omc_port_get(self) -> str:
15411553
port = output.decode().strip()
15421554
except subprocess.CalledProcessError:
15431555
pass
1544-
15451556
if port is not None:
15461557
break
1547-
1548-
attempts += 1
1549-
if attempts == 80.0:
1550-
raise OMCSessionException(f"WSL based OMC Server did not start (timeout={self._timeout}). "
1551-
f"Could not open port file {omc_portfile_path}. "
1552-
f"Log-file says:\n{self.get_log()}")
1553-
time.sleep(self._timeout / 80.0)
1558+
time.sleep(self._timeout / MAX_RETRIES)
1559+
else:
1560+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1561+
raise OMCSessionException(f"WSL based OMC Server did not start (timeout={self._timeout}).")
15541562

15551563
logger.info(f"WSL based OMC Server is up and running at ZMQ port {port} "
15561564
f"pid={self._omc_process.pid if isinstance(self._omc_process, subprocess.Popen) else '?'}")

0 commit comments

Comments
 (0)