Skip to content

Commit b2f265d

Browse files
committed
fixing timeout for container launch
1 parent 13969e6 commit b2f265d

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

src/ansys/fluent/core/launcher/container_launcher.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,17 @@ def __call__(self):
216216

217217
if is_compose():
218218
port, config_dict, container = start_fluent_container(
219-
self._args, self.argvals["container_dict"]
219+
self._args,
220+
self.argvals["container_dict"],
221+
self.argvals["start_timeout"],
220222
)
221223

222224
_, _, password = _get_server_info_from_container(config_dict=config_dict)
223225
else:
224226
port, password, container = start_fluent_container(
225-
self._args, self.argvals["container_dict"]
227+
self._args,
228+
self.argvals["container_dict"],
229+
self.argvals["start_timeout"],
226230
)
227231

228232
fluent_connection = FluentConnection(

src/ansys/fluent/core/launcher/error_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class LaunchFluentError(Exception):
7070

7171
def __init__(self, launch_string):
7272
"""__init__ method of LaunchFluentError class."""
73-
details = "\n" + "Fluent Launch string: " + launch_string
73+
details = "\n" + "Fluent Launch command: " + launch_string
7474
super().__init__(details)
7575

7676

src/ansys/fluent/core/launcher/fluent_container.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080

8181
import ansys.fluent.core as pyfluent
8282
from ansys.fluent.core.docker.docker_compose import ComposeBasedLauncher
83+
from ansys.fluent.core.launcher.error_handler import (
84+
LaunchFluentError,
85+
)
8386
from ansys.fluent.core.launcher.launcher_utils import is_compose
8487
from ansys.fluent.core.session import _parse_server_info_file
8588
from ansys.fluent.core.utils.deprecate import all_deprecators
@@ -155,7 +158,6 @@ def configure_container_dict(
155158
args: List[str],
156159
mount_source: str | Path | None = None,
157160
mount_target: str | Path | None = None,
158-
timeout: int | None = None,
159161
port: int | None = None,
160162
license_server: str | None = None,
161163
container_server_info_file: str | Path | None = None,
@@ -179,8 +181,6 @@ def configure_container_dict(
179181
mount_target : str | Path, optional
180182
Path inside the container where ``mount_source`` will be mounted. This will be the working directory path
181183
visible to the Fluent process running inside the container.
182-
timeout : int, optional
183-
Time limit for the Fluent container to start, in seconds. By default, 60 seconds.
184184
port : int, optional
185185
Port for Fluent container to use.
186186
license_server : str, optional
@@ -206,7 +206,6 @@ def configure_container_dict(
206206
-------
207207
fluent_image : str
208208
container_dict : dict
209-
timeout : int
210209
port : int
211210
host_server_info_file : Path
212211
remove_server_info_file: bool
@@ -230,8 +229,6 @@ def configure_container_dict(
230229
231230
See also :func:`start_fluent_container`.
232231
"""
233-
if timeout is None:
234-
timeout = int(os.getenv("PYFLUENT_FLUENT_LAUNCH_TIMEOUT", "60"))
235232

236233
logger.debug(f"container_dict before processing:\n{dict_to_str(container_dict)}")
237234

@@ -446,23 +443,22 @@ def configure_container_dict(
446443
container_dict["mount_target"] = mount_target
447444

448445
logger.debug(
449-
f"Fluent container timeout: {timeout}, container_grpc_port: {container_grpc_port}, "
446+
f"Fluent container container_grpc_port: {container_grpc_port}, "
450447
f"host_server_info_file: '{host_server_info_file}', "
451448
f"remove_server_info_file: {remove_server_info_file}"
452449
)
453450
logger.debug(f"container_dict after processing:\n{dict_to_str(container_dict)}")
454451

455452
return (
456453
container_dict,
457-
timeout,
458454
container_grpc_port,
459455
host_server_info_file,
460456
remove_server_info_file,
461457
)
462458

463459

464460
def start_fluent_container(
465-
args: List[str], container_dict: dict | None = None
461+
args: List[str], container_dict: dict | None = None, start_timeout: int = 60
466462
) -> tuple[int, str, Any]:
467463
"""Start a Fluent container.
468464
@@ -472,6 +468,9 @@ def start_fluent_container(
472468
List of Fluent launch arguments.
473469
container_dict : dict, optional
474470
Dictionary with Docker container configuration.
471+
start_timeout : int, optional
472+
Timeout in seconds for the container to start. If not specified, it defaults to 60
473+
seconds.
475474
476475
Returns
477476
-------
@@ -500,11 +499,11 @@ def start_fluent_container(
500499

501500
(
502501
config_dict,
503-
timeout,
504502
port,
505503
host_server_info_file,
506504
remove_server_info_file,
507505
) = container_vars
506+
launch_string = " ".join(config_dict["command"])
508507

509508
try:
510509
if is_compose():
@@ -538,20 +537,26 @@ def start_fluent_container(
538537
config_dict.pop("fluent_image"), **config_dict
539538
)
540539

541-
logger.debug(f"Waiting for container for up to {timeout} seconds...")
540+
logger.debug(
541+
f"Waiting for Fluent container for up to {start_timeout} seconds..."
542+
)
542543

543544
success = timeout_loop(
544-
lambda: host_server_info_file.stat().st_mtime > last_mtime, timeout
545+
lambda: host_server_info_file.stat().st_mtime > last_mtime,
546+
start_timeout,
545547
)
546548

547549
if not success:
548550
raise TimeoutError(
549-
f"Fluent container launch has timed out after {timeout} seconds. Container will need to be stopped manually."
551+
f"Fluent container launch has timed out after {start_timeout} seconds. Container will need to be stopped manually."
550552
)
551553
else:
552554
_, _, password = _parse_server_info_file(str(host_server_info_file))
553555

554556
return port, password, container
557+
except Exception as ex:
558+
logger.error(f"Exception caught - {type(ex).__name__}: {ex}")
559+
raise LaunchFluentError(launch_string) from ex
555560
finally:
556561
if remove_server_info_file and host_server_info_file.exists():
557562
host_server_info_file.unlink()

src/ansys/fluent/core/launcher/launcher.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,8 @@ def launch_fluent(
207207
The string path to a Fluent journal file, or a list of such paths. Fluent will execute the
208208
journal(s). The default is ``None``.
209209
start_timeout : int, optional
210-
Maximum allowable time in seconds for connecting to the Fluent
211-
server. The default is ``60`` if Fluent is launched outside a Slurm environment,
212-
no timeout if Fluent is launched within a Slurm environment.
210+
Maximum allowable time, in seconds, to connect to the Fluent server after launching it,
211+
before raising a timeout error. The default is ``60`` seconds.
213212
additional_arguments : str, optional
214213
Additional arguments to send to Fluent as a string in the same
215214
format they are normally passed to Fluent on the command line.
@@ -323,6 +322,9 @@ def launch_fluent(
323322
if env is None:
324323
env = {}
325324

325+
if start_timeout is None:
326+
start_timeout = int(os.getenv("PYFLUENT_FLUENT_LAUNCH_TIMEOUT", "60"))
327+
326328
def _mode_to_launcher_type(fluent_launch_mode: LaunchMode):
327329
launcher_mode_type = {
328330
LaunchMode.CONTAINER: DockerLauncher,

tests/test_launcher.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,10 @@ def test_mode():
8787
)
8888

8989

90-
@pytest.mark.standalone
9190
def test_unsuccessful_fluent_connection():
92-
# start-timeout is intentionally provided to be 2s for the connection to fail
91+
# start-timeout is intentionally provided to be 1s for the connection to fail
9392
with pytest.raises(LaunchFluentError) as ex:
94-
pyfluent.launch_fluent(mode="solver", start_timeout=2)
93+
pyfluent.launch_fluent(mode="solver", start_timeout=1)
9594
# TimeoutError -> LaunchFluentError
9695
assert isinstance(ex.value.__context__, TimeoutError)
9796

0 commit comments

Comments
 (0)