Skip to content

Commit

Permalink
feat: avoiding reconnecting if MAPDL exited already (#3708)
Browse files Browse the repository at this point in the history
* test: refactor check_stds and post_mortem_checks

* test: backing off algorithm

* chore: adding changelog file 3703.added.md [dependabot-skip]

* fix: codacity warnings

* feat: using get_value to obtain the n elements

* revert: revert "feat: using get_value to obtain the n elements"
Performance is not as go

This reverts commit 877f803.

* feat: using get_value to obtain the n elements

* revert: revert "feat: using get_value to obtain the n elements"
Performance is not as go

This reverts commit 877f803.

* feat: using mapdl.exit when raising final error.

* test: fix test by avoiding killing mapdl.

* fix: test

* fix: test

* feat: adding warnings when restarting MAPDL during testing

* fix: test

* feat: caching requires_package

* test: adding more tests

* chore: adding changelog file 3705.added.md [dependabot-skip]

* chore: adding changelog file 3705.added.md [dependabot-skip]

* fix: warnings import

* feat: not reconnecting if MAPDL already exited

* test: adding tests

* chore: adding changelog file 3708.miscellaneous.md [dependabot-skip]

* fix: tests

---------

Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com>
  • Loading branch information
germa89 and pyansys-ci-bot authored Jan 27, 2025
1 parent 9d63421 commit 521ee2f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/3708.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: avoiding reconnecting if MAPDL exited already
32 changes: 17 additions & 15 deletions src/ansys/mapdl/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,27 +327,29 @@ def wrapper(*args, **kwargs):

except grpc.RpcError as error:
mapdl = retrieve_mapdl_from_args(args)

mapdl._log.debug("A gRPC error has been detected.")

i_attemps += 1
if i_attemps <= n_attempts:
if not mapdl.exited:
i_attemps += 1
if i_attemps <= n_attempts:

wait = (
initial_backoff * multiplier_backoff**i_attemps
) # Exponential backoff
wait = (
initial_backoff * multiplier_backoff**i_attemps
) # Exponential backoff

# reconnect
mapdl._log.debug(
f"Re-connection attempt {i_attemps} after waiting {wait:0.3f} seconds"
)
# reconnect
mapdl._log.debug(
f"Re-connection attempt {i_attemps} after waiting {wait:0.3f} seconds"
)

if not mapdl.is_alive:
connected = mapdl._connect(timeout=wait)
else:
sleep(wait)
if not mapdl.is_alive:
connected = mapdl._connect(timeout=wait)
else:
sleep(wait)

# Retry again
continue
# Retry again
continue

# Custom errors
reason = ""
Expand Down
1 change: 1 addition & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def test_launch_mapdl_cli(monkeypatch, run_cli, start_instance):
assert pid != 0


@requires("click")
@pytest.mark.parametrize(
"mapping",
((False, True), (False, True, False), (False, False, False), (True, True, False)),
Expand Down
22 changes: 18 additions & 4 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2749,7 +2749,8 @@ def test_comment_on_debug_mode(mapdl, cleared):

@patch("ansys.mapdl.core.errors.N_ATTEMPTS", 2)
@patch("ansys.mapdl.core.errors.MULTIPLIER_BACKOFF", 1)
def test_timeout_when_exiting(mapdl):
@pytest.mark.parametrize("is_exited", [True, False])
def test_timeout_when_exiting(mapdl, is_exited):
from ansys.mapdl.core import errors

def raise_exception(*args, **kwargs):
Expand All @@ -2759,9 +2760,13 @@ def raise_exception(*args, **kwargs):
e.code = lambda: grpc.StatusCode.ABORTED
e.details = lambda: "My gRPC error details"

# Simulating MAPDL exiting by force
mapdl._exited = is_exited

raise e

handle_generic_grpc_error = errors.handle_generic_grpc_error

with (
patch("ansys.mapdl.core.mapdl_grpc.pb_types.CmdRequest") as mock_cmdrequest,
patch(
Expand All @@ -2787,9 +2792,17 @@ def raise_exception(*args, **kwargs):
assert mapdl._exited

assert mock_handle.call_count == 1
assert mock_connect.call_count == errors.N_ATTEMPTS
assert mock_cmdrequest.call_count == errors.N_ATTEMPTS + 1
assert mock_is_alive.call_count == errors.N_ATTEMPTS + 1

if is_exited:
# Checking no trying to reconnect
assert mock_connect.call_count == 0
assert mock_cmdrequest.call_count == 1
assert mock_is_alive.call_count == 1

else:
assert mock_connect.call_count == errors.N_ATTEMPTS
assert mock_cmdrequest.call_count == errors.N_ATTEMPTS + 1
assert mock_is_alive.call_count == errors.N_ATTEMPTS + 1

mapdl._exited = False

Expand Down Expand Up @@ -2838,6 +2851,7 @@ def test_none_on_selecting(mapdl, cleared, func):
assert len(selfunc(None)) == 0


@requires("pyvista")
def test_requires_package_speed():
from ansys.mapdl.core.misc import requires_package

Expand Down

0 comments on commit 521ee2f

Please sign in to comment.