Skip to content
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

Fix/tcp osl server processing started #59

Merged
merged 11 commits into from
Oct 19, 2022
14 changes: 12 additions & 2 deletions src/ansys/optislang/core/optislang.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class Optislang:
- WARNING: Log some oddities or potential problems.
- INFO: Log some useful information that program works as expected.
- DEBUG: The most grained logging.
shutdown_on_finished: bool, optional
Shut down when execution is finished and there are not any listeners registered.
It is ignored when the host and port parameters are specified. Defaults to ``True``.

Raises
------
Expand Down Expand Up @@ -81,6 +84,7 @@ def __init__(
name: str = None,
password: str = None,
loglevel: str = None,
shutdown_on_finished: bool = True,
) -> None:
"""Initialize a new instance of the ``Optislang`` class."""
self.__host = host
Expand All @@ -91,8 +95,8 @@ def __init__(
self.__ini_timeout = ini_timeout
self.__name = name
self.__password = password

self.log = LOG.add_instance_logger(self.name, self, loglevel)
self.__shutdown_on_finished = shutdown_on_finished
self.__logger = LOG.add_instance_logger(self.name, self, loglevel)
self.__osl_server: OslServer = self.__init_osl_server("tcp")

def __init_osl_server(self, server_type: str) -> OslServer:
Expand Down Expand Up @@ -125,6 +129,7 @@ def __init_osl_server(self, server_type: str) -> OslServer:
ini_timeout=self.__ini_timeout,
password=self.__password,
logger=self.log,
shutdown_on_finished=self.__shutdown_on_finished,
)
else:
raise NotImplementedError(f'OptiSLang server of type "{server_type}" is not supported.')
Expand All @@ -147,6 +152,11 @@ def name(self) -> str:
self.__name = f"optiSLang_{id(self)}"
return self.__name

@property
def log(self):
"""Return instance logger."""
return self.__logger

def get_osl_version(self) -> str:
"""Get version of used optiSLang.

Expand Down
42 changes: 18 additions & 24 deletions src/ansys/optislang/core/osl_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ class ServerNotification(Enum):
LOG_ERROR = 4
LOG_DEBUG = 5
EXECUTION_STARTED = 6
EXECUTION_FINISHED = 7
NOTHING_PROCESSED = 8
CHECK_FAILED = 9
EXEC_FAILED = 10
ACTOR_STATE_CHANGED = 11
ACTOR_ACTIVE_CHANGED = 12
ACTOR_NAME_CHANGED = 13
ACTOR_CONTENTS_CHANGED = 14
ACTOR_DATA_CHANGED = 15
NUM_NOTIFICATIONS = 16
PROCESSING_STARTED = 7
EXECUTION_FINISHED = 8
NOTHING_PROCESSED = 9
CHECK_FAILED = 10
EXEC_FAILED = 11
ACTOR_STATE_CHANGED = 12
ACTOR_ACTIVE_CHANGED = 13
ACTOR_NAME_CHANGED = 14
ACTOR_CONTENTS_CHANGED = 15
ACTOR_DATA_CHANGED = 16
ALL = 17


class OslServerProcess:
Expand Down Expand Up @@ -469,11 +470,9 @@ def _get_process_args(self) -> List[str]:

if self.__notifications is not None:
# Subscribe to push notifications sent to the listener.
cmd_arg = ""
args.append("--enable-notifications")
for notification in self.__notifications:
cmd_arg += notification.name
cmd_arg += " "
args.append(f"--enable-notifications={cmd_arg.strip()}")
args.append(notification.name)

if self.__additional_args is not None:
for arg_name, arg_value in self.__additional_args.items():
Expand Down Expand Up @@ -618,21 +617,16 @@ def __terminate_osl_child_processes(self):
process.terminate()
except psutil.NoSuchProcess:
self._logger.debug(
"Cannot terminate child process PID: %s. " "The process does not exist.",
process.pid,
f"Cannot terminate child process PID: {process.pid}. "
"The process does not exist."
)

gone, alive = psutil.wait_procs(children, timeout=3)
for process in gone:
self._logger.debug(
"optiSLang server child process %s terminated with exit code %s.",
process,
process.returncode,
)

for process in alive:
self._logger.debug(
"optiSLang server child process %s could not be terminated and will be killed.",
process,
f"optiSLang server child process {process} could not be terminated "
"and will be killed.",
)
process.kill()

Expand Down
17 changes: 9 additions & 8 deletions src/ansys/optislang/core/server_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Module for generation of all server commands."""
import json
from typing import Dict, Sequence, Union
from typing import Dict, Iterable, Sequence, Union

_APPLY_WIZARD = "APPLY_WIZARD"
_CLOSE = "CLOSE"
Expand Down Expand Up @@ -599,7 +599,7 @@ def register_listener(
host: str = None,
port: int = None,
timeout: int = None,
notifications: Sequence = None,
notifications: Iterable[str] = None,
password: str = None,
) -> str:
"""Generate JSON string of register_listener command.
Expand Down Expand Up @@ -1348,8 +1348,8 @@ def subscribe_for_push_notifications(
Either ["ALL"] or Sequence picked from below options:
Server: [ "SERVER_UP", "SERVER_DOWN" ] (always be sent by default).
Logging: [ "LOG_INFO", "LOG_WARNING", "LOG_ERROR", "LOG_DEBUG" ].
Project: [ "EXECUTION_STARTED", "EXECUTION_FINISHED", "NOTHING_PROCESSED",
"CHECK_FAILED", "EXEC_FAILED" ].
Project: [ "EXECUTION_STARTED", "PROCESSING_STARTED", "EXECUTION_FINISHED",
"NOTHING_PROCESSED", "CHECK_FAILED", "EXEC_FAILED" ].
Nodes: [ "ACTOR_STATE_CHANGED", "ACTOR_ACTIVE_CHANGED", "ACTOR_NAME_CHANGED",
"ACTOR_CONTENTS_CHANGED", "ACTOR_DATA_CHANGED" ].
node_types: Sequence, opt
Expand All @@ -1366,6 +1366,7 @@ def subscribe_for_push_notifications(
logging = ["LOG_INFO", "LOG_WARNING", "LOG_ERROR", "LOG_DEBUG"]
project = [
"EXECUTION_STARTED",
"PROCESSING_STARTED",
"EXECUTION_FINISHED",
"NOTHING_PROCESSED",
"CHECK_FAILED",
Expand All @@ -1391,10 +1392,10 @@ def subscribe_for_push_notifications(
raise TypeError(
f"Unsuppored values of ``notifications``: {invalid_items}, "
"supported options are: \n"
"server: {server},\n"
"logging: {logging},\n"
"project: {project},\n"
"nodes: {nodes}"
f"server: {server},\n"
f"logging: {logging},\n"
f"project: {project},\n"
f"nodes: {nodes}"
)
args["notifications"] = notifications

Expand Down
Loading