Skip to content

Refactor server context #634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9ad39f5
Refactor server context
cbellot000 Nov 22, 2022
477a2f7
not set as global if apply context didn't work
cbellot000 Nov 22, 2022
a4bdba9
fix python plugins
cbellot000 Nov 22, 2022
67c4186
styling
cbellot000 Nov 22, 2022
9f5eb03
improve unit test
cbellot000 Nov 22, 2022
8c345d5
fix tests for docker
cbellot000 Nov 22, 2022
fef10ad
docker errors
cbellot000 Nov 22, 2022
dead6a0
Merge branch 'master' of https://github.com/pyansys/DPF-Core into ref…
cbellot000 Nov 23, 2022
71ee972
typo
cbellot000 Nov 23, 2022
f1470fc
Update doc (#636)
anslpa Nov 23, 2022
aa0733b
docker rm even when server failed
cbellot000 Nov 23, 2022
6baadee
Revert "not set as global if apply context didn't work"
cbellot000 Nov 23, 2022
4d54385
try
cbellot000 Nov 23, 2022
0690cfb
revert
cbellot000 Nov 23, 2022
31ec8cc
Revert "revert"
cbellot000 Nov 23, 2022
449885f
Revert "try"
cbellot000 Nov 23, 2022
e57eb24
Revert "Revert "not set as global if apply context didn't work""
cbellot000 Nov 23, 2022
430fade
threading
cbellot000 Nov 23, 2022
3745a23
initialize
cbellot000 Nov 23, 2022
c03214b
fix service unit tests
cbellot000 Nov 23, 2022
a89aa5b
skip failing test
cbellot000 Nov 23, 2022
e844c14
skip failing test
cbellot000 Nov 23, 2022
d7741e3
Merge branch 'cbellot/fix_docker_stop' of https://github.com/pyansys/…
cbellot000 Nov 23, 2022
dacfa70
API change
cbellot000 Nov 23, 2022
74469aa
styling
cbellot000 Nov 23, 2022
525e895
split tests service
cbellot000 Nov 24, 2022
1c10008
test_service docker
cbellot000 Nov 24, 2022
2596a31
add time out and disable always
cbellot000 Nov 24, 2022
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
6 changes: 5 additions & 1 deletion .github/workflows/scripts/separate_long_core_tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@ New-Item -Path ".\" -Name "test_multi_server" -ItemType "directory"
New-Item -Path ".\" -Name "test_workflow" -ItemType "directory"
New-Item -Path ".\" -Name "test_remote_workflow" -ItemType "directory"
New-Item -Path ".\" -Name "test_remote_operator" -ItemType "directory"
New-Item -Path ".\" -Name "test_service" -ItemType "directory"
Copy-Item -Path "tests\conftest.py" -Destination ".\test_launcher\"
Copy-Item -Path "tests\conftest.py" -Destination ".\test_server\"
Copy-Item -Path "tests\conftest.py" -Destination ".\test_local_server\"
Copy-Item -Path "tests\conftest.py" -Destination ".\test_multi_server\"
Copy-Item -Path "tests\conftest.py" -Destination ".\test_workflow\"
Copy-Item -Path "tests\conftest.py" -Destination ".\test_remote_workflow\"
Copy-Item -Path "tests\conftest.py" -Destination ".\test_remote_operator\"
Copy-Item -Path "tests\conftest.py" -Destination ".\test_service\"
Copy-Item -Path "tests\test_launcher.py" -Destination ".\test_launcher\"
Copy-Item -Path "tests\test_server.py" -Destination ".\test_server\"
Copy-Item -Path "tests\test_local_server.py" -Destination ".\test_local_server\"
Copy-Item -Path "tests\test_multi_server.py" -Destination ".\test_multi_server\"
Copy-Item -Path "tests\test_workflow.py" -Destination ".\test_workflow\"
Copy-Item -Path "tests\test_remote_workflow.py" -Destination ".\test_remote_workflow\"
Copy-Item -Path "tests\test_remote_operator.py" -Destination ".\test_remote_operator\"
Copy-Item -Path "tests\test_service.py" -Destination ".\test_service\"
Remove-Item -Path "tests\test_server.py"
Remove-Item -Path "tests\test_launcher.py"
Remove-Item -Path "tests\test_local_server.py"
Remove-Item -Path "tests\test_multi_server.py"
Remove-Item -Path "tests\test_workflow.py"
Remove-Item -Path "tests\test_remote_workflow.py"
Remove-Item -Path "tests\test_remote_operator.py"
Remove-Item -Path "tests\test_remote_operator.py"
Remove-Item -Path "tests\test_service.py"
12 changes: 7 additions & 5 deletions .github/workflows/test_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,20 @@ jobs:
working-directory: test_launcher
run: |
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 .
if: always()
timeout-minutes: 20

- name: "Test API test_server"
shell: bash
working-directory: test_server
run: |
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 .
if: always()
timeout-minutes: 10

- name: "Test API test_local_server"
shell: bash
working-directory: test_local_server
run: |
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 .
if: always()
timeout-minutes: 20

- name: "Test API test_multi_server"
Expand Down Expand Up @@ -104,15 +101,20 @@ jobs:
working-directory: test_workflow
run: |
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 .
if: always()
timeout-minutes: 20

- name: "Test API test_service"
shell: bash
working-directory: test_service
run: |
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 .

- name: "Upload Test Results"
uses: actions/upload-artifact@v3
with:
name: ${{ env.PACKAGE_NAME }}_${{ matrix.python-version }}_${{ matrix.os }}_pytest_${{ env.ANSYS_VERSION }}_docker
path: tests/junit/test-results.xml
if: always()
timeout-minutes: 20

- name: "Upload coverage to Codecov"
uses: codecov/codecov-action@v3
15 changes: 10 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ jobs:
working-directory: test_launcher
run: |
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 .
if: always()

- name: "Kill all servers"
uses: pyansys/pydpf-actions/[email protected]
Expand All @@ -155,7 +154,6 @@ jobs:
working-directory: test_local_server
run: |
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 .
if: always()

- name: "Kill all servers"
uses: pyansys/pydpf-actions/[email protected]
Expand All @@ -166,7 +164,6 @@ jobs:
working-directory: test_multi_server
run: |
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 .
if: always()

- name: "Kill all servers"
uses: pyansys/pydpf-actions/[email protected]
Expand All @@ -177,7 +174,6 @@ jobs:
working-directory: test_remote_workflow
run: |
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 .
if: always()

- name: "Kill all servers"
uses: pyansys/pydpf-actions/[email protected]
Expand All @@ -188,7 +184,6 @@ jobs:
working-directory: test_remote_operator
run: |
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 .
if: always()

- name: "Kill all servers"
uses: pyansys/pydpf-actions/[email protected]
Expand All @@ -205,6 +200,16 @@ jobs:
uses: pyansys/pydpf-actions/[email protected]
if: always()

- name: "Test API test_service"
shell: bash
working-directory: test_service
run: |
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 .

- name: "Kill all servers"
uses: pyansys/pydpf-actions/[email protected]
if: always()

- name: "Upload Test Results"
uses: actions/upload-artifact@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion ansys/dpf/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
from ansys.dpf.core import path_utilities
from ansys.dpf.core import settings
from ansys.dpf.core.server_factory import ServerConfig, AvailableServerConfigs
from ansys.dpf.core.server_context import apply_server_context, AvailableServerContexts
from ansys.dpf.core.server_context import set_default_server_context, AvailableServerContexts

# for matplotlib
# solves "QApplication: invalid style override passed, ignoring it."
Expand Down
16 changes: 8 additions & 8 deletions ansys/dpf/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,12 @@ def apply_context(self, context):
if not self._server().meet_version("6.0"):
raise errors.DpfVersionNotSupported("6.0")
if self._server().has_client():
error = self._api.data_processing_apply_context_on_client(
self._server().client, context.context_type.value, context.xml_path
self._api.data_processing_apply_context_on_client(
self._server().client, int(context.licensing_context_type), context.xml_path
)
else:
error = self._api.data_processing_apply_context(
context.context_type.value, context.xml_path
self._api.data_processing_apply_context(
int(context.licensing_context_type), context.xml_path
)

def initialize_with_context(self, context):
Expand All @@ -464,14 +464,14 @@ def initialize_with_context(self, context):
if self._server().has_client():
if not self._server().meet_version("6.0"):
raise errors.DpfVersionNotSupported("6.0")
error = self._api.data_processing_initialize_with_context_on_client(
self._server().client, context.context_type.value, context.xml_path
self._api.data_processing_initialize_with_context_on_client(
self._server().client, int(context.licensing_context_type), context.xml_path
)
else:
if not self._server().meet_version("4.0"):
raise errors.DpfVersionNotSupported("4.0")
error = self._api.data_processing_initialize_with_context(
context.context_type.value, context.xml_path
self._api.data_processing_initialize_with_context(
int(context.licensing_context_type), context.xml_path
)

@version_requires("6.0")
Expand Down
3 changes: 2 additions & 1 deletion ansys/dpf/core/custom_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
operator_specification,
dpf_operator,
collection,
AvailableServerContexts,
)
from ansys.dpf.core._custom_operators_helpers import __operator_main__, functions_registry, \
external_operator_api, _type_to_output_method, _type_to_input_method
Expand Down Expand Up @@ -46,7 +47,7 @@ def record_operator(operator_type, *args) -> None:
operator = operator_type
if dpf.SERVER is None:
settings.set_server_configuration(server_factory.ServerConfig(None, False))
server.start_local_server()
server.start_local_server(context=AvailableServerContexts.premium)
if len(args) == 2:
external_operator_api.external_operator_record_with_abstract_core_and_wrapper(
operator._call_back(),
Expand Down
36 changes: 30 additions & 6 deletions ansys/dpf/core/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from ansys.dpf.core.server_factory import ServerConfig, ServerFactory, CommunicationProtocols
from ansys.dpf.core.server_types import DPF_DEFAULT_PORT, LOCALHOST, RUNNING_DOCKER
from ansys.dpf.core import server_context


def shutdown_global_server():
Expand Down Expand Up @@ -129,7 +130,8 @@ def start_local_server(
docker_config=RUNNING_DOCKER,
timeout=20.,
config=None,
use_pypim_by_default=True
use_pypim_by_default=True,
context=None
):
"""Start a new local DPF server at a given port and IP address.

Expand Down Expand Up @@ -168,6 +170,10 @@ def start_local_server(
use_pypim_by_default: bool, optional
Whether to use PyPIM functionalities by default when a PyPIM environment is detected.
Defaults to True.
context: ServerContext, optional
Defines the settings that will be used to load DPF's plugins.
A DPF xml file can be used to list the plugins and set up variables. Default is
`server_context.SERVER_CONTEXT`.

Returns
-------
Expand Down Expand Up @@ -207,6 +213,9 @@ def start_local_server(
else:
docker_config.use_docker = False

if context is None:
context = server_context.SERVER_CONTEXT

server = None
n_attempts = 3
timed_out = False
Expand All @@ -221,11 +230,11 @@ def start_local_server(
server = server_type(
ansys_path, ip, port, as_global=as_global, launch_server=True,
load_operators=load_operators, docker_config=docker_config, timeout=timeout,
use_pypim=use_pypim)
use_pypim=use_pypim, context=context)
else:
server = server_type(
ansys_path, as_global=as_global,
load_operators=load_operators, timeout=timeout)
load_operators=load_operators, timeout=timeout, context=context)
break
except errors.InvalidPortError: # allow socket in use errors
port += 1
Expand All @@ -249,7 +258,14 @@ def start_local_server(
return server


def connect_to_server(ip=LOCALHOST, port=DPF_DEFAULT_PORT, as_global=True, timeout=5, config=None):
def connect_to_server(
ip=LOCALHOST,
port=DPF_DEFAULT_PORT,
as_global=True,
timeout=5,
config=None,
context=None,
):
"""Connect to an existing DPF server.

This method sets the global default channel that is then used for the
Expand All @@ -273,6 +289,10 @@ def connect_to_server(ip=LOCALHOST, port=DPF_DEFAULT_PORT, as_global=True, timeo
passes, the connection fails.
config: ServerConfig, optional
Manages the type of server connection to use.
context: ServerContext, optional
Defines the settings that will be used to load DPF's plugins.
A DPF xml file can be used to list the plugins and set up variables. Default is
`server_context.SERVER_CONTEXT`.

Examples
--------
Expand All @@ -293,17 +313,21 @@ def connect_to_server(ip=LOCALHOST, port=DPF_DEFAULT_PORT, as_global=True, timeo
>>> #unspecified_server = dpf.connect_to_server(as_global=False)

"""
if context is None:
context = server_context.SERVER_CONTEXT

def connect():
server_init_signature = inspect.signature(server_type.__init__)
if "ip" in server_init_signature.parameters.keys() \
and "port" in server_init_signature.parameters.keys():
server = server_type(
ip=ip, port=port, as_global=as_global, launch_server=False
ip=ip, port=port, as_global=as_global, launch_server=False,
context=context
)
else:
server = server_type(
as_global=as_global
as_global=as_global,
context=context
)
dpf.core._server_instances.append(weakref.ref(server))
return server
Expand Down
Loading