Skip to content

Commit

Permalink
feat: adding channel subscription method and tests (#3340)
Browse files Browse the repository at this point in the history
* feat: adding channel subscription method and tests

* chore: adding changelog file 3340.miscellaneous.md

* feat: check channel also before establishing if instance is alive or not

* fix: test and adding more logging

---------

Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
germa89 and pyansys-ci-bot authored Aug 9, 2024
1 parent 893f741 commit 6b6b797
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/3340.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: adding channel subscription method and tests
57 changes: 55 additions & 2 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ def __init__(
self._state: Optional[grpc.Future] = None
self._timeout: int = timeout
self._pids: List[Union[int, None]] = []
self._channel_state: grpc.ChannelConnectivity = (
grpc.ChannelConnectivity.CONNECTING
)

if channel is None:
self._log.debug("Creating channel to %s:%s", ip, port)
Expand All @@ -405,6 +408,9 @@ def __init__(
self._log.debug("Using provided channel")
self._channel: grpc.Channel = channel

# Subscribe to channel for channel state updates
self._subscribe_to_channel()

# connect and validate to the channel
self._mapdl_process: Popen = start_parm.pop("process", None)

Expand Down Expand Up @@ -487,6 +493,31 @@ def _create_channel(self, ip: str, port: int) -> grpc.Channel:
],
)

def _subscribe_to_channel(self):
"""Subscribe to channel status and store the value in 'mapdl._channel_state'"""

# Callback function to monitor state changes
def connectivity_callback(connectivity):
self._log.debug(f"Channel connectivity changed to: {connectivity}")
self._channel_state = connectivity

# Subscribe to channel state changes
self._channel.subscribe(connectivity_callback, try_to_connect=True)

@property
def channel_state(self) -> str:
"""Returns the gRPC channel state.
The possible values are:
- 0 - 'IDLE'
- 1 - 'CONNECTING'
- 2 - 'READY'
- 3 - 'TRANSIENT_FAILURE'
- 4 - 'SHUTDOWN'
"""
return self._channel_state.name

def _multi_connect(self, n_attempts=5, timeout=15):
"""Try to connect over a series of attempts to the channel.
Expand Down Expand Up @@ -2528,13 +2559,35 @@ def _download_as_raw(self, target_name: str) -> str:
@property
def is_alive(self) -> bool:
"""True when there is an active connect to the gRPC server"""
if self.channel_state not in ["IDLE", "READY"]:
self._log.debug(
"MAPDL instance is not alive because the channel is not 'IDLE' o 'READY'."
)
return False

if self._exited:
self._log.debug("MAPDL instance is not alive because it is exited.")
return False
if self.busy:
self._log.debug("MAPDL instance is alive because it is busy.")
return True

try:
return bool(self.inquire("", "JOBNAME"))
except:
check = bool(self._ctrl("VERSION"))
if check:
self._log.debug(
"MAPDL instance is alive because version was retrieved."
)
else:
self._log.debug(
"MAPDL instance is not alive because version was not retrieved. Maybe output is muted?."
)
return check

except Exception as error:
self._log.debug(
f"MAPDL instance is not alive because retrieving version failed with:\n{error}"
)
return False

@property
Expand Down
17 changes: 17 additions & 0 deletions tests/test_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,23 @@ def test__check_stds(mapdl):
assert mapdl._stderr is not None


def test_subscribe_to_channel(mapdl):
assert mapdl.channel_state in [
"IDLE",
"CONNECTING",
"READY",
"TRANSIENT_FAILURE",
"SHUTDOWN",
]
assert mapdl._channel_state in [
grpc.ChannelConnectivity.IDLE,
grpc.ChannelConnectivity.CONNECTING,
grpc.ChannelConnectivity.READY,
grpc.ChannelConnectivity.TRANSIENT_FAILURE,
grpc.ChannelConnectivity.SHUTDOWN,
]


@requires("remote")
def test_exception_message_length(mapdl):
# This test does not fail if running on local
Expand Down

0 comments on commit 6b6b797

Please sign in to comment.