Skip to content

Commit e1787a1

Browse files
committed
fix no context
1 parent 8eefad1 commit e1787a1

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

src/ansys/dpf/core/server_context.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
class LicensingContextType(Enum):
4646
"""Enum representing different types of licensing contexts."""
4747

48-
none = 5
4948
premium = 1
5049
"""Checks if at least one license increment exists
5150
and allows operators to block an increment."""
@@ -295,7 +294,7 @@ def __ne__(self, other):
295294
class AvailableServerContexts:
296295
"""Defines available server contexts."""
297296

298-
no_context = ServerContext(LicensingContextType.none, "")
297+
no_context = ServerContext(2, "")
299298
pre_defined_environment = ServerContext(0)
300299
"""DataProcessingCore.xml that is next to DataProcessingCore.dll/libDataProcessingCore.so will
301300
be taken"""

src/ansys/dpf/core/server_types.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
from ansys.dpf.core import __version__, errors, server_context, server_factory
5151
from ansys.dpf.core._version import min_server_version, server_to_ansys_version
5252
from ansys.dpf.core.check_version import server_meet_version
53+
from ansys.dpf.core.server_context import ServerContext
5354
from ansys.dpf.gate import data_processing_grpcapi, load_api
5455

5556
if TYPE_CHECKING: # pragma: no cover
@@ -114,7 +115,11 @@ def _verify_ansys_path_is_valid(ansys_path, executable, path_in_install=None):
114115

115116

116117
def _run_launch_server_process(
117-
ip, port, ansys_path=None, docker_config=server_factory.RunningDockerConfig()
118+
ip,
119+
port,
120+
ansys_path=None,
121+
docker_config=server_factory.RunningDockerConfig(),
122+
context: ServerContext = None,
118123
):
119124
bShell = False
120125
if docker_config.use_docker:
@@ -127,13 +132,17 @@ def _run_launch_server_process(
127132
if os.name == "nt":
128133
executable = "Ans.Dpf.Grpc.bat"
129134
run_cmd = f"{executable} --address {ip} --port {port}"
135+
if context is not None:
136+
run_cmd += f" --context {context.licensing_context_type}"
130137
else:
131138
executable = "./Ans.Dpf.Grpc.sh" # pragma: no cover
132139
run_cmd = [
133140
executable,
134141
f"--address {ip}",
135142
f"--port {port}",
136143
] # pragma: no cover
144+
if context is not None:
145+
run_cmd.append(f"--context {context.licensing_context_type}")
137146
path_in_install = load_api._get_path_in_install(internal_folder="bin")
138147
dpf_run_dir = _verify_ansys_path_is_valid(ansys_path, executable, path_in_install)
139148

@@ -204,7 +213,9 @@ def read_stdout():
204213
raise RuntimeError(errstr)
205214

206215

207-
def launch_dpf(ansys_path, ip=LOCALHOST, port=DPF_DEFAULT_PORT, timeout=10):
216+
def launch_dpf(
217+
ansys_path, ip=LOCALHOST, port=DPF_DEFAULT_PORT, timeout=10, context: ServerContext = None
218+
):
208219
"""Launch Ansys DPF.
209220
210221
Parameters
@@ -222,9 +233,10 @@ def launch_dpf(ansys_path, ip=LOCALHOST, port=DPF_DEFAULT_PORT, timeout=10):
222233
Maximum number of seconds for the initialization attempt.
223234
The default is ``10``. Once the specified number of seconds
224235
passes, the connection fails.
225-
236+
context : , optional
237+
Context to apply to DPF server when launching it.
226238
"""
227-
process = _run_launch_server_process(ip, port, ansys_path)
239+
process = _run_launch_server_process(ip, port, ansys_path, context=context)
228240
lines = []
229241
current_errors = []
230242
_wait_and_check_server_connection(
@@ -766,7 +778,7 @@ def __init__(
766778
launch_server: bool = True,
767779
docker_config: DockerConfig = RUNNING_DOCKER,
768780
use_pypim: bool = True,
769-
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
781+
context: server_context.ServerContext = server_context.SERVER_CONTEXT,
770782
):
771783
# Load DPFClientAPI
772784
from ansys.dpf.core.misc import is_pypim_configured
@@ -808,7 +820,7 @@ def __init__(
808820
timeout=timeout,
809821
)
810822
else:
811-
launch_dpf(ansys_path, ip, port, timeout=timeout)
823+
launch_dpf(ansys_path, ip, port, timeout=timeout, context=context)
812824
self._local_server = True
813825

814826
# store port and ip for later reference
@@ -820,15 +832,11 @@ def __init__(
820832
self._create_shutdown_funcs()
821833
self._check_first_call(timeout=timeout - (time.time() - start_time)) # Pass remaining time
822834
if context:
823-
if context == core.AvailableServerContexts.no_context:
824-
self._base_service.initialize()
835+
try:
836+
self._base_service.initialize_with_context(context)
825837
self._context = context
826-
else:
827-
try:
828-
self._base_service.initialize_with_context(context)
829-
self._context = context
830-
except errors.DpfVersionNotSupported:
831-
pass
838+
except errors.DpfVersionNotSupported:
839+
pass
832840
self.set_as_global(as_global=as_global)
833841

834842
def _check_first_call(self, timeout: float):
@@ -1201,7 +1209,7 @@ def __init__(
12011209
launch_server: bool = True,
12021210
docker_config: DockerConfig = RUNNING_DOCKER,
12031211
use_pypim: bool = True,
1204-
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
1212+
context: server_context.ServerContext = server_context.SERVER_CONTEXT,
12051213
):
12061214
"""Start the DPF server."""
12071215
# Use ansys.grpc.dpf
@@ -1247,7 +1255,7 @@ def __init__(
12471255
timeout=timeout,
12481256
)
12491257
else:
1250-
launch_dpf(ansys_path, ip, port, timeout=timeout)
1258+
launch_dpf(ansys_path, ip, port, timeout=timeout, context=context)
12511259
self._local_server = True
12521260
from ansys.dpf.core import misc, settings
12531261

@@ -1267,14 +1275,11 @@ def __init__(
12671275

12681276
check_ansys_grpc_dpf_version(self, timeout)
12691277
if context:
1270-
if context == core.AvailableServerContexts.no_context:
1278+
try:
1279+
self._base_service.initialize_with_context(context)
12711280
self._context = context
1272-
else:
1273-
try:
1274-
self._base_service.initialize_with_context(context)
1275-
self._context = context
1276-
except errors.DpfVersionNotSupported:
1277-
pass
1281+
except errors.DpfVersionNotSupported:
1282+
pass
12781283
self.set_as_global(as_global=as_global)
12791284

12801285
def _create_shutdown_funcs(self):

tests/test_service.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ def test_server_without_context(remote_config_server_type):
510510
)
511511
none_type = dpf.core.AvailableServerContexts.no_context.licensing_context_type
512512
assert server.context.licensing_context_type == none_type
513+
assert len(dpf.core.available_operator_names(server=server)) < 20
513514

514515

515516
@pytest.mark.order("last")

0 commit comments

Comments
 (0)