Skip to content

Commit

Permalink
fix: pool issues (#3266)
Browse files Browse the repository at this point in the history
* feat: adding port to exception message

* fix: attempt tests before being ready

* feat: changing arguments order

* chore: wait for complete exit

* test: raise exception if mapdl instances are alive between tests

* chore: adding changelog file 3257.added.md

* fix: adding missing import

* fix: adding missing object

* test: check process status

* fix: running process check only on local.

* feat: enforcing having exactly the amount of instances specified.
Adding timeout to check if the instance is already launched.

* feat: adding a timeout before moving on

* fix: latest_version env var

* feat: added pool_creator fixture. We use pool fixture to check pool health.
fix: some tests

* fix: NoSuchProcess error. Small cosmetic fix

* chore: adding changelog file 3266.fixed.md

* chore: adding changelog file 3266.fixed.md

* refactor: small reog

* test: activating previously skipped tests

* fix: test

* fix: adding port to avoid port collision

* fkix: tests

* docs: adding comments

* feat: adding ``ready`` property and ``wait_for_ready`` method.
fix: Making sure we restart the instance on the same path.
refactor: waiting for the instance to get ready.
test: added test to check directory names when there is a restart.

* feat: Checking ports from the cmdline

* fix: tests

* fix: early exit in process check to avoid accessdenied.

* Revert "fkix: tests"

This reverts commit d58971b.

* feat: catching already dead process.

* fix: pymapdl list not showing any instance because name method wasn't called.

* feat: wrapping process checking in a try/except to avoid calling already dead process

* feat: using dynamic port in test_cli. Starting and stopping another instance.

* fix: test

* refactor: reducing code duplicity

* feat: making sure we stop MAPDL if failure

* fix: test_remove_temp_dir_on_exit on windows

* test: without rerun

* ci: using v24.2 for docs building

* fix: exception in list instance processing

* feat: using PORT1 variable
refactor: moving console test to test_console

* fix: tests

* ci: run all tests

* test: testing

* test: no raise exception.

* ci: increasing timeout for local and min jobs

* chore: adding logging statements.

* test: marking tests as xfail

* ci: adding back pytest config

* Revert "build: update ansys-api-mapdl to 0.5.2 (#3255)"

This reverts commit 0bcf344.

* test: skip flaky tests

* build: update ansys-api-mapdl to 0.5.2 (#3255)

* build: update ansys-api-mapdl to 0.5.2

* chore: adding changelog file 3255.dependencies.md

---------

Co-authored-by: pyansys-ci-bot <[email protected]>

* test: skip flaky test. See #2435 comment

* fix: not showing instances on linux (#3263)

* fix: not showing instances on linux

* chore: adding changelog file 3263.fixed.md

---------

Co-authored-by: pyansys-ci-bot <[email protected]>

* ci: undo some stuff

* test: adding some waiting time after attempting to kill instance.

* fix: missing import.

* chore: remove fragment from other PR.

---------

Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
germa89 and pyansys-ci-bot committed Jul 12, 2024
1 parent fb2f659 commit 2df6130
Show file tree
Hide file tree
Showing 13 changed files with 342 additions and 163 deletions.
4 changes: 4 additions & 0 deletions .ci/build_matrix.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

# **** REMEMBER *****
# Remember to update the env var ``LATEST_VERSION`` in ci.yml
#

# List of versions
versions=(
# if added more "latest", change "$LATEST"
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ env:
PACKAGE_NAME: 'ansys-mapdl-core'
PACKAGE_NAMESPACE: 'ansys.mapdl.core'
DOCUMENTATION_CNAME: 'mapdl.docs.pyansys.com'
LATEST_VERSION: "242"
MAPDL_IMAGE_VERSION_DOCS_BUILD: v24.2-ubuntu-student
MEILISEARCH_API_KEY: ${{ secrets.MEILISEARCH_API_KEY }}
MEILISEARCH_PUBLIC_API_KEY: ${{ secrets.MEILISEARCH_PUBLIC_API_KEY }}
PYANSYS_OFF_SCREEN: True
DPF_START_SERVER: False
DPF_PORT: 21004
MAPDL_PACKAGE: ghcr.io/ansys/mapdl
MAPDL_IMAGE_VERSION_DOCS_BUILD: v24.1-ubuntu-student
ON_CI: True
PYTEST_ARGUMENTS: '-vvv -ra --durations=10 --maxfail=3 --reruns 3 --reruns-delay 4 --cov=ansys.mapdl.core --cov-report=html'

Expand Down Expand Up @@ -807,7 +808,7 @@ jobs:
# executable path with the env var: PYMAPDL_MAPDL_EXEC.
if [[ "${{ matrix.mapdl-version }}" == *"latest-ubuntu"* ]] ; then
version="242"
version=${{ env.LATEST_VERSION }}
else
version=$(echo "${{ matrix.mapdl-version }}" | head -c 5 | tail -c 4 | tr -d '.')
fi;
Expand Down
1 change: 1 addition & 0 deletions doc/changelog.d/3266.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix: pool issues
30 changes: 21 additions & 9 deletions src/ansys/mapdl/core/cli/list_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,35 @@ def list_instances(instances, long, cmd, location):
mapdl_instances = []

def is_valid_process(proc):
valid_status = proc.status in [psutil.STATUS_RUNNING, psutil.STATUS_IDLE]
valid_status = proc.status() in [
psutil.STATUS_RUNNING,
psutil.STATUS_IDLE,
psutil.STATUS_SLEEPING,
]
valid_ansys_process = ("ansys" in proc.name().lower()) or (
"mapdl" in proc.name().lower()
)
# Early exit to avoid checking 'cmdline' of a protected process (raises psutil.AccessDenied)
if not valid_ansys_process:
return False

grpc_is_active = "-grpc" in proc.cmdline()
return valid_status and valid_ansys_process and grpc_is_active

for proc in psutil.process_iter():
# Check if the process is running and not suspended
if is_valid_process(proc):
# Checking the number of children we infer if the process is the main process,
# or one of the main process thread.
if len(proc.children(recursive=True)) < 2:
proc.ansys_instance = False
else:
proc.ansys_instance = True
mapdl_instances.append(proc)
try:
if is_valid_process(proc):
# Checking the number of children we infer if the process is the main process,
# or one of the main process thread.
if len(proc.children(recursive=True)) < 2:
proc.ansys_instance = False
else:
proc.ansys_instance = True
mapdl_instances.append(proc)

except (psutil.NoSuchProcess, psutil.ZombieProcess) as e:
continue

# printing
table = []
Expand Down
44 changes: 22 additions & 22 deletions src/ansys/mapdl/core/cli/stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@
import click


def is_ansys_process(proc):
return (
"ansys" in proc.name().lower() or "mapdl" in proc.name().lower()
) and "-grpc" in proc.cmdline()


@click.command(
short_help="Stop MAPDL instances.",
help="""This command stop MAPDL instances running on a given port or with a given process id (PID).
Expand Down Expand Up @@ -58,6 +52,8 @@ def is_ansys_process(proc):
def stop(port, pid, all):
import psutil

from ansys.mapdl.core.launcher import is_ansys_process

PROCESS_OK_STATUS = [
# List of all process status, comment out the ones that means that
# process is not OK.
Expand All @@ -84,28 +80,32 @@ def stop(port, pid, all):
if port or all:
killed_ = False
for proc in psutil.process_iter():
if (
psutil.pid_exists(proc.pid)
and proc.status() in PROCESS_OK_STATUS
and is_ansys_process(proc)
):
# Killing "all"
if all:
try:
proc.kill()
killed_ = True
except psutil.NoSuchProcess:
pass

else:
# Killing by ports
if str(port) in proc.cmdline():
try:
if (
psutil.pid_exists(proc.pid)
and proc.status() in PROCESS_OK_STATUS
and is_ansys_process(proc)
):
# Killing "all"
if all:
try:
proc.kill()
killed_ = True
except psutil.NoSuchProcess:
pass

else:
# Killing by ports
if str(port) in proc.cmdline():
try:
proc.kill()
killed_ = True
except psutil.NoSuchProcess:
pass

except psutil.NoSuchProcess:
continue

if all:
str_ = ""
else:
Expand Down
3 changes: 3 additions & 0 deletions src/ansys/mapdl/core/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ def get_process_at_port(port) -> Optional[psutil.Process]:
) # just to check if we can access the
except psutil.AccessDenied:
continue
except psutil.NoSuchProcess:
# process already died
continue

for conns in connections:
if conns.laddr.port == port:
Expand Down
7 changes: 5 additions & 2 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1059,9 +1059,12 @@ def _remove_temp_dir_on_exit(self, path=None):
"""
if self.remove_temp_dir_on_exit and self._local:
path = path or self.directory
from pathlib import Path

path = str(Path(path or self.directory))
tmp_dir = tempfile.gettempdir()
ans_temp_dir = os.path.join(tmp_dir, "ansys_")
ans_temp_dir = str(Path(os.path.join(tmp_dir, "ansys_")))

if path.startswith(ans_temp_dir):
self._log.debug("Removing the MAPDL temporary directory %s", path)
shutil.rmtree(path, ignore_errors=True)
Expand Down
Loading

0 comments on commit 2df6130

Please sign in to comment.