Skip to content

Commit d345023

Browse files
committed
[OMCSession*] align all usages of timeout to the same structure
1 parent 26c1b12 commit d345023

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

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

853847
logger.debug("sendExpression(%r, parsed=%r)", command, parsed)
854848

849+
MAX_RETRIES = 50
855850
attempts = 0
856-
while True:
851+
while attempts < MAX_RETRIES:
852+
attempts += 1
853+
857854
try:
858855
self._omc_zmq.send_string(str(command), flags=zmq.NOBLOCK)
859856
break
860857
except zmq.error.Again:
861858
pass
862-
attempts += 1
863-
if attempts >= 50:
864-
# in the deletion process, the content is cleared. Thus, any access to a class attribute must be checked
865-
try:
866-
log_content = self.get_log()
867-
except OMCSessionException:
868-
log_content = 'log not available'
869-
raise OMCSessionException(f"No connection with OMC (timeout={timeout}). "
870-
f"Log-file says: \n{log_content}")
871-
time.sleep(timeout / 50.0)
859+
time.sleep(self._timeout / MAX_RETRIES)
860+
else:
861+
# in the deletion process, the content is cleared. Thus, any access to a class attribute must be checked
862+
try:
863+
log_content = self.get_log()
864+
except OMCSessionException:
865+
log_content = 'log not available'
866+
867+
logger.error(f"Docker did not start. Log-file says:\n{log_content}")
868+
raise OMCSessionException(f"No connection with OMC (timeout={self._timeout}).")
869+
872870
if command == "quit()":
873871
self._omc_zmq.close()
874872
self._omc_zmq = None
@@ -1113,25 +1111,23 @@ def _omc_port_get(self) -> str:
11131111
port = None
11141112

11151113
# See if the omc server is running
1114+
MAX_RETRIES = 80
11161115
attempts = 0
1117-
while True:
1118-
omc_portfile_path = self._get_portfile_path()
1116+
while attempts < MAX_RETRIES:
1117+
attempts += 1
11191118

1119+
omc_portfile_path = self._get_portfile_path()
11201120
if omc_portfile_path is not None and omc_portfile_path.is_file():
11211121
# Read the port file
11221122
with open(file=omc_portfile_path, mode='r', encoding="utf-8") as f_p:
11231123
port = f_p.readline()
11241124
break
1125-
11261125
if port is not None:
11271126
break
1128-
1129-
attempts += 1
1130-
if attempts == 80.0:
1131-
raise OMCSessionException(f"OMC Server did not start (timeout={self._timeout}). "
1132-
f"Could not open file {omc_portfile_path}. "
1133-
f"Log-file says:\n{self.get_log()}")
1134-
time.sleep(self._timeout / 80.0)
1127+
time.sleep(self._timeout / MAX_RETRIES)
1128+
else:
1129+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1130+
raise OMCSessionException(f"OMC Server did not start (timeout={self._timeout}).")
11351131

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

12151211
docker_process = None
1216-
for _ in range(0, 40):
1212+
MAX_RETRIES = 40
1213+
attempts = 0
1214+
while attempts < MAX_RETRIES:
1215+
attempts += 1
1216+
12171217
docker_top = subprocess.check_output(["docker", "top", docker_cid]).decode().strip()
12181218
docker_process = None
12191219
for line in docker_top.split("\n"):
@@ -1224,10 +1224,12 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]:
12241224
except psutil.NoSuchProcess as ex:
12251225
raise OMCSessionException(f"Could not find PID {docker_top} - "
12261226
"is this a docker instance spawned without --pid=host?") from ex
1227-
12281227
if docker_process is not None:
12291228
break
1230-
time.sleep(self._timeout / 40.0)
1229+
time.sleep(self._timeout / MAX_RETRIES)
1230+
else:
1231+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1232+
raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}).")
12311233

12321234
return docker_process
12331235

@@ -1249,8 +1251,11 @@ def _omc_port_get(self) -> str:
12491251
raise OMCSessionException(f"Invalid docker container ID: {self._docker_container_id}")
12501252

12511253
# See if the omc server is running
1254+
MAX_RETRIES = 80
12521255
attempts = 0
1253-
while True:
1256+
while attempts < MAX_RETRIES:
1257+
attempts += 1
1258+
12541259
omc_portfile_path = self._get_portfile_path()
12551260
if omc_portfile_path is not None:
12561261
try:
@@ -1261,16 +1266,12 @@ def _omc_port_get(self) -> str:
12611266
port = output.decode().strip()
12621267
except subprocess.CalledProcessError:
12631268
pass
1264-
12651269
if port is not None:
12661270
break
1267-
1268-
attempts += 1
1269-
if attempts == 80.0:
1270-
raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}). "
1271-
f"Could not open port file {omc_portfile_path}. "
1272-
f"Log-file says:\n{self.get_log()}")
1273-
time.sleep(self._timeout / 80.0)
1271+
time.sleep(self._timeout / MAX_RETRIES)
1272+
else:
1273+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1274+
raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}).")
12741275

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

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

14401441
docker_cid = None
1441-
for _ in range(0, 40):
1442+
MAX_RETRIES = 40
1443+
attempts = 0
1444+
while attempts < MAX_RETRIES:
1445+
attempts += 1
1446+
14421447
try:
14431448
with open(file=docker_cid_file, mode="r", encoding="utf-8") as fh:
14441449
docker_cid = fh.read().strip()
14451450
except IOError:
14461451
pass
1447-
if docker_cid:
1452+
if docker_cid is not None:
14481453
break
1449-
time.sleep(self._timeout / 40.0)
1450-
1451-
if docker_cid is None:
1454+
time.sleep(self._timeout / MAX_RETRIES)
1455+
else:
14521456
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
14531457
raise OMCSessionException(f"Docker did not start (timeout={self._timeout} might be too short "
14541458
"especially if you did not docker pull the image before this command).")
14551459

14561460
docker_process = self._docker_process_get(docker_cid=docker_cid)
14571461
if docker_process is None:
1458-
raise OMCSessionException(f"Docker top did not contain omc process {self._random_string}. "
1459-
f"Log-file says:\n{self.get_log()}")
1462+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1463+
raise OMCSessionException(f"Docker top did not contain omc process {self._random_string}.")
14601464

14611465
return omc_process, docker_process, docker_cid
14621466

@@ -1612,8 +1616,11 @@ def _omc_port_get(self) -> str:
16121616
port = None
16131617

16141618
# See if the omc server is running
1619+
MAX_RETRIES = 80
16151620
attempts = 0
1616-
while True:
1621+
while attempts < MAX_RETRIES:
1622+
attempts += 1
1623+
16171624
try:
16181625
omc_portfile_path = self._get_portfile_path()
16191626
if omc_portfile_path is not None:
@@ -1624,16 +1631,12 @@ def _omc_port_get(self) -> str:
16241631
port = output.decode().strip()
16251632
except subprocess.CalledProcessError:
16261633
pass
1627-
16281634
if port is not None:
16291635
break
1630-
1631-
attempts += 1
1632-
if attempts == 80.0:
1633-
raise OMCSessionException(f"WSL based OMC Server did not start (timeout={self._timeout}). "
1634-
f"Could not open port file {omc_portfile_path}. "
1635-
f"Log-file says:\n{self.get_log()}")
1636-
time.sleep(self._timeout / 80.0)
1636+
time.sleep(self._timeout / MAX_RETRIES)
1637+
else:
1638+
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
1639+
raise OMCSessionException(f"WSL based OMC Server did not start (timeout={self._timeout}).")
16371640

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

0 commit comments

Comments
 (0)