Skip to content

Commit 0a1701a

Browse files
germa89jleonattiakaszynski
authored
Fix/Implement license server check (#603)
* Added main structure of the code. Added also a function which ping an IP:Port using python. Helping fix issue #595 * Added 'check_license_server' before running MAPDL in any of the modes. * Added error class for license_sever_connection_error. * Implemented error handeling in `launcher` * Fixing license server ip and port check. * Fixing grammar. * Added optional argument to `launch_mapdl` to avoid license server check. * Implemented lmutil license check. However it seems to not work properly. This method is not recommended anymore but it is keep for the moment. * Created `license.py`. Preparing to move everything related to licensing there. * Moved license functions to license.py * Adding new local license file check. * Adding new functions for checking the file. * Major changes. Added file server log checking. Restructured completelely 'license.py' Added thread behaviour to the license checking in launcher.py Improved behaviour of `LicenseServerConnectionError` # Aiming to fix #595 * Fixed the license client file name. Now it is dynamically obtained from passed version parameter. Added detailed message to `LicenseServerConnectionError`. * Cleaning a bit * Doing some cosmetical changes. Removing unnecessary parameter. * Implemented `ansysli_util` method. * Formating error class. * Added functionality to check multiple licenses when using ansysli_util * Refurbished 'LicenseServerConnectionError' class. * Format fixing * Format fixing * Added licdebug naming pattern to match previous and future versions. * Added some comments. * Apply suggestions from code review Co-authored-by: jleonatti <[email protected]> * Update ansys/mapdl/core/license.py Co-authored-by: jleonatti <[email protected]> * Apply suggestions from code review Co-authored-by: jleonatti <[email protected]> * Format fixing * flake8 fixes * cleanup licensing * style fixes * Fixed file name spelling * Removed unnecesary functions. Format file using flake8. * Removed unnecesary test function * Debugging * Added docstrings. Format fixed. * Format fxing * Spell checking. * Fixing test_licensing. Many test cannot be executed when the license is not local. * Fixing import function. * Added a way-out to the while in `check_license_file` Added global variables. * Fixing testing * Spell checking. * More details to check_license_file docstring. * More details to check_license_file docstring. * Update ansys/mapdl/core/errors.py Co-authored-by: Alex Kaszynski <[email protected]> * Update ansys/mapdl/core/licensing.py Co-authored-by: Alex Kaszynski <[email protected]> * add basic test for licencing for get_licdebug_msg * additional fixes * style fixes * renaming function * Added a bit in docstring. * fix windows testing * ``get_licdebug_tail`` now gets all the lines, not one by one. Added double quote wrapper around cmd command in case of Windows. * Reduced the amount of attemps at connect in 'mapdl_grpc'. implemented more robuts time limit for checking. Now it stops if timeout or the maximum amount of attemps is reached. * Reduced the amount of attemps at connect in 'mapdl_grpc'. implemented more robuts time limit for checking. Now it stops if timeout or the maximum amount of attemps is reached. * Fixing licensing tests. * Grammar fixing. * Fixing test in docker. * format fxing. * Small changes in the documentation. In the section of running MAPDL to clarify when to use `launch_mapdl` or `Mapdl`. * Fixing wrong rst markup. * Cleaning PR. * Small fixes and cleaning. * permit failed MAPDL start * add additional licensing docs and options * fix test asserts on pass * Update ansys/mapdl/core/licensing.py Co-authored-by: German <[email protected]> Co-authored-by: jleonatti <[email protected]> Co-authored-by: Alex Kaszynski <[email protected]>
1 parent 4b5a87d commit 0a1701a

File tree

9 files changed

+728
-90
lines changed

9 files changed

+728
-90
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[flake8]
2-
exclude = venv, __init__.py, build
2+
exclude = venv, __init__.py, build, doc/source/examples
33
# To be added after refactoring code to be compliant: E501
44
select = W191, W291, W293, W391, E115, E117, E122, E124, E125, E225, E231, E301, E303, F401, F403
55
count = True

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
make
3535
3636
- name: flake8
37+
if: always()
3738
run: |
3839
make flake8
3940

ansys/mapdl/core/errors.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ def __init__(self, msg=LOCKFILE_MSG):
7575
RuntimeError.__init__(self, msg)
7676

7777

78+
class LicenseServerConnectionError(RuntimeError):
79+
"""Error when the license server is not available."""
80+
81+
def __init__(self, msg=""):
82+
RuntimeError.__init__(self, msg)
83+
84+
7885
# handler for protect_grpc
7986
def handler(sig, frame): # pragma: no cover
8087
"""Pass signal to custom interrupt handler."""

ansys/mapdl/core/launcher.py

Lines changed: 73 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212

1313
from ansys.mapdl import core as pymapdl
1414
from ansys.mapdl.core.misc import is_float, random_string, create_temp_dir, threaded
15-
from ansys.mapdl.core.errors import LockFileException, VersionError
15+
from ansys.mapdl.core.errors import (
16+
LockFileException,
17+
VersionError,
18+
)
1619
from ansys.mapdl.core.mapdl_grpc import MapdlGrpc
20+
from ansys.mapdl.core.licensing import LicenseChecker
1721

1822
# settings directory
1923
SETTINGS_DIR = appdirs.user_data_dir("ansys_mapdl_core")
@@ -674,7 +678,7 @@ def check_lock_file(path, jobname, override):
674678
raise LockFileException(
675679
"Unable to remove lock file. "
676680
"Another instance of MAPDL might be "
677-
'running at "%s"' % path
681+
f"running at '{path}'"
678682
)
679683

680684

@@ -703,8 +707,8 @@ def _validate_add_sw(add_sw, exec_path, force_intel=False):
703707
if "smp" not in add_sw: # pragma: no cover
704708
# Ubuntu ANSYS fails to launch without I_MPI_SHM_LMT
705709
if _is_ubuntu():
706-
os.environ['I_MPI_SHM_LMT'] = 'shm'
707-
if os.name == 'nt' and not force_intel:
710+
os.environ["I_MPI_SHM_LMT"] = "shm"
711+
if os.name == "nt" and not force_intel:
708712
# Workaround to fix a problem when launching ansys in 'dmp' mode in the
709713
# recent windows version and using VPN.
710714
#
@@ -715,14 +719,14 @@ def _validate_add_sw(add_sw, exec_path, force_intel=False):
715719
#
716720
# Adding '-mpi msmpi' to the launch parameter fix it.
717721

718-
if 'intelmpi' in add_sw:
722+
if "intelmpi" in add_sw:
719723
# Remove intel flag.
720724
regex = "(-mpi)( *?)(intelmpi)"
721-
add_sw = re.sub(regex, '', add_sw)
725+
add_sw = re.sub(regex, "", add_sw)
722726
warnings.warn(INTEL_MSG)
723727

724728
if _version_from_path(exec_path) >= 210:
725-
add_sw += ' -mpi msmpi'
729+
add_sw += " -mpi msmpi"
726730

727731
return add_sw
728732

@@ -745,6 +749,7 @@ def launch_mapdl(
745749
clear_on_connect=True,
746750
log_apdl=False,
747751
verbose_mapdl=False,
752+
license_server_check=True,
748753
**kwargs,
749754
):
750755
"""Start MAPDL locally in gRPC mode.
@@ -856,6 +861,10 @@ def launch_mapdl(
856861
MAPDL. This should be used for debugging only as output can
857862
be tracked within pymapdl. Default ``False``.
858863
864+
license_server_check : bool, optional
865+
Check if the license server is available if MAPDL fails to
866+
start. Only available on ``mode='grpc'``. Defaults to True
867+
859868
Notes
860869
-----
861870
These are the MAPDL switch options as of 2020R2 applicable for
@@ -1028,8 +1037,9 @@ def launch_mapdl(
10281037
mode = check_mode(mode, _version_from_path(exec_file))
10291038

10301039
# cache start parameters
1031-
additional_switches = _validate_add_sw(additional_switches, exec_file,
1032-
kwargs.pop('force_intel', False))
1040+
additional_switches = _validate_add_sw(
1041+
additional_switches, exec_file, kwargs.pop("force_intel", False)
1042+
)
10331043
start_parm = {
10341044
"exec_file": exec_file,
10351045
"run_location": run_location,
@@ -1045,46 +1055,61 @@ def launch_mapdl(
10451055
start_parm["override"] = override
10461056
start_parm["timeout"] = start_timeout
10471057

1048-
if mode == "console":
1049-
from ansys.mapdl.core.mapdl_console import MapdlConsole
1050-
1051-
mapdl = MapdlConsole(loglevel=loglevel, log_apdl=log_apdl, **start_parm)
1052-
elif mode == "corba":
1053-
try:
1054-
# pending deprication to ansys-mapdl-corba
1055-
from ansys.mapdl.core.mapdl_corba import MapdlCorba
1056-
except ImportError:
1057-
raise ImportError(
1058-
"To use this feature, install the MAPDL CORBA package"
1059-
" with:\n\npip install ansys_corba"
1060-
) from None
1061-
1062-
broadcast = kwargs.get("log_broadcast", False)
1063-
mapdl = MapdlCorba(
1064-
loglevel=loglevel,
1065-
log_apdl=log_apdl,
1066-
log_broadcast=broadcast,
1067-
verbose=verbose_mapdl,
1068-
**start_parm,
1069-
)
1070-
elif mode == "grpc":
1071-
port, actual_run_location = launch_grpc(
1072-
port=port, verbose=verbose_mapdl, ip=ip, **start_parm
1073-
)
1074-
mapdl = MapdlGrpc(
1075-
ip=ip,
1076-
port=port,
1077-
cleanup_on_exit=cleanup_on_exit,
1078-
loglevel=loglevel,
1079-
set_no_abort=set_no_abort,
1080-
remove_temp_files=kwargs.pop("remove_temp_files", False),
1081-
log_apdl=log_apdl,
1082-
**start_parm,
1058+
# Check the license server
1059+
if license_server_check:
1060+
# configure timeout to be 90% of the wait time of the startup
1061+
# time for Ansys.
1062+
lic_check = LicenseChecker(
1063+
timeout=start_timeout*0.9, verbose=verbose_mapdl
10831064
)
1084-
if run_location is None:
1085-
mapdl._path = actual_run_location
1086-
else:
1087-
raise ValueError("Invalid mode %s" % mode)
1065+
lic_check.start()
1066+
1067+
try:
1068+
if mode == "console":
1069+
from ansys.mapdl.core.mapdl_console import MapdlConsole
1070+
1071+
mapdl = MapdlConsole(loglevel=loglevel, log_apdl=log_apdl, **start_parm)
1072+
elif mode == "corba":
1073+
try:
1074+
# pending deprication to ansys-mapdl-corba
1075+
from ansys.mapdl.core.mapdl_corba import MapdlCorba
1076+
except ImportError:
1077+
raise ImportError(
1078+
"To use this feature, install the MAPDL CORBA package"
1079+
" with:\n\npip install ansys_corba"
1080+
) from None
1081+
1082+
broadcast = kwargs.get("log_broadcast", False)
1083+
mapdl = MapdlCorba(
1084+
loglevel=loglevel,
1085+
log_apdl=log_apdl,
1086+
log_broadcast=broadcast,
1087+
verbose=verbose_mapdl,
1088+
**start_parm,
1089+
)
1090+
elif mode == "grpc":
1091+
port, actual_run_location = launch_grpc(
1092+
port=port, verbose=verbose_mapdl, ip=ip, **start_parm
1093+
)
1094+
mapdl = MapdlGrpc(
1095+
ip=ip,
1096+
port=port,
1097+
cleanup_on_exit=cleanup_on_exit,
1098+
loglevel=loglevel,
1099+
set_no_abort=set_no_abort,
1100+
remove_temp_files=kwargs.pop("remove_temp_files", False),
1101+
log_apdl=log_apdl,
1102+
**start_parm,
1103+
)
1104+
if run_location is None:
1105+
mapdl._path = actual_run_location
1106+
except Exception as exception:
1107+
# Failed to launch for some reason. Check if failure was due
1108+
# to the license check
1109+
if license_server_check:
1110+
lic_check.check()
1111+
# pass
1112+
raise exception
10881113

10891114
return mapdl
10901115

0 commit comments

Comments
 (0)