From f6b30b686b7e3a4cb9283c78fddb667ff8a0d1bf Mon Sep 17 00:00:00 2001 From: David Perl Date: Mon, 19 Oct 2026 17:10:20 +0200 Subject: [PATCH] WIP fleshing out actor framework --- ..._CONSOLE.2026-10-16_15-51-45_086484.log.gz | Bin 0 -> 1182 bytes src/aare_bec_proto/__init__.py | 4 +- src/aare_bec_proto/actor_manager.py | 39 ++++++++++++++++-- src/aare_bec_proto/actors/core.py | 33 +++++++++++++++ src/aare_bec_proto/actors/mx_automation.py | 17 ++++++++ src/aare_bec_proto/constants.py | 6 +++ src/aare_bec_proto/endpoints.py | 38 +++++++++++++++++ src/aare_bec_proto/main.py | 6 +-- src/aare_bec_proto/messages.py | 6 +++ src/aare_bec_proto/service.py | 13 +++--- 10 files changed, 148 insertions(+), 14 deletions(-) create mode 100644 logs/BECIPythonClient_CONSOLE.2026-10-16_15-51-45_086484.log.gz create mode 100644 src/aare_bec_proto/actors/core.py create mode 100644 src/aare_bec_proto/actors/mx_automation.py create mode 100644 src/aare_bec_proto/constants.py create mode 100644 src/aare_bec_proto/endpoints.py create mode 100644 src/aare_bec_proto/messages.py diff --git a/logs/BECIPythonClient_CONSOLE.2026-10-16_15-51-45_086484.log.gz b/logs/BECIPythonClient_CONSOLE.2026-10-16_15-51-45_086484.log.gz new file mode 100644 index 0000000000000000000000000000000000000000..214e7f2cded483fc96bab1160ab2383db246eeeb GIT binary patch literal 1182 zcmV;P1Y!FhiwFo{=GAHf|3XDWNlS5<*H_u~?Jpndo?b4hIa_$^RY5e(6jh4B zb03*rW^=}Azw0kWN&OX-JS+U4=p*G~RRYu}MAFOzTS=jXuVlX8RI`;|e##13ifkKe zLrUHqbfSIrIBBhZ#xu1PA2Y&v18;l^@A42Bho=Dh4MR?Fdm@GOu?pr&!;Gm0=J-^m z_k`pm#X$=Hw877%C~bGnW;Po(0%GM#NR6PLSWe$TwXy1&l}xL9bN`JkluoBf8!3u@ z2f^=)&-N=)(rMQ`oQa3-1uAUsX;-ppk>zVCK{GUS7$`RnPjL_oI)l?gN-+~UV+E(X zQB5OUFWF+bjTd-{iFc6SC~`P`P$2CV84SR);=NL=D}A{SOww@D3aB)i?o<0Az6g`S zA^QkFdwwnekKN+OEi-e7z+p>-SBQ!>aNLd*pQ8rC<{F3tr_y7WB{phn#?6^&O3b9K za!Wjkl3m^1-QF42?~>$nM)C*LF9m3pG>2zU4oN_qC_PBQE`NsO1N#5*!CP%;_2@rR zX-*l+$6gqDA@-qB?H?Xszwm2GrkAgnro$D4Bnztix4+ptaKez(XO*TPNV7+(mXvdd zVw@dj2;p&rFA$7Gu3==Npw`CeCz|)oRVHSCT_8pI+*>OuvjrD3!m~>4Hdboz?`U3b zvovJYYORM+FvNt$5l+G+&%;-r!-h8I%Tg3;PG8xWb^32$i@1_f$P}5~0`+Wj4>5%* zp<(9d@OaTMOYw28eMm}7$pquC*3Nbk%=MqV7$=~Uz=hx>IzKf`1Bbhx!H*nexiz}Aa))m@anv^N zbv_#BuodgOP43$yj)p-&f}94UY|oM*g^ zg6hPj|KPT(_{*OQ4(MTUKKkV$9*@428HH=b78T*s?z881N5AqcrBVOx z_1(?e8>{?vh3sv#BnpY#emyBXbWg!$nBJsl+t=&-LHN2}ZC>XG^!~?dWQT$Nll~z^ z_f{(ES*bm{3g@ts6!2GW$Mt!Ydgxd3K#^KYinKs#MRPXaAibmrdX%+g@Y6!>lEf^Y8O<+uic1 z1AY>GLq4-pm361*)@Bm6b>CLL1Nbm{hvB{KzeS>=YjbZtr{Auu!i-I}B7Plk%AGxJ=#YP 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() diff --git a/src/aare_bec_proto/actors/core.py b/src/aare_bec_proto/actors/core.py new file mode 100644 index 0000000..bbc72a7 --- /dev/null +++ b/src/aare_bec_proto/actors/core.py @@ -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: ... diff --git a/src/aare_bec_proto/actors/mx_automation.py b/src/aare_bec_proto/actors/mx_automation.py new file mode 100644 index 0000000..5b62a4c --- /dev/null +++ b/src/aare_bec_proto/actors/mx_automation.py @@ -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()} diff --git a/src/aare_bec_proto/constants.py b/src/aare_bec_proto/constants.py new file mode 100644 index 0000000..3e88485 --- /dev/null +++ b/src/aare_bec_proto/constants.py @@ -0,0 +1,6 @@ +from enum import StrEnum + + +class AareBecWorkflowState(StrEnum): + IDLE = "IDLE" + SAMPLE_MOUNTED = "SAMPLE_MOUNTED" diff --git a/src/aare_bec_proto/endpoints.py b/src/aare_bec_proto/endpoints.py new file mode 100644 index 0000000..bbca507 --- /dev/null +++ b/src/aare_bec_proto/endpoints.py @@ -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, + ) diff --git a/src/aare_bec_proto/main.py b/src/aare_bec_proto/main.py index 6ac06db..a63ab58 100644 --- a/src/aare_bec_proto/main.py +++ b/src/aare_bec_proto/main.py @@ -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() diff --git a/src/aare_bec_proto/messages.py b/src/aare_bec_proto/messages.py new file mode 100644 index 0000000..d23fc77 --- /dev/null +++ b/src/aare_bec_proto/messages.py @@ -0,0 +1,6 @@ +from bec_lib.messages import BECMessage + + +class MXMeasurementSequenceMessage(BECMessage): + sample_id: int + success: bool diff --git a/src/aare_bec_proto/service.py b/src/aare_bec_proto/service.py index 1c66f0d..f1b0d30 100644 --- a/src/aare_bec_proto/service.py +++ b/src/aare_bec_proto/service.py @@ -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)