Skip to content

Commit 0f3e681

Browse files
authored
Refactor server context (#634)
1 parent 28123a1 commit 0f3e681

16 files changed

+306
-118
lines changed

.github/workflows/scripts/separate_long_core_tests.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@ New-Item -Path ".\" -Name "test_multi_server" -ItemType "directory"
55
New-Item -Path ".\" -Name "test_workflow" -ItemType "directory"
66
New-Item -Path ".\" -Name "test_remote_workflow" -ItemType "directory"
77
New-Item -Path ".\" -Name "test_remote_operator" -ItemType "directory"
8+
New-Item -Path ".\" -Name "test_service" -ItemType "directory"
89
Copy-Item -Path "tests\conftest.py" -Destination ".\test_launcher\"
910
Copy-Item -Path "tests\conftest.py" -Destination ".\test_server\"
1011
Copy-Item -Path "tests\conftest.py" -Destination ".\test_local_server\"
1112
Copy-Item -Path "tests\conftest.py" -Destination ".\test_multi_server\"
1213
Copy-Item -Path "tests\conftest.py" -Destination ".\test_workflow\"
1314
Copy-Item -Path "tests\conftest.py" -Destination ".\test_remote_workflow\"
1415
Copy-Item -Path "tests\conftest.py" -Destination ".\test_remote_operator\"
16+
Copy-Item -Path "tests\conftest.py" -Destination ".\test_service\"
1517
Copy-Item -Path "tests\test_launcher.py" -Destination ".\test_launcher\"
1618
Copy-Item -Path "tests\test_server.py" -Destination ".\test_server\"
1719
Copy-Item -Path "tests\test_local_server.py" -Destination ".\test_local_server\"
1820
Copy-Item -Path "tests\test_multi_server.py" -Destination ".\test_multi_server\"
1921
Copy-Item -Path "tests\test_workflow.py" -Destination ".\test_workflow\"
2022
Copy-Item -Path "tests\test_remote_workflow.py" -Destination ".\test_remote_workflow\"
2123
Copy-Item -Path "tests\test_remote_operator.py" -Destination ".\test_remote_operator\"
24+
Copy-Item -Path "tests\test_service.py" -Destination ".\test_service\"
2225
Remove-Item -Path "tests\test_server.py"
2326
Remove-Item -Path "tests\test_launcher.py"
2427
Remove-Item -Path "tests\test_local_server.py"
2528
Remove-Item -Path "tests\test_multi_server.py"
2629
Remove-Item -Path "tests\test_workflow.py"
2730
Remove-Item -Path "tests\test_remote_workflow.py"
28-
Remove-Item -Path "tests\test_remote_operator.py"
31+
Remove-Item -Path "tests\test_remote_operator.py"
32+
Remove-Item -Path "tests\test_service.py"

.github/workflows/test_docker.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,20 @@ jobs:
5959
working-directory: test_launcher
6060
run: |
6161
pytest $DEBUG --cov=ansys.dpf.core --cov-report=xml --cov-report=html --cov-append --log-level=ERROR --junitxml=../tests/junit/test-results2.xml --reruns 2 .
62-
if: always()
6362
timeout-minutes: 20
6463

6564
- name: "Test API test_server"
6665
shell: bash
6766
working-directory: test_server
6867
run: |
6968
pytest $DEBUG --cov=ansys.dpf.core --cov-report=xml --cov-report=html --cov-append --log-level=ERROR --junitxml=../tests/junit/test-results4.xml --reruns 2 .
70-
if: always()
7169
timeout-minutes: 10
7270

7371
- name: "Test API test_local_server"
7472
shell: bash
7573
working-directory: test_local_server
7674
run: |
7775
pytest $DEBUG --cov=ansys.dpf.core --cov-report=xml --cov-report=html --cov-append --log-level=ERROR --junitxml=../tests/junit/test-results5.xml --reruns 2 .
78-
if: always()
7976
timeout-minutes: 20
8077

8178
- name: "Test API test_multi_server"
@@ -104,15 +101,20 @@ jobs:
104101
working-directory: test_workflow
105102
run: |
106103
pytest $DEBUG --cov=ansys.dpf.core --cov-report=xml --cov-report=html --cov-append --log-level=ERROR --junitxml=../tests/junit/test-results3.xml --reruns 3 .
107-
if: always()
108104
timeout-minutes: 20
109105

106+
- name: "Test API test_service"
107+
shell: bash
108+
working-directory: test_service
109+
run: |
110+
pytest $DEBUG --cov=ansys.dpf.core --cov-report=xml --cov-report=html --cov-append --log-level=ERROR --junitxml=../tests/junit/test-results9.xml --reruns 3 .
111+
110112
- name: "Upload Test Results"
111113
uses: actions/upload-artifact@v3
112114
with:
113115
name: ${{ env.PACKAGE_NAME }}_${{ matrix.python-version }}_${{ matrix.os }}_pytest_${{ env.ANSYS_VERSION }}_docker
114116
path: tests/junit/test-results.xml
115-
if: always()
117+
timeout-minutes: 20
116118

117119
- name: "Upload coverage to Codecov"
118120
uses: codecov/codecov-action@v3

.github/workflows/tests.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ jobs:
133133
working-directory: test_launcher
134134
run: |
135135
pytest $DEBUG --cov=ansys.dpf.core --cov-report=xml --cov-report=html --cov-append --log-level=ERROR --junitxml=../tests/junit/test-results2.xml --reruns 2 .
136-
if: always()
137136
138137
- name: "Kill all servers"
139138
uses: pyansys/pydpf-actions/[email protected]
@@ -155,7 +154,6 @@ jobs:
155154
working-directory: test_local_server
156155
run: |
157156
pytest $DEBUG --cov=ansys.dpf.core --cov-report=xml --cov-report=html --cov-append --log-level=ERROR --junitxml=../tests/junit/test-results5.xml --reruns 2 .
158-
if: always()
159157
160158
- name: "Kill all servers"
161159
uses: pyansys/pydpf-actions/[email protected]
@@ -166,7 +164,6 @@ jobs:
166164
working-directory: test_multi_server
167165
run: |
168166
pytest $DEBUG --cov=ansys.dpf.core --cov-report=xml --cov-report=html --cov-append --log-level=ERROR --junitxml=../tests/junit/test-results6.xml --reruns 2 .
169-
if: always()
170167
171168
- name: "Kill all servers"
172169
uses: pyansys/pydpf-actions/[email protected]
@@ -177,7 +174,6 @@ jobs:
177174
working-directory: test_remote_workflow
178175
run: |
179176
pytest $DEBUG --cov=ansys.dpf.core --cov-report=xml --cov-report=html --cov-append --log-level=ERROR --junitxml=../tests/junit/test-results7.xml --reruns 2 .
180-
if: always()
181177
182178
- name: "Kill all servers"
183179
uses: pyansys/pydpf-actions/[email protected]
@@ -188,7 +184,6 @@ jobs:
188184
working-directory: test_remote_operator
189185
run: |
190186
pytest $DEBUG --cov=ansys.dpf.core --cov-report=xml --cov-report=html --cov-append --log-level=ERROR --junitxml=../tests/junit/test-results8.xml --reruns 2 .
191-
if: always()
192187
193188
- name: "Kill all servers"
194189
uses: pyansys/pydpf-actions/[email protected]
@@ -205,6 +200,16 @@ jobs:
205200
uses: pyansys/pydpf-actions/[email protected]
206201
if: always()
207202

203+
- name: "Test API test_service"
204+
shell: bash
205+
working-directory: test_service
206+
run: |
207+
pytest $DEBUG --cov=ansys.dpf.core --cov-report=xml --cov-report=html --cov-append --log-level=ERROR --junitxml=../tests/junit/test-results9.xml --reruns 3 .
208+
209+
- name: "Kill all servers"
210+
uses: pyansys/pydpf-actions/[email protected]
211+
if: always()
212+
208213
- name: "Upload Test Results"
209214
uses: actions/upload-artifact@v3
210215
with:

ansys/dpf/core/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
from ansys.dpf.core import path_utilities
8282
from ansys.dpf.core import settings
8383
from ansys.dpf.core.server_factory import ServerConfig, AvailableServerConfigs
84-
from ansys.dpf.core.server_context import apply_server_context, AvailableServerContexts
84+
from ansys.dpf.core.server_context import set_default_server_context, AvailableServerContexts
8585

8686
# for matplotlib
8787
# solves "QApplication: invalid style override passed, ignoring it."

ansys/dpf/core/core.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -439,12 +439,12 @@ def apply_context(self, context):
439439
if not self._server().meet_version("6.0"):
440440
raise errors.DpfVersionNotSupported("6.0")
441441
if self._server().has_client():
442-
error = self._api.data_processing_apply_context_on_client(
443-
self._server().client, context.context_type.value, context.xml_path
442+
self._api.data_processing_apply_context_on_client(
443+
self._server().client, int(context.licensing_context_type), context.xml_path
444444
)
445445
else:
446-
error = self._api.data_processing_apply_context(
447-
context.context_type.value, context.xml_path
446+
self._api.data_processing_apply_context(
447+
int(context.licensing_context_type), context.xml_path
448448
)
449449

450450
def initialize_with_context(self, context):
@@ -464,14 +464,14 @@ def initialize_with_context(self, context):
464464
if self._server().has_client():
465465
if not self._server().meet_version("6.0"):
466466
raise errors.DpfVersionNotSupported("6.0")
467-
error = self._api.data_processing_initialize_with_context_on_client(
468-
self._server().client, context.context_type.value, context.xml_path
467+
self._api.data_processing_initialize_with_context_on_client(
468+
self._server().client, int(context.licensing_context_type), context.xml_path
469469
)
470470
else:
471471
if not self._server().meet_version("4.0"):
472472
raise errors.DpfVersionNotSupported("4.0")
473-
error = self._api.data_processing_initialize_with_context(
474-
context.context_type.value, context.xml_path
473+
self._api.data_processing_initialize_with_context(
474+
int(context.licensing_context_type), context.xml_path
475475
)
476476

477477
@version_requires("6.0")

ansys/dpf/core/custom_operator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
operator_specification,
2020
dpf_operator,
2121
collection,
22+
AvailableServerContexts,
2223
)
2324
from ansys.dpf.core._custom_operators_helpers import __operator_main__, functions_registry, \
2425
external_operator_api, _type_to_output_method, _type_to_input_method
@@ -46,7 +47,7 @@ def record_operator(operator_type, *args) -> None:
4647
operator = operator_type
4748
if dpf.SERVER is None:
4849
settings.set_server_configuration(server_factory.ServerConfig(None, False))
49-
server.start_local_server()
50+
server.start_local_server(context=AvailableServerContexts.premium)
5051
if len(args) == 2:
5152
external_operator_api.external_operator_record_with_abstract_core_and_wrapper(
5253
operator._call_back(),

ansys/dpf/core/server.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from ansys.dpf.core.server_factory import ServerConfig, ServerFactory, CommunicationProtocols
2020
from ansys.dpf.core.server_types import DPF_DEFAULT_PORT, LOCALHOST, RUNNING_DOCKER
21+
from ansys.dpf.core import server_context
2122

2223

2324
def shutdown_global_server():
@@ -129,7 +130,8 @@ def start_local_server(
129130
docker_config=RUNNING_DOCKER,
130131
timeout=20.,
131132
config=None,
132-
use_pypim_by_default=True
133+
use_pypim_by_default=True,
134+
context=None
133135
):
134136
"""Start a new local DPF server at a given port and IP address.
135137
@@ -168,6 +170,10 @@ def start_local_server(
168170
use_pypim_by_default: bool, optional
169171
Whether to use PyPIM functionalities by default when a PyPIM environment is detected.
170172
Defaults to True.
173+
context: ServerContext, optional
174+
Defines the settings that will be used to load DPF's plugins.
175+
A DPF xml file can be used to list the plugins and set up variables. Default is
176+
`server_context.SERVER_CONTEXT`.
171177
172178
Returns
173179
-------
@@ -207,6 +213,9 @@ def start_local_server(
207213
else:
208214
docker_config.use_docker = False
209215

216+
if context is None:
217+
context = server_context.SERVER_CONTEXT
218+
210219
server = None
211220
n_attempts = 3
212221
timed_out = False
@@ -221,11 +230,11 @@ def start_local_server(
221230
server = server_type(
222231
ansys_path, ip, port, as_global=as_global, launch_server=True,
223232
load_operators=load_operators, docker_config=docker_config, timeout=timeout,
224-
use_pypim=use_pypim)
233+
use_pypim=use_pypim, context=context)
225234
else:
226235
server = server_type(
227236
ansys_path, as_global=as_global,
228-
load_operators=load_operators, timeout=timeout)
237+
load_operators=load_operators, timeout=timeout, context=context)
229238
break
230239
except errors.InvalidPortError: # allow socket in use errors
231240
port += 1
@@ -249,7 +258,14 @@ def start_local_server(
249258
return server
250259

251260

252-
def connect_to_server(ip=LOCALHOST, port=DPF_DEFAULT_PORT, as_global=True, timeout=5, config=None):
261+
def connect_to_server(
262+
ip=LOCALHOST,
263+
port=DPF_DEFAULT_PORT,
264+
as_global=True,
265+
timeout=5,
266+
config=None,
267+
context=None,
268+
):
253269
"""Connect to an existing DPF server.
254270
255271
This method sets the global default channel that is then used for the
@@ -273,6 +289,10 @@ def connect_to_server(ip=LOCALHOST, port=DPF_DEFAULT_PORT, as_global=True, timeo
273289
passes, the connection fails.
274290
config: ServerConfig, optional
275291
Manages the type of server connection to use.
292+
context: ServerContext, optional
293+
Defines the settings that will be used to load DPF's plugins.
294+
A DPF xml file can be used to list the plugins and set up variables. Default is
295+
`server_context.SERVER_CONTEXT`.
276296
277297
Examples
278298
--------
@@ -293,17 +313,21 @@ def connect_to_server(ip=LOCALHOST, port=DPF_DEFAULT_PORT, as_global=True, timeo
293313
>>> #unspecified_server = dpf.connect_to_server(as_global=False)
294314
295315
"""
316+
if context is None:
317+
context = server_context.SERVER_CONTEXT
296318

297319
def connect():
298320
server_init_signature = inspect.signature(server_type.__init__)
299321
if "ip" in server_init_signature.parameters.keys() \
300322
and "port" in server_init_signature.parameters.keys():
301323
server = server_type(
302-
ip=ip, port=port, as_global=as_global, launch_server=False
324+
ip=ip, port=port, as_global=as_global, launch_server=False,
325+
context=context
303326
)
304327
else:
305328
server = server_type(
306-
as_global=as_global
329+
as_global=as_global,
330+
context=context
307331
)
308332
dpf.core._server_instances.append(weakref.ref(server))
309333
return server

0 commit comments

Comments
 (0)