diff --git a/mx_bec/actor_manager.py b/mx_bec/actor_manager.py index 73868b7..1ddf989 100644 --- a/mx_bec/actor_manager.py +++ b/mx_bec/actor_manager.py @@ -1,20 +1,22 @@ from __future__ import annotations import functools -from collections.abc import Callable, Iterable from threading import Event, Thread -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Callable, Iterable from bec_lib.config_values import RedisConfigValue from bec_lib.endpoints import MessageEndpoints from bec_lib.logger import bec_logger -from bec_lib.messages import BuiltinActorStateUpdatedNotification +from bec_lib.messages import AvailableResourceMessage, BuiltinActorStateUpdatedNotification from mx_bec.actors import all_actor_classes from mx_bec.actors.core import MXAutomationActor +from mx_bec.endpoints import ActorEndpoints if TYPE_CHECKING: - from bec_lib.client import BECClient + from bec_lib.endpoints import EndpointInfo + + from mx_bec.service import AareBec logger = bec_logger.logger @@ -33,7 +35,7 @@ class AareActorManager: """ def __init__( - self, client: BECClient, actor_classes: Iterable[type[MXAutomationActor]] | None = None + self, client: AareBec, actor_classes: Iterable[type[MXAutomationActor]] | None = None ) -> None: self._client = client self._actor_classes = ( @@ -43,16 +45,23 @@ class AareActorManager: self._enabled_flags: dict[ActorClass, RedisConfigValue[bool]] = {} # RedisConfigValue only keeps weak references to subscribers, so hold the partials here. self._enabled_callbacks: dict[ActorClass, Callable[[bool], None]] = {} - + enabled_endpoints: dict[str, EndpointInfo] = {} for actor_class in self._actor_classes: + ep = actor_class.config_endpoint() flag: RedisConfigValue[bool] = RedisConfigValue( - connector=self._client.connector, endpoint=actor_class.config_endpoint() + connector=self._client.connector, endpoint=ep ) + enabled_endpoints[actor_class.__name__] = ep callback = functools.partial(self._enabled_changed, actor_class) flag.subscribe(callback) self._enabled_flags[actor_class] = flag self._enabled_callbacks[actor_class] = callback - + available_actor_config_eps = AvailableResourceMessage( + resource={"endpoints": enabled_endpoints} + ) + self._client.connector.set_and_publish( + ActorEndpoints.available_actor_config_eps(), available_actor_config_eps + ) self._start_all() @property diff --git a/mx_bec/bec_widgets/widgets/aare_bec/__init__.py b/mx_bec/bec_widgets/widgets/aare_bec/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mx_bec/bec_widgets/widgets/aare_bec/actors_config.py b/mx_bec/bec_widgets/widgets/aare_bec/actors_config.py new file mode 100644 index 0000000..0f8bf55 --- /dev/null +++ b/mx_bec/bec_widgets/widgets/aare_bec/actors_config.py @@ -0,0 +1,42 @@ +from bec_lib.logger import bec_logger +from bec_lib.messages import AvailableResourceMessage +from bec_widgets.utils.bec_connector import ConnectionConfig +from bec_widgets.utils.bec_widget import BECWidget +from bec_widgets.widgets.utility.toggle.toggle import ToggleSwitch +from qtpy.QtWidgets import QLabel, QVBoxLayout, QWidget + +from mx_bec.endpoints import ActorEndpoints + +logger = bec_logger.logger + + +class MxActorConfigWidget(BECWidget, QWidget): + """ + Widget displaying and managing MX actors - automation steps. + """ + + PLUGIN = True + ICON_NAME = "format_list_bulleted" + USER_ACCESS = ["refresh"] + + def __init__( + self, + parent: QWidget | None = None, + client=None, + config: ConnectionConfig | None = None, + gui_id: str | None = None, + **kwargs, + ) -> None: + super().__init__(parent=parent, client=client, config=config, gui_id=gui_id, **kwargs) + self.refresh() + + def refresh(self): + """Reset the layout for currently loaded actors""" + self._layout = QVBoxLayout(self) + available_actors = self.client.connector.get(ActorEndpoints.available_actor_config_eps()) + if not isinstance(available_actors, AvailableResourceMessage): + logger.warning("No available MX actors registered") + self._layout.addWidget(QLabel("No available MX actors registered")) + for name, ep in available_actors.resource["endpoints"].items(): + self._layout.addWidget(QLabel(name)) + self._layout.addWidget(ToggleSwitch()) diff --git a/mx_bec/endpoints.py b/mx_bec/endpoints.py index d303ef2..518413b 100644 --- a/mx_bec/endpoints.py +++ b/mx_bec/endpoints.py @@ -1,5 +1,5 @@ from bec_lib.endpoints import EndpointInfo, EndpointType, MessageOp -from bec_lib.messages import BoolConfigDefaultFalse +from bec_lib.messages import AvailableResourceMessage, BoolConfigDefaultFalse from mx_bec.messages import MXMeasurementSequenceMessage, OpticalPredictionMessage @@ -54,6 +54,15 @@ class ActorEndpoints: message_op=MessageOp.STREAM, ) + @staticmethod + def available_actor_config_eps(): + """Endpoint to broadcast which MX actors are available on the MX builtin actor manager""" + return EndpointInfo( + endpoint=f"{EndpointType.INFO}/actor/mx/available_config_eps", + message_type=AvailableResourceMessage, + message_op=MessageOp.SET_PUBLISH, + ) + class DataAnalysisEndpoints: @staticmethod diff --git a/mx_bec/service.py b/mx_bec/service.py index aadf19c..e8a1fc7 100644 --- a/mx_bec/service.py +++ b/mx_bec/service.py @@ -32,9 +32,12 @@ class AareBec(BECClient): if (config_redis_url := os.environ.get("CONFIG_REDIS_URL")) is None: raise RuntimeError( "No address for redis config client specified. " - "Set CONFIG_REDIS_URL to 'mx--queue-01'" + "Set CONFIG_REDIS_URL to 'redis://mx--queue-01:6379'" "if you want to create an AareBec client" ) + config.model.log_writer.base_path = "/tmp/mx_bec_logs" + config.config["log_writer"]["base_path"] = "/tmp/mx_bec_logs" + super().__init__(config, connector_cls, name="AareBec", **kwargs) self.aare_config = AareConfigClient(redis_url=config_redis_url, key_prefix="config") self.workflow_state = AareBecWorkflowState.IDLE diff --git a/pyproject.toml b/pyproject.toml index 1f9b9bf..fd9891d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -71,6 +71,9 @@ good-names-rgxs = [ ".*_1D.*", ] +[project.scripts] +mx-bec-service = "mx_bec.main:main" + [tool.ruff] line-length = 100