Skip to content

Commit fe1775e

Browse files
committed
[OMCSession*] align all usages of timeout to the same structure
1 parent 73a38e5 commit fe1775e

1 file changed

Lines changed: 58 additions & 55 deletions

File tree

OMPython/OMCSession.py

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -868,34 +868,32 @@ def sendExpression(self, command: str, parsed: bool = True) -> Any:
868868
Caller should only check for OMCSessionException.
869869
"""
870870

871-
# this is needed if the class is not fully initialized or in the process of deletion
872-
if hasattr(self, '_timeout'):
873-
timeout = self._timeout
874-
else:
875-
timeout = 1.0
876-
877871
if self._omc_zmq is None:
878872
raise OMCSessionException("No OMC running. Please create a new instance of OMCSession!")
879873

880874
logger.debug("sendExpression(%r, parsed=%r)", command, parsed)
881875

876+
MAX_RETRIES = 50
882877
attempts = 0
883-
while True:
878+
while attempts < MAX_RETRIES:
879+
attempts += 1
880+
884881
try:
885882
self._omc_zmq.send_string(str(command), flags=zmq.NOBLOCK)
886883
break
887884
except zmq.error.Again:
888885
pass
889-
attempts += 1
890-
if attempts >= 50:
891-
# in the deletion process, the content is cleared. Thus, any access to a class attribute must be checked
892-
try:
893-
log_content = self.get_log()
894-
except OMCSessionException:
895-
log_content = 'log not available'
896-
raise OMCSessionException(f"No connection with OMC (timeout={timeout}). "
897-
f"Log-file says: \n{log_content}")
898-
time.sleep(timeout / 50.0)
886+
time.sleep(self._timeout / MAX_RETRIES)
887+
else:
888+
# in the deletion process, the content is cleared. Thus, any access to a class attribute must be checked
889+
try:
890+
log_content = self.get_log()
891+
except OMCSessionException:
892+
log_content = 'log not available'
893+
894+
logger.error(f"Docker did not start. Log-file says:\n{log_content}")
895+
raise OMCSessionException(f"No connection with OMC (timeout={self._timeout}).")
896+
899897
if command == "quit()":
900898
self._omc_zmq.close()
901899
self._omc_zmq = None
@@ -1124,25 +1122,23 @@ def _omc_port_get(self) -> str:
11241122
port = None
11251123

11261124
# See if the omc server is running
1125+
MAX_RETRIES = 80
11271126
attempts = 0
1128-
while True:
1129-
omc_portfile_path = self._get_portfile_path()
1127+
while attempts < MAX_RETRIES:
1128+
attempts += 1
11301129

1130+
omc_portfile_path = self._get_portfile_path()
11311131
if omc_portfile_path is not None and omc_portfile_path.is_file():
11321132
# Read the port file
11331133
with open(file=omc_portfile_path, mode='r', encoding="utf-8") as f_p:
11341134
port = f_p.readline()
11351135
break
1136-
11371136
if port is not None:
11381137
break
1139-
1140-
attempts += 1
1141-
if attempts == 80.0:
1142-
raise OMCSessionException(f"OMC Server did not start (timeout={self._timeout}). "
1143-
f"Could not open file {omc_portfile_path}. "
1144-
f"Log-file says:\n{self.get_log()}")
1145-
time.sleep(self._timeout / 80.0)
1138+
time.sleep(self._timeout / MAX_RETRIES)
1139+
else:
1140+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1141+
raise OMCSessionException(f"OMC Server did not start (timeout={self._timeout}).")
11461142

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

12261222
docker_process = None
1227-
for _ in range(0, 40):
1223+
MAX_RETRIES = 40
1224+
attempts = 0
1225+
while attempts < MAX_RETRIES:
1226+
attempts += 1
1227+
12281228
docker_top = subprocess.check_output(["docker", "top", docker_cid]).decode().strip()
12291229
docker_process = None
12301230
for line in docker_top.split("\n"):
@@ -1235,10 +1235,12 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]:
12351235
except psutil.NoSuchProcess as ex:
12361236
raise OMCSessionException(f"Could not find PID {docker_top} - "
12371237
"is this a docker instance spawned without --pid=host?") from ex
1238-
12391238
if docker_process is not None:
12401239
break
1241-
time.sleep(self._timeout / 40.0)
1240+
time.sleep(self._timeout / MAX_RETRIES)
1241+
else:
1242+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1243+
raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}).")
12421244

12431245
return docker_process
12441246

@@ -1260,8 +1262,11 @@ def _omc_port_get(self) -> str:
12601262
raise OMCSessionException(f"Invalid docker container ID: {self._docker_container_id}")
12611263

12621264
# See if the omc server is running
1265+
MAX_RETRIES = 80
12631266
attempts = 0
1264-
while True:
1267+
while attempts < MAX_RETRIES:
1268+
attempts += 1
1269+
12651270
omc_portfile_path = self._get_portfile_path()
12661271
if omc_portfile_path is not None:
12671272
try:
@@ -1272,16 +1277,12 @@ def _omc_port_get(self) -> str:
12721277
port = output.decode().strip()
12731278
except subprocess.CalledProcessError:
12741279
pass
1275-
12761280
if port is not None:
12771281
break
1278-
1279-
attempts += 1
1280-
if attempts == 80.0:
1281-
raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}). "
1282-
f"Could not open port file {omc_portfile_path}. "
1283-
f"Log-file says:\n{self.get_log()}")
1284-
time.sleep(self._timeout / 80.0)
1282+
time.sleep(self._timeout / MAX_RETRIES)
1283+
else:
1284+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1285+
raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}).")
12851286

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

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

14511452
docker_cid = None
1452-
for _ in range(0, 40):
1453+
MAX_RETRIES = 40
1454+
attempts = 0
1455+
while attempts < MAX_RETRIES:
1456+
attempts += 1
1457+
14531458
try:
14541459
with open(file=docker_cid_file, mode="r", encoding="utf-8") as fh:
14551460
docker_cid = fh.read().strip()
14561461
except IOError:
14571462
pass
1458-
if docker_cid:
1463+
if docker_cid is not None:
14591464
break
1460-
time.sleep(self._timeout / 40.0)
1461-
1462-
if docker_cid is None:
1465+
time.sleep(self._timeout / MAX_RETRIES)
1466+
else:
14631467
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
14641468
raise OMCSessionException(f"Docker did not start (timeout={self._timeout} might be too short "
14651469
"especially if you did not docker pull the image before this command).")
14661470

14671471
docker_process = self._docker_process_get(docker_cid=docker_cid)
14681472
if docker_process is None:
1469-
raise OMCSessionException(f"Docker top did not contain omc process {self._random_string}. "
1470-
f"Log-file says:\n{self.get_log()}")
1473+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1474+
raise OMCSessionException(f"Docker top did not contain omc process {self._random_string}.")
14711475

14721476
return omc_process, docker_process, docker_cid
14731477

@@ -1623,8 +1627,11 @@ def _omc_port_get(self) -> str:
16231627
port = None
16241628

16251629
# See if the omc server is running
1630+
MAX_RETRIES = 80
16261631
attempts = 0
1627-
while True:
1632+
while attempts < MAX_RETRIES:
1633+
attempts += 1
1634+
16281635
try:
16291636
omc_portfile_path = self._get_portfile_path()
16301637
if omc_portfile_path is not None:
@@ -1635,16 +1642,12 @@ def _omc_port_get(self) -> str:
16351642
port = output.decode().strip()
16361643
except subprocess.CalledProcessError:
16371644
pass
1638-
16391645
if port is not None:
16401646
break
1641-
1642-
attempts += 1
1643-
if attempts == 80.0:
1644-
raise OMCSessionException(f"WSL based OMC Server did not start (timeout={self._timeout}). "
1645-
f"Could not open port file {omc_portfile_path}. "
1646-
f"Log-file says:\n{self.get_log()}")
1647-
time.sleep(self._timeout / 80.0)
1647+
time.sleep(self._timeout / MAX_RETRIES)
1648+
else:
1649+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1650+
raise OMCSessionException(f"WSL based OMC Server did not start (timeout={self._timeout}).")
16481651

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

0 commit comments

Comments
 (0)