Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ansys/dpf/core/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def start_local_server(
config=None,
use_pypim_by_default=True,
context=None,
):
) -> BaseServer:
"""Start a new local DPF server at a given port and IP address.

This method requires Windows and ANSYS 2021 R1 or later. If ``as_global=True``, which is
Expand Down
8 changes: 2 additions & 6 deletions src/ansys/dpf/core/server_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
class LicensingContextType(Enum):
"""Enum representing different types of licensing contexts."""

none = 5
premium = 1
"""Checks if at least one license increment exists
and allows operators to block an increment."""
Expand Down Expand Up @@ -81,10 +80,6 @@ def same_licensing_context(first, second):
bool
True if the licensing contexts are compatible, False otherwise.
"""
if (first == LicensingContextType.none and second != LicensingContextType.none) or (
first != LicensingContextType.none and second == LicensingContextType.none
):
return False
if int(first) == int(LicensingContextType.entry) and int(second) != int(
LicensingContextType.entry
):
Expand Down Expand Up @@ -295,7 +290,8 @@ def __ne__(self, other):
class AvailableServerContexts:
"""Defines available server contexts."""

no_context = ServerContext(LicensingContextType.none, "")
"""Applies an empty plugins xml: no plugins are loaded."""
no_context = ServerContext(2, "") # 2 == ContextType.userDefined. This is not exposed publicly.
pre_defined_environment = ServerContext(0)
"""DataProcessingCore.xml that is next to DataProcessingCore.dll/libDataProcessingCore.so will
be taken"""
Expand Down
79 changes: 44 additions & 35 deletions src/ansys/dpf/core/server_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from ansys.dpf.core import __version__, errors, server_context, server_factory
from ansys.dpf.core._version import min_server_version, server_to_ansys_version
from ansys.dpf.core.check_version import server_meet_version
from ansys.dpf.core.server_context import AvailableServerContexts, ServerContext
from ansys.dpf.gate import data_processing_grpcapi, load_api

if TYPE_CHECKING: # pragma: no cover
Expand Down Expand Up @@ -114,7 +115,11 @@


def _run_launch_server_process(
ip, port, ansys_path=None, docker_config=server_factory.RunningDockerConfig()
ip,
port,
ansys_path=None,
docker_config=server_factory.RunningDockerConfig(),
context: ServerContext = None,
):
bShell = False
if docker_config.use_docker:
Expand All @@ -127,13 +132,25 @@
if os.name == "nt":
executable = "Ans.Dpf.Grpc.bat"
run_cmd = f"{executable} --address {ip} --port {port}"
if context not in (
None,
AvailableServerContexts.entry,
AvailableServerContexts.premium,
):
run_cmd += f" --context {int(context.licensing_context_type)}"

Check warning on line 140 in src/ansys/dpf/core/server_types.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/server_types.py#L140

Added line #L140 was not covered by tests
else:
executable = "./Ans.Dpf.Grpc.sh" # pragma: no cover
run_cmd = [
executable,
f"--address {ip}",
f"--port {port}",
] # pragma: no cover
if context not in (
None,
AvailableServerContexts.entry,
AvailableServerContexts.premium,
):
run_cmd.append(f"--context {int(context.licensing_context_type)}")

Check warning on line 153 in src/ansys/dpf/core/server_types.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/server_types.py#L153

Added line #L153 was not covered by tests
path_in_install = load_api._get_path_in_install(internal_folder="bin")
dpf_run_dir = _verify_ansys_path_is_valid(ansys_path, executable, path_in_install)

Expand Down Expand Up @@ -204,7 +221,9 @@
raise RuntimeError(errstr)


def launch_dpf(ansys_path, ip=LOCALHOST, port=DPF_DEFAULT_PORT, timeout=10):
def launch_dpf(
ansys_path, ip=LOCALHOST, port=DPF_DEFAULT_PORT, timeout=10, context: ServerContext = None
):
"""Launch Ansys DPF.

Parameters
Expand All @@ -222,9 +241,10 @@
Maximum number of seconds for the initialization attempt.
The default is ``10``. Once the specified number of seconds
passes, the connection fails.

context : , optional
Context to apply to DPF server when launching it.
"""
process = _run_launch_server_process(ip, port, ansys_path)
process = _run_launch_server_process(ip, port, ansys_path, context=context)
lines = []
current_errors = []
_wait_and_check_server_connection(
Expand Down Expand Up @@ -766,7 +786,7 @@
launch_server: bool = True,
docker_config: DockerConfig = RUNNING_DOCKER,
use_pypim: bool = True,
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
context: server_context.ServerContext = server_context.SERVER_CONTEXT,
):
# Load DPFClientAPI
from ansys.dpf.core.misc import is_pypim_configured
Expand Down Expand Up @@ -808,7 +828,7 @@
timeout=timeout,
)
else:
launch_dpf(ansys_path, ip, port, timeout=timeout)
launch_dpf(ansys_path, ip, port, timeout=timeout, context=context)
self._local_server = True

# store port and ip for later reference
Expand All @@ -820,15 +840,11 @@
self._create_shutdown_funcs()
self._check_first_call(timeout=timeout - (time.time() - start_time)) # Pass remaining time
if context:
if context == core.AvailableServerContexts.no_context:
self._base_service.initialize()
try:
self._base_service.initialize_with_context(context)
self._context = context
else:
try:
self._base_service.initialize_with_context(context)
self._context = context
except errors.DpfVersionNotSupported:
pass
except errors.DpfVersionNotSupported:
pass
self.set_as_global(as_global=as_global)

def _check_first_call(self, timeout: float):
Expand Down Expand Up @@ -1039,18 +1055,14 @@
)
raise e
if context:
if context == core.AvailableServerContexts.no_context:
self._base_service.initialize()
self._context = context
else:
try:
self.apply_context(context)
except errors.DpfVersionNotSupported:
self._base_service.initialize_with_context(
server_context.AvailableServerContexts.premium
)
self._context = server_context.AvailableServerContexts.premium
pass
try:
self.apply_context(context)
except errors.DpfVersionNotSupported:
self._base_service.initialize_with_context(
server_context.AvailableServerContexts.premium
)
self._context = server_context.AvailableServerContexts.premium
pass
self.set_as_global(as_global=as_global)
# Update the python os.environment
if not os.name == "posix":
Expand Down Expand Up @@ -1201,7 +1213,7 @@
launch_server: bool = True,
docker_config: DockerConfig = RUNNING_DOCKER,
use_pypim: bool = True,
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
context: server_context.ServerContext = server_context.SERVER_CONTEXT,
):
"""Start the DPF server."""
# Use ansys.grpc.dpf
Expand Down Expand Up @@ -1247,7 +1259,7 @@
timeout=timeout,
)
else:
launch_dpf(ansys_path, ip, port, timeout=timeout)
launch_dpf(ansys_path, ip, port, timeout=timeout, context=context)
self._local_server = True
from ansys.dpf.core import misc, settings

Expand All @@ -1267,14 +1279,11 @@

check_ansys_grpc_dpf_version(self, timeout)
if context:
if context == core.AvailableServerContexts.no_context:
try:
self._base_service.initialize_with_context(context)
self._context = context
else:
try:
self._base_service.initialize_with_context(context)
self._context = context
except errors.DpfVersionNotSupported:
pass
except errors.DpfVersionNotSupported:
pass
self.set_as_global(as_global=as_global)

def _create_shutdown_funcs(self):
Expand Down
7 changes: 5 additions & 2 deletions tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import pytest

from ansys import dpf
from ansys.dpf.core import examples, path_utilities
from ansys.dpf.core import check_version, examples, path_utilities
import conftest
from conftest import running_docker

Expand Down Expand Up @@ -499,7 +499,8 @@ def test_context_environment_variable(reset_context_environment_variable):


@pytest.mark.skipif(
not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_6_0, reason="Failures on Windows 231"
not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_0 or running_docker,
reason="Failures on Windows 231",
)
def test_server_without_context(remote_config_server_type):
"""Tests starting a server without a no_context given."""
Expand All @@ -510,6 +511,8 @@ def test_server_without_context(remote_config_server_type):
)
none_type = dpf.core.AvailableServerContexts.no_context.licensing_context_type
assert server.context.licensing_context_type == none_type
if check_version.server_meet_version("10.0", server): # Before, there was a bug
assert len(dpf.core.available_operator_names(server=server)) < 20


@pytest.mark.order("last")
Expand Down
Loading