Skip to content

Commit 04fe7b3

Browse files
committed
[OMCProcessDocker] improve handling of docker_cid
1 parent 4501729 commit 04fe7b3

1 file changed

Lines changed: 25 additions & 20 deletions

File tree

OMPython/OMCSession.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -700,10 +700,8 @@ def __init__(
700700

701701
self._interactivePort = port
702702

703-
self._dockerCidFile: Optional[pathlib.Path] = None
704-
705703
# start up omc executable in docker container waiting for the ZMQ connection
706-
self._omc_process, self._docker_process = self._omc_docker_start()
704+
self._omc_process, self._docker_process, self._dockerCid = self._omc_docker_start()
707705
# connect to the running omc instance using ZMQ
708706
self._omc_port = self._omc_port_get()
709707

@@ -723,7 +721,11 @@ def __del__(self) -> None:
723721
finally:
724722
self._docker_process = None
725723

726-
def _omc_command_docker(self, omc_path_and_args_list) -> list:
724+
def _omc_command_docker(
725+
self,
726+
omc_path_and_args_list: list[str],
727+
docker_cid_file: pathlib.Path,
728+
) -> list:
727729
"""
728730
Define the command that will be called by the subprocess module.
729731
"""
@@ -753,13 +755,11 @@ def _omc_command_docker(self, omc_path_and_args_list) -> list:
753755
raise OMCSessionException(f'dockerNetwork was set to {self._dockerNetwork}, '
754756
'but only \"host\" or \"separate\" is allowed')
755757

756-
self._dockerCidFile = self._temp_dir / (self._omc_filebase + ".docker.cid")
757-
758758
if isinstance(self._interactivePort, int):
759759
extraFlags = extraFlags + [f"--interactivePort={int(self._interactivePort)}"]
760760

761761
omc_command = (["docker", "run",
762-
"--cidfile", self._dockerCidFile.as_posix(),
762+
"--cidfile", docker_cid_file.as_posix(),
763763
"--rm",
764764
"--env", f"USER={self._currentUser}",
765765
"--user", str(self._getuid())]
@@ -806,46 +806,51 @@ def _omc_port_get(self) -> str:
806806

807807
return port
808808

809-
def _omc_docker_start(self) -> Tuple[subprocess.Popen, DummyPopen]:
809+
def _omc_docker_start(self) -> Tuple[subprocess.Popen, DummyPopen, str]:
810810
my_env = os.environ.copy()
811811
my_env["USER"] = self._currentUser
812812

813-
omc_command = self._omc_command_docker(omc_path_and_args_list=["--locale=C",
814-
"--interactive=zmq",
815-
f"-z={self._random_string}"])
813+
docker_cid_file = self._temp_dir / (self._omc_filebase + ".docker.cid")
814+
815+
omc_command = self._omc_command_docker(
816+
omc_path_and_args_list=["--locale=C",
817+
"--interactive=zmq",
818+
f"-z={self._random_string}"],
819+
docker_cid_file=docker_cid_file,
820+
)
816821

817822
omc_process = subprocess.Popen(omc_command,
818823
stdout=self._omc_loghandle,
819824
stderr=self._omc_loghandle,
820825
env=my_env)
821826

822-
if not isinstance(self._dockerCidFile, pathlib.Path):
823-
raise OMCSessionException(f"Invalid content for docker container ID file path: {self._dockerCidFile}")
827+
if not isinstance(docker_cid_file, pathlib.Path):
828+
raise OMCSessionException(f"Invalid content for docker container ID file path: {docker_cid_file}")
824829

830+
docker_cid = None
825831
for idx in range(0, 40):
826832
try:
827-
with open(file=self._dockerCidFile, mode="r", encoding="utf-8") as fh:
828-
content = fh.read().strip()
829-
self._dockerCid = content
833+
with open(file=docker_cid_file, mode="r", encoding="utf-8") as fh:
834+
docker_cid = fh.read().strip()
830835
except IOError:
831836
pass
832-
if self._dockerCid:
837+
if docker_cid:
833838
break
834839
time.sleep(self._timeout / 40.0)
835840

836-
if self._dockerCid is None:
841+
if docker_cid is None:
837842
logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}")
838843
raise OMCSessionException(f"Docker did not start (timeout={self._timeout} might be too short "
839844
"especially if you did not docker pull the image before this command).")
840845

841-
docker_process = self._omc_process_docker(dockerCid=self._dockerCid,
846+
docker_process = self._omc_process_docker(dockerCid=docker_cid,
842847
random_string=self._random_string,
843848
timeout=self._timeout)
844849
if docker_process is None:
845850
raise OMCSessionException(f"Docker top did not contain omc process {self._random_string}. "
846851
f"Log-file says:\n{self.get_log()}")
847852

848-
return omc_process, docker_process
853+
return omc_process, docker_process, docker_cid
849854

850855

851856
class OMCProcessDockerContainer(OMCProcessDockerHelper, OMCProcess):

0 commit comments

Comments
 (0)