Skip to content

Commit 628dde1

Browse files
germa89pyansys-ci-botsourcery-ai[bot]
authored
feat: exposing a reconnection method (#3933)
* exposing a reconnection method * chore: adding changelog file 3933.miscellaneous.md [dependabot-skip] * Update src/ansys/mapdl/core/mapdl_grpc.py Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> * chore: adding changelog file 3933.miscellaneous.md [dependabot-skip] --------- Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
1 parent 194c0c6 commit 628dde1

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

doc/changelog.d/3933.miscellaneous.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
feat: exposing a reconnection method

src/ansys/mapdl/core/mapdl_grpc.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -464,18 +464,7 @@ def __init__(
464464
if not self._mapdl_on_hpc and self._mapdl_process:
465465
self._create_process_stds_queue()
466466

467-
try:
468-
self._multi_connect(timeout=timeout)
469-
except MapdlConnectionError as err: # pragma: no cover
470-
self._post_mortem_checks()
471-
self._log.debug(
472-
"The error wasn't caught by the post-mortem checks.\nThe stdout is printed now:"
473-
)
474-
self._log.debug(self._stdout)
475-
476-
raise err # Raise original error if we couldn't catch it in post-mortem analysis
477-
else:
478-
self._log.debug("Connection established")
467+
self.reconnect_to_mapdl()
479468

480469
# Avoiding muting when connecting to the session
481470
# It might trigger some errors later on if not.
@@ -527,6 +516,38 @@ def _before_run(self, _command: str) -> None:
527516
# For some reason the session hasn't been created
528517
self._create_session()
529518

519+
def _connect_to_mapdl(self, timeout: int):
520+
"""(Re)connect to an existing MAPDL instance"""
521+
try:
522+
self._multi_connect(timeout=timeout)
523+
except MapdlConnectionError as err: # pragma: no cover
524+
self._post_mortem_checks()
525+
self._log.debug(
526+
"The error wasn't caught by the post-mortem checks.\nThe stdout is printed now:"
527+
)
528+
self._log.debug(self._stdout)
529+
530+
raise err # Raise original error if we couldn't catch it in post-mortem analysis
531+
else:
532+
self._log.debug("Connection established")
533+
534+
def reconnect_to_mapdl(self, timeout: int = None):
535+
"""Reconnect to an already instantiated MAPDL instance.
536+
537+
Re-establish an stopped or crashed gRPC connection with an already alive
538+
MAPDL instance. This function does not relaunch the MAPDL instance.
539+
540+
Parameters
541+
----------
542+
timeout : int, optional
543+
Timeout before raising an exception, by default None
544+
"""
545+
546+
if timeout is None:
547+
timeout = self._timeout
548+
549+
self._connect_to_mapdl(timeout)
550+
530551
def _create_process_stds_queue(self, process=None):
531552
from ansys.mapdl.core.launcher import (
532553
_create_queue_for_std, # Avoid circular import error

0 commit comments

Comments
 (0)