Skip to content

Commit

Permalink
refactor: make cli testing not depending on MAPDL. (#3678)
Browse files Browse the repository at this point in the history
* refactor: make cli testing not depending on MAPDL.

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

* test: improving test coverage

---------

Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
germa89 and pyansys-ci-bot authored Jan 24, 2025
1 parent b5e2492 commit a2f91e6
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 122 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/3678.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
refactor: make cli testing not depending on MAPDL.
32 changes: 13 additions & 19 deletions src/ansys/mapdl/core/cli/list_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
flag_value=True,
type=bool,
default=False,
help="Print only instances",
help="Do not print the child process, only the main processes (instances).",
)
@click.option(
"--long",
Expand Down Expand Up @@ -72,6 +72,15 @@ def list_instances(instances, long, cmd, location) -> None:
# Assuming all ansys processes have -grpc flag
mapdl_instances = []

def is_grpc_based(proc):
cmdline = proc.cmdline()
return "-grpc" in cmdline

def get_port(proc):
cmdline = proc.cmdline()
ind_grpc = cmdline.index("-port")
return cmdline[ind_grpc + 1]

def is_valid_process(proc):
valid_status = proc.status() in [
psutil.STATUS_RUNNING,
Expand All @@ -81,12 +90,7 @@ def is_valid_process(proc):
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
return valid_status and valid_ansys_process and is_grpc_based(proc)

for proc in psutil.process_iter():
# Check if the process is running and not suspended
Expand All @@ -104,8 +108,6 @@ def is_valid_process(proc):
continue

# printing
table = []

if long:
cmd = True
location = True
Expand All @@ -120,18 +122,10 @@ def is_valid_process(proc):
if location:
headers.append("Working directory")

def get_port(proc):
cmdline = proc.cmdline()
ind_grpc = cmdline.index("-port")
return cmdline[ind_grpc + 1]

def is_grpc_based(proc):
cmdline = proc.cmdline()
return "-grpc" in cmdline

table = []
for each_p in mapdl_instances:
if not each_p.ansys_instance or not is_grpc_based(each_p):
if instances and not each_p.ansys_instance:
# Skip child processes if only printing instances
continue

proc_line = []
Expand Down
1 change: 0 additions & 1 deletion src/ansys/mapdl/core/cli/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ def start(
additional_switches=additional_switches,
start_timeout=start_timeout,
port=port,
license_server_check=license_server_check,
license_type=license_type,
version=version,
)
Expand Down
33 changes: 22 additions & 11 deletions src/ansys/mapdl/core/cli/stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ def stop(port: int, pid: Optional[int], all: bool) -> None:
"""
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 Down Expand Up @@ -97,15 +95,11 @@ def stop(port: int, pid: Optional[int], all: bool) -> None:
killed_ = False
for proc in psutil.process_iter():
try:
if (
psutil.pid_exists(proc.pid)
and proc.status() in PROCESS_OK_STATUS
and is_ansys_process(proc)
):
if _is_valid_ansys_process(PROCESS_OK_STATUS, proc):
# Killing "all"
if all:
try:
proc.kill()
_kill_process(proc)
killed_ = True
except psutil.NoSuchProcess:
pass
Expand All @@ -114,7 +108,7 @@ def stop(port: int, pid: Optional[int], all: bool) -> None:
# Killing by ports
if str(port) in proc.cmdline():
try:
proc.kill()
_kill_process(proc)
killed_ = True
except psutil.NoSuchProcess:
pass
Expand Down Expand Up @@ -154,8 +148,9 @@ def stop(port: int, pid: Optional[int], all: bool) -> None:

p = psutil.Process(pid)
for child in p.children(recursive=True):
child.kill()
p.kill()
_kill_process(child)

_kill_process(p)

if p.status == "running":
click.echo(
Expand All @@ -168,3 +163,19 @@ def stop(port: int, pid: Optional[int], all: bool) -> None:
+ f"The process with PID {pid} and its children have been stopped."
)
return


def _kill_process(proc):
proc.kill()


def _is_valid_ansys_process(PROCESS_OK_STATUS, proc):
import psutil

from ansys.mapdl.core.launcher import is_ansys_process

return (
psutil.pid_exists(proc.pid)
and proc.status() in PROCESS_OK_STATUS
and is_ansys_process(proc)
)
Loading

0 comments on commit a2f91e6

Please sign in to comment.