Skip to content

Commit fb568de

Browse files
committed
[OMCSession*] align all usages of timeout to the same structure
1 parent 12eef69 commit fb568de

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
@@ -839,34 +839,32 @@ def sendExpression(self, command: str, parsed: bool = True) -> Any:
839839
Caller should only check for OMCSessionException.
840840
"""
841841

842-
# this is needed if the class is not fully initialized or in the process of deletion
843-
if hasattr(self, '_timeout'):
844-
timeout = self._timeout
845-
else:
846-
timeout = 1.0
847-
848842
if self._omc_zmq is None:
849843
raise OMCSessionException("No OMC running. Please create a new instance of OMCSession!")
850844

851845
logger.debug("sendExpression(%r, parsed=%r)", command, parsed)
852846

847+
MAX_RETRIES = 50
853848
attempts = 0
854-
while True:
849+
while attempts < MAX_RETRIES:
850+
attempts += 1
851+
855852
try:
856853
self._omc_zmq.send_string(str(command), flags=zmq.NOBLOCK)
857854
break
858855
except zmq.error.Again:
859856
pass
860-
attempts += 1
861-
if attempts >= 50:
862-
# in the deletion process, the content is cleared. Thus, any access to a class attribute must be checked
863-
try:
864-
log_content = self.get_log()
865-
except OMCSessionException:
866-
log_content = 'log not available'
867-
raise OMCSessionException(f"No connection with OMC (timeout={timeout}). "
868-
f"Log-file says: \n{log_content}")
869-
time.sleep(timeout / 50.0)
857+
time.sleep(self._timeout / MAX_RETRIES)
858+
else:
859+
# in the deletion process, the content is cleared. Thus, any access to a class attribute must be checked
860+
try:
861+
log_content = self.get_log()
862+
except OMCSessionException:
863+
log_content = 'log not available'
864+
865+
logger.error(f"Docker did not start. Log-file says:\n{log_content}")
866+
raise OMCSessionException(f"No connection with OMC (timeout={self._timeout}).")
867+
870868
if command == "quit()":
871869
self._omc_zmq.close()
872870
self._omc_zmq = None
@@ -1095,25 +1093,23 @@ def _omc_port_get(self) -> str:
10951093
port = None
10961094

10971095
# See if the omc server is running
1096+
MAX_RETRIES = 80
10981097
attempts = 0
1099-
while True:
1100-
omc_portfile_path = self._get_portfile_path()
1098+
while attempts < MAX_RETRIES:
1099+
attempts += 1
11011100

1101+
omc_portfile_path = self._get_portfile_path()
11021102
if omc_portfile_path is not None and omc_portfile_path.is_file():
11031103
# Read the port file
11041104
with open(file=omc_portfile_path, mode='r', encoding="utf-8") as f_p:
11051105
port = f_p.readline()
11061106
break
1107-
11081107
if port is not None:
11091108
break
1110-
1111-
attempts += 1
1112-
if attempts == 80.0:
1113-
raise OMCSessionException(f"OMC Server did not start (timeout={self._timeout}). "
1114-
f"Could not open file {omc_portfile_path}. "
1115-
f"Log-file says:\n{self.get_log()}")
1116-
time.sleep(self._timeout / 80.0)
1109+
time.sleep(self._timeout / MAX_RETRIES)
1110+
else:
1111+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1112+
raise OMCSessionException(f"OMC Server did not start (timeout={self._timeout}).")
11171113

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

11971193
docker_process = None
1198-
for _ in range(0, 40):
1194+
MAX_RETRIES = 40
1195+
attempts = 0
1196+
while attempts < MAX_RETRIES:
1197+
attempts += 1
1198+
11991199
docker_top = subprocess.check_output(["docker", "top", docker_cid]).decode().strip()
12001200
docker_process = None
12011201
for line in docker_top.split("\n"):
@@ -1206,10 +1206,12 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]:
12061206
except psutil.NoSuchProcess as ex:
12071207
raise OMCSessionException(f"Could not find PID {docker_top} - "
12081208
"is this a docker instance spawned without --pid=host?") from ex
1209-
12101209
if docker_process is not None:
12111210
break
1212-
time.sleep(self._timeout / 40.0)
1211+
time.sleep(self._timeout / MAX_RETRIES)
1212+
else:
1213+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1214+
raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}).")
12131215

12141216
return docker_process
12151217

@@ -1231,8 +1233,11 @@ def _omc_port_get(self) -> str:
12311233
raise OMCSessionException(f"Invalid docker container ID: {self._docker_container_id}")
12321234

12331235
# See if the omc server is running
1236+
MAX_RETRIES = 80
12341237
attempts = 0
1235-
while True:
1238+
while attempts < MAX_RETRIES:
1239+
attempts += 1
1240+
12361241
omc_portfile_path = self._get_portfile_path()
12371242
if omc_portfile_path is not None:
12381243
try:
@@ -1243,16 +1248,12 @@ def _omc_port_get(self) -> str:
12431248
port = output.decode().strip()
12441249
except subprocess.CalledProcessError:
12451250
pass
1246-
12471251
if port is not None:
12481252
break
1249-
1250-
attempts += 1
1251-
if attempts == 80.0:
1252-
raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}). "
1253-
f"Could not open port file {omc_portfile_path}. "
1254-
f"Log-file says:\n{self.get_log()}")
1255-
time.sleep(self._timeout / 80.0)
1253+
time.sleep(self._timeout / MAX_RETRIES)
1254+
else:
1255+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1256+
raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}).")
12561257

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

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

14221423
docker_cid = None
1423-
for _ in range(0, 40):
1424+
MAX_RETRIES = 40
1425+
attempts = 0
1426+
while attempts < MAX_RETRIES:
1427+
attempts += 1
1428+
14241429
try:
14251430
with open(file=docker_cid_file, mode="r", encoding="utf-8") as fh:
14261431
docker_cid = fh.read().strip()
14271432
except IOError:
14281433
pass
1429-
if docker_cid:
1434+
if docker_cid is not None:
14301435
break
1431-
time.sleep(self._timeout / 40.0)
1432-
1433-
if docker_cid is None:
1436+
time.sleep(self._timeout / MAX_RETRIES)
1437+
else:
14341438
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
14351439
raise OMCSessionException(f"Docker did not start (timeout={self._timeout} might be too short "
14361440
"especially if you did not docker pull the image before this command).")
14371441

14381442
docker_process = self._docker_process_get(docker_cid=docker_cid)
14391443
if docker_process is None:
1440-
raise OMCSessionException(f"Docker top did not contain omc process {self._random_string}. "
1441-
f"Log-file says:\n{self.get_log()}")
1444+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1445+
raise OMCSessionException(f"Docker top did not contain omc process {self._random_string}.")
14421446

14431447
return omc_process, docker_process, docker_cid
14441448

@@ -1594,8 +1598,11 @@ def _omc_port_get(self) -> str:
15941598
port = None
15951599

15961600
# See if the omc server is running
1601+
MAX_RETRIES = 80
15971602
attempts = 0
1598-
while True:
1603+
while attempts < MAX_RETRIES:
1604+
attempts += 1
1605+
15991606
try:
16001607
omc_portfile_path = self._get_portfile_path()
16011608
if omc_portfile_path is not None:
@@ -1606,16 +1613,12 @@ def _omc_port_get(self) -> str:
16061613
port = output.decode().strip()
16071614
except subprocess.CalledProcessError:
16081615
pass
1609-
16101616
if port is not None:
16111617
break
1612-
1613-
attempts += 1
1614-
if attempts == 80.0:
1615-
raise OMCSessionException(f"WSL based OMC Server did not start (timeout={self._timeout}). "
1616-
f"Could not open port file {omc_portfile_path}. "
1617-
f"Log-file says:\n{self.get_log()}")
1618-
time.sleep(self._timeout / 80.0)
1618+
time.sleep(self._timeout / MAX_RETRIES)
1619+
else:
1620+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1621+
raise OMCSessionException(f"WSL based OMC Server did not start (timeout={self._timeout}).")
16191622

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

0 commit comments

Comments
 (0)