Skip to content

fix: Raising ValueError when using ips within pool library #3240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5191109
refactor: create_temp_dir function
germa89 Jul 2, 2024
c420147
feat: using create_temp_dir in mapdlpool
germa89 Jul 2, 2024
d7e592b
docs: API typo
germa89 Jul 2, 2024
098e115
test: adding unit test
germa89 Jul 2, 2024
c1748d1
chore: adding changelog file 3239.miscellaneous.md
pyansys-ci-bot Jul 2, 2024
8ed9c48
chore: adding changelog file 3239.miscellaneous.md
pyansys-ci-bot Jul 2, 2024
8cc8c13
Apply suggestions from code review
germa89 Jul 2, 2024
1e2a37e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 2, 2024
7d55e60
fix: Avoid raising error when using `launch_mapdl` within an `MapdlPo…
germa89 Jul 3, 2024
05988aa
Merge branch 'feat/refactoring-create_temp_dir' into fix/raising-valu…
germa89 Jul 3, 2024
8836a0c
feat: being more verbose about the errors raised by `protect_grpc`.
germa89 Jul 3, 2024
95f38fe
fix: dict not callable
germa89 Jul 3, 2024
2196be8
chore: adding changelog file 3240.fixed.md
pyansys-ci-bot Jul 3, 2024
d7cc7a3
fix: wrong variable name
germa89 Jul 8, 2024
b43ecd4
docs: fix spelling
germa89 Jul 8, 2024
c87f2d9
Merge branch 'fix/raising-valueerror-when-using-ips-within-pool-libra…
germa89 Jul 8, 2024
06dc3e0
Merge branch 'main' into fix/raising-valueerror-when-using-ips-within…
germa89 Jul 8, 2024
7731121
Merge branch 'main' into fix/raising-valueerror-when-using-ips-within…
germa89 Jul 8, 2024
3bd848b
chore: adding changelog file 3240.fixed.md
pyansys-ci-bot Jul 8, 2024
4aba9d8
fix: removing unused variable
germa89 Jul 8, 2024
b5bc10b
chore: Merge branch 'main' into fix/raising-valueerror-when-using-ips…
germa89 Jul 9, 2024
709c50f
Merge branch 'main' into fix/raising-valueerror-when-using-ips-within…
germa89 Jul 10, 2024
476c336
Merge branch 'main' into fix/raising-valueerror-when-using-ips-within…
germa89 Jul 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/3239.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: refactoring `create_temp_dir`
1 change: 1 addition & 0 deletions doc/changelog.d/3240.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix: Raising `ValueError` when using ips within pool library
4 changes: 2 additions & 2 deletions doc/source/api/pool.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.. _ref_pool_api:

Local MAPDL pool
================
MAPDL pool
==========

.. currentmodule:: ansys.mapdl.core

Expand Down
13 changes: 12 additions & 1 deletion src/ansys/mapdl/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,17 @@
except:
class_name = ""

# trying to get "cmd" argument:
cmd = args[1] if len(args) >= 2 else ""
cmd = kwargs.get("cmd", cmd)

caller = func.__name__

if cmd:
msg_ = f"running:\n{cmd}\ncalled by:\n{caller}"
else:
msg_ = f"calling:{caller}\nwith the following arguments:\nargs: {list(*args)}\nkwargs: {list(**kwargs_)}"

Check warning on line 321 in src/ansys/mapdl/core/errors.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/errors.py#L321

Added line #L321 was not covered by tests

if class_name == "MapdlGrpc":
mapdl = args[0]
elif hasattr(args[0], "_mapdl"):
Expand All @@ -317,7 +328,7 @@
# Must close unfinished processes
mapdl._close_process()
raise MapdlExitedError(
f"MAPDL server connection terminated with the following error\n{error}"
f"MAPDL server connection terminated unexpectedly while {msg_}\nwith the following error\n{error}"
) from None

if threading.current_thread().__class__.__name__ == "_MainThread":
Expand Down
3 changes: 2 additions & 1 deletion src/ansys/mapdl/core/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,7 @@ def launch_mapdl(
)
use_vtk = kwargs.pop("use_vtk", None)
just_launch = kwargs.pop("just_launch", None)
on_pool = kwargs.pop("on_pool", False)
_debug_no_launch = kwargs.pop("_debug_no_launch", None)

# Transferring MAPDL arguments to start_parameters:
Expand Down Expand Up @@ -1532,7 +1533,7 @@ def launch_mapdl(
if ON_WSL:
LOG.debug("On WSL: Allowing 'start_instance' and 'ip' arguments together.")
else:
if start_instance is True:
if start_instance is True and not on_pool:
raise ValueError(
"When providing a value for the argument 'ip', the argument "
"'start_instance' cannot be 'True'.\n"
Expand Down
40 changes: 17 additions & 23 deletions src/ansys/mapdl/core/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,33 +519,27 @@ def create_temp_dir(tmpdir=None, name=None):
"""Create a new unique directory at a given temporary directory"""
if tmpdir is None:
tmpdir = tempfile.gettempdir()
elif not os.path.isdir(tmpdir):
os.makedirs(tmpdir)

if not name:
random_name = True
letters_ = string.ascii_lowercase.replace("n", "")
name = random_string(10, letters_)
else:
random_name = False
# Possible letters
letters_ = string.ascii_lowercase.replace("n", "")

# running into a rare issue with MAPDL on Windows with "\n" being
# treated literally.
path = os.path.join(tmpdir, name)
def get_name():
return random_string(10, letters_)

if random_name:
# in the *rare* case of a duplicate path
while os.path.isdir(path):
path = os.path.join(tempfile.gettempdir(), name)
name = name or get_name()
while os.path.exists(os.path.join(tmpdir, name)):
name = get_name()

if not os.path.exists(path):
try:
os.mkdir(path)
except:
raise MapdlRuntimeError(
"Unable to create temporary working "
f"directory {path}\nPlease specify 'run_location' argument"
)
# create dir:
path = os.path.join(tmpdir, name)

try:
os.mkdir(path)
except: # pragma: no cover
raise MapdlRuntimeError(
"Unable to create temporary working "
f"directory {path}.\nSpecify 'run_location' argument"
)

return path

Expand Down
4 changes: 2 additions & 2 deletions src/ansys/mapdl/core/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import os
import shutil
import socket
import tempfile
import time
from typing import Any, Dict, List, Optional, Union
import warnings
Expand Down Expand Up @@ -219,7 +218,7 @@
_debug_no_launch = kwargs.pop("_debug_no_launch", None)

if run_location is None:
run_location = tempfile.gettempdir()
run_location = create_temp_dir()

Check warning on line 221 in src/ansys/mapdl/core/pool.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/pool.py#L221

Added line #L221 was not covered by tests
self._root_dir: str = run_location

kwargs["remove_temp_files"] = remove_temp_files
Expand Down Expand Up @@ -875,6 +874,7 @@
ip=ip,
override=True,
start_instance=start_instance,
on_pool=True,
**self._spawn_kwargs,
)

Expand Down
14 changes: 14 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"""Small or misc tests that don't fit in other test modules"""
import inspect
import os
import pathlib

import numpy as np
import pytest
Expand All @@ -37,6 +38,7 @@
check_valid_ip,
check_valid_port,
check_valid_routine,
create_temp_dir,
last_created,
load_file,
no_return,
Expand Down Expand Up @@ -376,3 +378,15 @@ def test_check_valid_routine():
assert check_valid_routine("begin level")
with pytest.raises(ValueError, match="Invalid routine"):
check_valid_routine("invalid")


@requires("local")
def test_create_temp_dir():

path = create_temp_dir()

path = pathlib.Path(path)
parent = path.parent
dir_ = path.parts[-1]

assert str(path) != create_temp_dir(parent, dir_)
Loading