WIP fleshing out actor framework

This commit is contained in:
perl_d committed 2026-10-19 17:10:20 +02:00
1 parent 8209134df0
commit f6b30b686b
10 files changed
+148 -14

No files matched your search

+2 -2
View File
@@ -1,3 +1,3 @@
from .service import AareBecService
from .service import AareBec
__all__ = ["AareBecService"]
__all__ = ["AareBec"]
+35 -4
View File
@@ -1,11 +1,42 @@
from threading import Event, Thread
from typing import TypeVar
from bec_lib.client import ServiceConfig
from bec_lib.config_values import RedisConfigValue
from bec_lib.logger import bec_logger
from bec_server.actors.builtin_actor_manager import BuiltinActorManager
from aare_bec_proto import AareBec
from aare_bec_proto.actors.core import MXAutomationActor
logger = bec_logger.logger
class AareActorManager(BuiltinActorManager):
"""Manager for the builtin actors of the AareBEC service."""
ActorType = TypeVar("ActorType", bound=MXAutomationActor)
class AareActorDict(dict):
def __setitem__(self, key: type[ActorType], value: tuple[ActorType, Thread, Event], /) -> None:
return super().__setitem__(key, value)
def __getitem__(self, key: type[ActorType], /) -> tuple[ActorType, Thread, Event]:
return super().__getitem__(key)
def get( # type: ignore
self, key: type[ActorType], default: tuple[ActorType, Thread, Event] | None = None
) -> tuple[ActorType, Thread, Event]:
return super().get(key) # type: ignore
class AareActorManager:
"""Manager for AareBec builtin actors"""
def __init__(self, bootstrap_server: str) -> None:
super().__init__(bootstrap_server)
host, port = bootstrap_server.split(":")
self._client = AareBec(
config=ServiceConfig(config={"redis": {"host": host, "port": port}}),
name="AareActorManager",
)
self._client.start()
self._actors_threads_and_stops = AareActorDict()
# TODO: for each actor, create a RedisConfigValue for it being enabled from its .config_endpoint()
+33
View File
@@ -0,0 +1,33 @@
from abc import abstractmethod
from bec_lib.actors import BECClient
from bec_lib.endpoints import EndpointInfo
from bec_server.actors.actor import SubscriptionActor
from aare_bec_proto.messages import MXMeasurementSequenceMessage
class MXAutomationActor(SubscriptionActor):
"""Base for MX Automation actors. Must be used with endpoints yielding
MXMeasurementSequenceMessage."""
def __init__(self, client: BECClient, name: str, exec_id: str):
super().__init__(client, name, exec_id)
self.action_table = {}
def evaluate(self, msg: MXMeasurementSequenceMessage):
if not msg.success:
return
return super().evaluate()
@property
@abstractmethod
def config_endpoint(self) -> EndpointInfo: ...
@abstractmethod
@staticmethod
def condition() -> bool: ...
@abstractmethod
@staticmethod
def action() -> None: ...
@@ -0,0 +1,17 @@
from bec_lib.endpoints import EndpointInfo, EndpointType
from aare_bec_proto.actors.core import MXAutomationActor
from aare_bec_proto.endpoints import StateEndpoints
class OnSampleMountedRunOpticalCentring(MXAutomationActor):
"""When a sample is mounted, run optical centring"""
@property
def config_endpoint(self) -> EndpointInfo:
return EndpointInfo(
endpoint=f"{EndpointType.INFO}/MxAutomation/"
) # TODO make only the last part into a variable on each actor class and fill in the messageop etc. on the parent class
def default_monitor_endpoints(self) -> set[EndpointInfo]:
return {StateEndpoints.sample_mounted()}
+6
View File
@@ -0,0 +1,6 @@
from enum import StrEnum
class AareBecWorkflowState(StrEnum):
IDLE = "IDLE"
SAMPLE_MOUNTED = "SAMPLE_MOUNTED"
+38
View File
@@ -0,0 +1,38 @@
from bec_lib.endpoints import EndpointInfo, EndpointType
from bec_lib.messages import MessageOp
from aare_bec_proto.messages import MXMeasurementSequenceMessage
class StateEndpoints:
@staticmethod
def sample_mounted():
return EndpointInfo(
endpoint=f"{EndpointType.INFO.value}/mx/sequence/mounted",
message_type=MXMeasurementSequenceMessage,
message_op=MessageOp.SET_PUBLISH,
)
@staticmethod
def optical_centring_done():
return EndpointInfo(
endpoint=f"{EndpointType.INFO.value}/mx/sequence/optical_centring_done",
message_type=MXMeasurementSequenceMessage,
message_op=MessageOp.SET_PUBLISH,
)
@staticmethod
def xray_centring_done():
return EndpointInfo(
endpoint=f"{EndpointType.INFO.value}/mx/sequence/xray_centring_done",
message_type=MXMeasurementSequenceMessage,
message_op=MessageOp.SET_PUBLISH,
)
@staticmethod
def rotation_done():
return EndpointInfo(
endpoint=f"{EndpointType.INFO.value}/mx/sequence/rotation_done",
message_type=MXMeasurementSequenceMessage,
message_op=MessageOp.SET_PUBLISH,
)
+3 -3
View File
@@ -6,7 +6,7 @@ from bec_lib.bec_service import parse_cmdline_args
from bec_lib.logger import bec_logger
from bec_lib.redis_connector import RedisConnector
from aare_bec_proto.service import AareBecService
from aare_bec_proto.service import AareBec
logger = bec_logger.logger
bec_logger.level = bec_logger.LOGLEVEL.INFO
@@ -18,10 +18,10 @@ def main():
"""
_, _, config = parse_cmdline_args()
service = AareBecService(config, RedisConnector)
service = AareBec(config, RedisConnector)
try:
event = threading.Event()
logger.success("Started AareBecService")
logger.success("Started AareBec")
event.wait()
except KeyboardInterrupt:
service.shutdown()
+6
View File
@@ -0,0 +1,6 @@
from bec_lib.messages import BECMessage
class MXMeasurementSequenceMessage(BECMessage):
sample_id: int
success: bool
+8 -5
View File
@@ -3,10 +3,11 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from bec_lib import messages
from bec_lib.bec_service import BECService
from bec_lib.client import BECClient
from bec_lib.logger import bec_logger
from aare_bec_proto.actor_manager import AareActorManager
from aare_bec_proto.constants import AareBecWorkflowState
if TYPE_CHECKING:
from bec_lib.redis_connector import RedisConnector
@@ -15,18 +16,20 @@ if TYPE_CHECKING:
logger = bec_logger.logger
class AareBecService(BECService):
class AareBec(BECClient):
"""Prototype BEC service standing in for AareDAQ."""
def __init__(self, config: str | ServiceConfig, connector_cls: type[RedisConnector]) -> None:
super().__init__(config, connector_cls, unique_service=True)
def __init__(self, config: ServiceConfig, connector_cls: type[RedisConnector]) -> None:
super().__init__(config, connector_cls)
self._start_actor_manager()
self.status = messages.BECStatus.RUNNING
self.workflow_state = AareBecWorkflowState.IDLE
def _start_actor_manager(self):
self.actor_manager = AareActorManager(self.bootstrap_server)
def shutdown(self, per_thread_timeout_s: float | None = None) -> None:
"""shutdown the service"""
self.actor_manager.shutdown()
# TODO: reenable once implemented # self.actor_manager.shutdown()
super().shutdown(per_thread_timeout_s)