From 9862fb30a1b2217189dcd9409bc689256fca7466 Mon Sep 17 00:00:00 2001 From: hitz_s Date: Thu, 12 Nov 2026 14:42:37 +0100 Subject: [PATCH] feat(scheduler)!: background blocks, beamline-state gating and action registry Restructure the Scheduler widget and extend its functionality: - New schedule table: line, status, command, report, pause on failure, pause on state, wait for completion. Items running while a background item (wait_for_completion=False) is busy are indented with a guide line. - New execution loop (executor.py) supporting background items and the "Wait until parent has completed" item. - Pause on state: embed BEC's BeamlineStateManager (scan-interlock controls hidden) and gate items on all beamline states being VALID or WARNING; running gated scans are cancelled and repeated after recovery. Replaces the custom signal guard. - Replace eval of command strings with a JSON-serialisable function registry (actions.py). - Add a delay item (hours/minutes/seconds), interruptible by "Pause now". - Add wait for completion / pause on state options to the item dialog; remove the free-form "Other" page. - Remove Microsoft Teams notifications. - Fix: failures without "pause on failure" no longer stop the schedule, start/stop notifications respect the enabled flag, remote updates are no longer ignored after renaming a schedule. - Split the package into models, actions, executor, state monitor and table modules; update docstrings; add unit tests. BREAKING CHANGE: schedules are stored as action + params instead of command strings. Old schedules in Redis are logged and replaced by an empty schedule; old JSON files can no longer be loaded. The RPC methods add_item/edit_item now take action and params instead of command, kind and form_state. Co-Authored-By: Claude Opus 5.5 --- debye_bec/bec_widgets/widgets/client.py | 108 +- .../bec_widgets/widgets/scheduler/actions.py | 512 +++++ .../bec_widgets/widgets/scheduler/enums.py | 11 - .../bec_widgets/widgets/scheduler/executor.py | 442 ++++ .../bec_widgets/widgets/scheduler/guard.py | 188 -- .../widgets/scheduler/guard_dialog.py | 94 - .../widgets/scheduler/interlock_hiding.py | 102 + .../widgets/scheduler/item_dialog.py | 809 +++---- .../bec_widgets/widgets/scheduler/models.py | 170 ++ .../widgets/scheduler/notification_dialog.py | 206 +- .../widgets/scheduler/notifications.py | 171 +- .../widgets/scheduler/qt_widgets.py | 159 -- .../widgets/scheduler/schedule_item.py | 70 - .../widgets/scheduler/schedule_logic.py | 160 +- .../widgets/scheduler/schedule_table.py | 397 ++++ .../widgets/scheduler/scheduler.py | 1902 ++++++----------- .../widgets/scheduler/state_monitor.py | 190 ++ .../bec_widgets/widgets/scheduler/widgets.py | 35 + .../test_scheduler_executor.py | 284 +++ .../tests_bec_widgets/test_scheduler_logic.py | 311 +++ .../test_scheduler_state_monitor.py | 117 + 21 files changed, 3925 insertions(+), 2513 deletions(-) create mode 100644 debye_bec/bec_widgets/widgets/scheduler/actions.py delete mode 100644 debye_bec/bec_widgets/widgets/scheduler/enums.py create mode 100644 debye_bec/bec_widgets/widgets/scheduler/executor.py delete mode 100644 debye_bec/bec_widgets/widgets/scheduler/guard.py delete mode 100644 debye_bec/bec_widgets/widgets/scheduler/guard_dialog.py create mode 100644 debye_bec/bec_widgets/widgets/scheduler/interlock_hiding.py create mode 100644 debye_bec/bec_widgets/widgets/scheduler/models.py delete mode 100644 debye_bec/bec_widgets/widgets/scheduler/qt_widgets.py delete mode 100644 debye_bec/bec_widgets/widgets/scheduler/schedule_item.py create mode 100644 debye_bec/bec_widgets/widgets/scheduler/schedule_table.py create mode 100644 debye_bec/bec_widgets/widgets/scheduler/state_monitor.py create mode 100644 debye_bec/bec_widgets/widgets/scheduler/widgets.py create mode 100644 tests/tests_bec_widgets/test_scheduler_executor.py create mode 100644 tests/tests_bec_widgets/test_scheduler_logic.py create mode 100644 tests/tests_bec_widgets/test_scheduler_state_monitor.py diff --git a/debye_bec/bec_widgets/widgets/client.py b/debye_bec/bec_widgets/widgets/client.py index 14ea1a2..af1dff4 100644 --- a/debye_bec/bec_widgets/widgets/client.py +++ b/debye_bec/bec_widgets/widgets/client.py @@ -145,103 +145,125 @@ class ScanControlXAS(RPCBase): class Scheduler(RPCBase): - """Schedule, persist and execute a sequence of BEC scan/device commands.""" + """Schedule, persist and execute a sequence of BEC actions.""" _IMPORT_MODULE = "debye_bec.bec_widgets.widgets.scheduler.scheduler" @rpc_call - def run_schedule(self, first_item: "int" = 0): + def run_schedule(self, first_item: "int" = 0, retry_unfinished: "bool" = False) -> "None": """ - Run the schedule, continuing from wherever it last left off. + Start (or continue) executing the schedule. + + Already COMPLETED items are skipped. Does nothing if the schedule is already running. + + Args: + first_item: Index of the item to start with; items above it are ignored. + retry_unfinished: If True, FAILED and ABORTED items are executed again. """ @rpc_call - def abort_schedule(self): + def pause_schedule(self) -> "None": """ - Request that the schedule stop after the current item. + Pause after the currently running item(s) have finished. """ @rpc_call - def reset_schedule(self): + def abort_schedule(self) -> "None": """ - Clear all execution state and start the schedule over from item 1. + Pause now: cancel running scans/moves, interrupt delays and stop the schedule. + """ + + @rpc_call + def reset_schedule(self) -> "None": + """ + Mark all items PENDING again and clear their execution state (not while running). """ @rpc_call def get_status(self) -> "dict": """ - RPC-exposed: current schedule state, e.g. for another widget or a script. + The complete schedule (definition and execution state) as a JSON-compatible dict. """ @rpc_call def add_item( self, - command: "str", + action: "str", + params: "dict | None" = None, index: "int | None" = None, - kind: "str" = "custom", - form_state: "dict | None" = None, + wait_for_completion: "bool" = True, pause_on_failure: "bool" = False, + pause_on_state: "bool" = False, ) -> "str": """ - RPC-exposed: insert a new, PENDING command into the schedule. - Safe to call while the schedule is running. + Insert a new PENDING item. Args: - command: command text, evaluated the same way as the existing - items (against `scans`/`dev`) once the schedule runs. - index: position to insert at (0 = first). Clamped so the item - can never land before something already running or - finished. Defaults to appending at the end. - kind/form_state: optional structured description of how - `command` was built (see `schedule_item.ScheduleItem`), - used to reopen the Edit dialog pre-filled. Leave as - defaults for a plain, hand-typed command. + action: Registered action name, e.g. "scan", "move", "delay", "wait_for_parent". + params: JSON-serialisable parameters of the action, e.g. + `{"device": "samx", "value": 1.0, "relative": False}` for "move" or + `{"seconds": 60}` for "delay". + index: Insert position (0 = first). Clamped so the item never lands before an item + that has already started. Defaults to the end. + wait_for_completion: If False, the following items start while this one runs. + pause_on_failure: Pause the schedule if this item fails. + pause_on_state: Only start while all beamline states are VALID/WARNING. Returns: - The new item's item_id. + The id of the new item. + + Raises: + ValueError: Unknown action. + TypeError: Parameters are not JSON-serialisable. """ @rpc_call def edit_item( self, item_id: "str", - command: "str", - kind: "str" = "custom", - form_state: "dict | None" = None, - pause_on_failure: "bool" = True, - ): + action: "str", + params: "dict | None" = None, + wait_for_completion: "bool" = True, + pause_on_failure: "bool" = False, + pause_on_state: "bool" = False, + ) -> "None": """ - RPC-exposed: change the command of an item that has not started - yet. Raises `RuntimeError` for an item that is already - running/finished. + Replace the definition of an item that has not started yet (see `add_item` for the + arguments). + + Raises: + RuntimeError: The item has already started or finished. """ @rpc_call - def delete_item(self, item_id: "str"): + def delete_item(self, item_id: "str") -> "None": """ - RPC-exposed: remove an item that has not started yet. Raises - `RuntimeError` for an item that is already running/finished. + Remove an item that has not started yet. + + Raises: + RuntimeError: The item has already started or finished. """ @rpc_call - def move_item(self, item_id: "str", new_index: "int"): + def move_item(self, item_id: "str", new_index: "int") -> "None": """ - RPC-exposed: move an item that has not started yet to a new - position (0 = first, but never before something already - running/finished). Raises `RuntimeError` for an item that is - already running/finished. + Move an item that has not started yet to `new_index` (never before an item that has + already started). + + Raises: + RuntimeError: The item has already started or finished. """ @rpc_call - def move_item_up(self, item_id: "str"): + def move_item_up(self, item_id: "str") -> "None": """ - RPC-exposed: swap an item with the one directly before it. + Swap an item with the one above it. """ @rpc_call - def move_item_down(self, item_id: "str"): + def move_item_down(self, item_id: "str") -> "None": """ - RPC-exposed: swap an item with the one directly after it. + Swap an item with the one below it. """ diff --git a/debye_bec/bec_widgets/widgets/scheduler/actions.py b/debye_bec/bec_widgets/widgets/scheduler/actions.py new file mode 100644 index 0000000..985e347 --- /dev/null +++ b/debye_bec/bec_widgets/widgets/scheduler/actions.py @@ -0,0 +1,512 @@ +""" +Function registry of the Scheduler widget. + +A schedule item never stores executable code. It stores the *name* of a registered action plus +JSON-serialisable parameters (`ScheduleItem.action` / `ScheduleItem.params`). At execution time +the executor looks the action up in `ACTIONS` and calls its `run` function with an +`ExecutionContext` (BEC shortcuts and beamline helpers) and the stored parameters. This replaces +the former `eval` of command strings: only registered, reviewed functions can ever be executed, +and a schedule stays saveable as plain JSON. + +Adding a new item type means: + +1. writing a `run(ctx, **params)` function (and a `describe(params, beamline)` function for the + "Command" column), +2. registering both with `register_action(ActionSpec(...))`, +3. adding a page to `item_dialog.ScheduleItemDialog` that produces the parameters. +""" + +from __future__ import annotations + +import threading +from dataclasses import dataclass, field +from typing import TYPE_CHECKING, Any, Callable, Literal + +if TYPE_CHECKING: # pragma: no cover + from .models import ScheduleItem + +ActionCategory = Literal["scan", "move", "device", "delay", "control"] + +#: Registry key of the "Wait until parent has completed" control item. +WAIT_FOR_PARENT = "wait_for_parent" +#: Registry key of the delay item. +DELAY = "delay" + + +class ActionInterrupted(Exception): + """Raised by an action that was interrupted by an abort of the schedule.""" + + +@dataclass +class ExecutionContext: + """ + Everything an action may need at execution time. + + Attributes: + scans: BEC scan shortcut (`client.scans`). + dev: BEC device container (`client.device_manager.devices`). + digital_twin: `DigitalTwinCore` instance, if available at this beamline. + auto_gain: `AutoGainCore` instance, if available at this beamline. + beamline: Beamline name, e.g. "x01da". + abort_event: Set when the schedule is aborted or the widget is closed; long-running local + actions (e.g. delays) must return early when it is set. + """ + + scans: Any + dev: Any + digital_twin: Any = None + auto_gain: Any = None + beamline: str | None = None + abort_event: threading.Event = field(default_factory=threading.Event) + + +@dataclass(frozen=True) +class ActionSpec: + """ + Description of one registered schedule action. + + Attributes: + name: Registry key, stored in `ScheduleItem.action` (and therefore in Redis/JSON). Never + rename the key of an existing action, or saved schedules can no longer be loaded. + label: Human-readable name, e.g. for the item dialog. + category: Used for notification filtering and execution rules: + "scan" and "move" submit requests to the BEC scan server, "device" covers other + device actions and beamline tools, "delay" is a local wait and "control" items only + steer the schedule itself. + run: `run(ctx, **params) -> handle`. May block until done, or return a handle with + `wait()` (and optionally `cancel()`), e.g. a BEC `ScanReport`. `None` for control + actions, which are handled by the executor itself. + describe: `describe(params, beamline) -> str`, the text of the "Command" column. + report: Optional `report(handle, item, ctx) -> str | None`, the text of the "Report" + column after a successful execution. Defaults to `default_report`. + cancellable: True if the returned handle supports `cancel()` (e.g. scans/moves), so that + the item can be interrupted when the schedule is aborted or a beamline state trips. + """ + + name: str + label: str + category: ActionCategory + run: Callable[..., Any] | None + describe: Callable[[dict, str | None], str] + report: Callable[[Any, "ScheduleItem", ExecutionContext], str | None] | None = None + cancellable: bool = False + + +ACTIONS: dict[str, ActionSpec] = {} + + +def register_action(spec: ActionSpec) -> ActionSpec: + """ + Add an action to the registry. + + Args: + spec: The action to register. + + Returns: + The registered spec (so this can be used inline). + + Raises: + ValueError: If an action with the same name is already registered. + """ + if spec.name in ACTIONS: + raise ValueError(f"A schedule action named {spec.name!r} is already registered.") + ACTIONS[spec.name] = spec + return spec + + +def get_action(name: str) -> ActionSpec: + """ + Look up a registered action. + + Raises: + KeyError: If no action with this name is registered. + """ + try: + return ACTIONS[name] + except KeyError: + raise KeyError(f"Unknown schedule action {name!r}.") from None + + +def run_action(ctx: ExecutionContext, action: str, params: dict) -> Any: + """ + Execute a registered action - the single dispatch point used by the executor. + + Args: + ctx: Execution context. + action: Registry key of the action. + params: Keyword arguments for the action's `run` function. + + Returns: + Whatever the action returns (e.g. a `ScanReport` to wait on), or None. + + Raises: + KeyError: If the action is not registered. + ValueError: If the action is a control action that cannot be run directly. + """ + spec = get_action(action) + if spec.run is None: + raise ValueError(f"Schedule action {action!r} is handled by the executor.") + return spec.run(ctx, **params) + + +def describe_item(item: "ScheduleItem", beamline: str | None) -> str: + """ + Text for the "Command" column of an item. Never raises: an unknown action or malformed + parameters fall back to a generic representation. + """ + spec = ACTIONS.get(item.action) + if spec is None: + return f"Unknown action '{item.action}' {item.params}" + try: + return spec.describe(item.params, beamline) + except Exception: # pylint: disable=broad-except + return f"{spec.label} {item.params}" + + +def default_report(handle: Any, item: "ScheduleItem", _ctx: ExecutionContext) -> str | None: + """Fallback "Report" text: the scan number, or a short summary of a returned value.""" + if item.scan_number is not None: + return f"Scan #{item.scan_number} done" + if handle is None or hasattr(handle, "wait"): + return "Done" + text = str(handle) + return text if len(text) <= 120 else text[:117] + "..." + + +def format_duration(seconds: float) -> str: + """Format a duration as e.g. "1 h 05 min 00 s" / "2 min 30 s" / "12 s".""" + seconds = max(0, int(round(seconds))) + hours, rest = divmod(seconds, 3600) + minutes, secs = divmod(rest, 60) + if hours: + return f"{hours} h {minutes:02d} min {secs:02d} s" + if minutes: + return f"{minutes} min {secs:02d} s" + return f"{secs} s" + + +def _format_call(name: str, args: list, kwargs: dict) -> str: + parts = [repr(a) for a in args] + parts += [f"{key}={value!r}" for key, value in kwargs.items()] + return f"{name}({', '.join(parts)})" + + +# ---------------------------------------------------------------------------------------------- # +# Scan +# ---------------------------------------------------------------------------------------------- # +def _run_scan(ctx: ExecutionContext, scan_name: str, args: list | None = None, kwargs=None): + scan = getattr(ctx.scans, scan_name, None) + if scan is None or scan_name.startswith("_"): + raise ValueError(f"Scan {scan_name!r} is not available in this BEC session.") + return scan(*(args or []), **(kwargs or {})) + + +def _describe_scan(params: dict, _beamline: str | None) -> str: + scan_name = params["scan_name"] + kwargs = params.get("kwargs") or {} + if scan_name == "xas_simple_scan": + metadata = kwargs.get("metadata") or {} + return ( + f"XAS simple scan from {kwargs.get('start')} eV to {kwargs.get('stop')} eV, " + f"{kwargs.get('scan_time')} s per spectrum, measure {kwargs.get('scan_duration')} s" + f"\nComment: {metadata.get('comment', '')}, " + f"Sample Name: {metadata.get('sample_name', '')}" + ) + return _format_call(f"scans.{scan_name}", params.get("args") or [], kwargs) + + +register_action( + ActionSpec( + name="scan", + label="Scan", + category="scan", + run=_run_scan, + describe=_describe_scan, + cancellable=True, + ) +) + + +# ---------------------------------------------------------------------------------------------- # +# Move +# ---------------------------------------------------------------------------------------------- # +def _run_move(ctx: ExecutionContext, device: str, value: float, relative: bool = False, **_): + return ctx.dev[device].move(value, relative=relative) + + +def _describe_move(params: dict, _beamline: str | None) -> str: + units = params.get("units", "") + if params.get("relative"): + return f"Move {params['device']} by {params['value']:+g} {units}".rstrip() + return f"Move {params['device']} to {params['value']:g} {units}".rstrip() + + +def _report_move(_handle, item: "ScheduleItem", ctx: ExecutionContext) -> str | None: + device = item.params["device"] + try: + position = ctx.dev[device].position + return f"Moved to {position:g} {item.params.get('units', '')}".rstrip() + except Exception: # pylint: disable=broad-except + return "Done" + + +register_action( + ActionSpec( + name="move", + label="Move", + category="move", + run=_run_move, + describe=_describe_move, + report=_report_move, + cancellable=True, + ) +) + + +# ---------------------------------------------------------------------------------------------- # +# Digital twin (X01DA / X10DA) +# ---------------------------------------------------------------------------------------------- # +def _run_digital_twin(ctx: ExecutionContext, config: dict): + if ctx.digital_twin is None: + raise RuntimeError("The digital twin is not available at this beamline.") + result = ctx.digital_twin.move_with_config(config) + if isinstance(result, dict) and not result.get("success", True): + details = {key: result[key] for key in ("failures", "aborted") if result.get(key)} + raise RuntimeError(f"Beamline alignment did not complete: {details}") + + +def _describe_digital_twin(params: dict, beamline: str | None) -> str: + config = params["config"] + monochromatic = config["mo1_mode"] == "Monochromatic" + text = ( + f"Align beamline for {config['energy']:.0f} eV" + if monochromatic + else "Align beamline for pink beam" + ) + text += ( + f"\nFE: {config['h_acc'] * 1e3:.3f} mrad x {config['v_acc'] * 1e3:.3f} mrad" + f"\nCM: {config['cm_stripe']} at {config['cm_pitch'] * 1e3:.3f} mrad" + ) + if monochromatic: + text += f"\nMO1: {config['mo1_xtal']}" + text += f"\nFM: {config['fm_stripe']} at {config['fm_rotx'] * 1e3:.3f} mrad" + if beamline == "x01da": + text += f"\nES1: {config['ot_es1_trz']:.0f} mm" + text += f"\nSMPL_TRZ: {config['es1man_trz']:.3f} mm" + if beamline == "x01da": + text += f"\nSMPL-XRD: {config['smpl_to_xrd']:.3f} mm" + return text + + +register_action( + ActionSpec( + name="digital_twin", + label="Digital Twin", + category="device", + run=_run_digital_twin, + describe=_describe_digital_twin, + report=lambda *_: "Beamline aligned", + ) +) + + +# ---------------------------------------------------------------------------------------------- # +# Auto-gain (X01DA / X10DA) +# ---------------------------------------------------------------------------------------------- # +def _run_auto_gain(ctx: ExecutionContext, config: dict): + if ctx.auto_gain is None: + raise RuntimeError("Auto-gain is not available at this beamline.") + return ctx.auto_gain.start( + element=config["element"], + edge=config["edge"], + amplifier=config["all_amplifiers"], + link_ic0_ic1=config["link_ic0_ic1"], + comp_ring_current=config["compensate_ring_current"], + prohibited_gains=config["prohibited_gains"], + ) + + +def _describe_auto_gain(params: dict, _beamline: str | None) -> str: + config = params["config"] + text = f"Auto-configure gains for {config['element']}, {config['edge']}-edge" + text += f"\nfor devices {config['all_amplifiers']}" + if config["link_ic0_ic1"]: + text += "\nLink IC0 and IC1 gains" + if config["compensate_ring_current"]: + text += "\nCalculate for 400 mA ring current" + if config["prohibited_gains"]: + prohibited = ", ".join( + f"{amp}: [" + ", ".join(f"{gain:.0e}" for gain in gains) + "]" + for amp, gains in config["prohibited_gains"].items() + ) + text += f"\nProhibited gains: {{{prohibited}}}" + return text + + +def _report_auto_gain(handle, _item, _ctx) -> str | None: + if isinstance(handle, dict) and handle: + return "Gains: " + ", ".join( + f"{key}={value:.0e}" if isinstance(value, (int, float)) else f"{key}={value}" + for key, value in handle.items() + ) + return "Done" + + +register_action( + ActionSpec( + name="auto_gain", + label="Auto Gain", + category="device", + run=_run_auto_gain, + describe=_describe_auto_gain, + report=_report_auto_gain, + ) +) + + +# ---------------------------------------------------------------------------------------------- # +# Absorber +# ---------------------------------------------------------------------------------------------- # +ABSORBER_ACTIONS = ("Open", "Force open", "Close") + + +def _run_absorber(ctx: ExecutionContext, action: str): + absorber = ctx.dev["abs"] + if action == "Open": + return absorber.open() + if action == "Force open": + return absorber.open(force=True) + if action == "Close": + return absorber.close() + raise ValueError(f"Unknown absorber action {action!r}.") + + +register_action( + ActionSpec( + name="absorber", + label="Absorber", + category="device", + run=_run_absorber, + describe=lambda params, _bl: f"{params['action']} absorber", + ) +) + + +# ---------------------------------------------------------------------------------------------- # +# Ionization chamber gas filling +# ---------------------------------------------------------------------------------------------- # +def _run_ic_fill( + ctx: ExecutionContext, + chamber: str, + gas1: str, + conc1: float, + gas2: str, + conc2: float, + pressure: float, +): + return ctx.dev[chamber.lower()].fill( + gas1=gas1, conc1=conc1, gas2=gas2, conc2=conc2, pressure=pressure, wait=True + ) + + +register_action( + ActionSpec( + name="ic_fill", + label="Ionization Chamber", + category="device", + run=_run_ic_fill, + describe=lambda p, _bl: ( + f"Fill {p['chamber']} with {p['conc1']:g} % {p['gas1']} and {p['conc2']:g} % " + f"{p['gas2']} to {p['pressure']:g} bar" + ), + ) +) + + +# ---------------------------------------------------------------------------------------------- # +# Reference foil changer +# ---------------------------------------------------------------------------------------------- # +def _run_reffoil(ctx: ExecutionContext, foil: str): + return ctx.dev["reffoilchanger"].insert(ref=foil, wait=True) + + +register_action( + ActionSpec( + name="reffoil", + label="Reference foil changer", + category="device", + run=_run_reffoil, + describe=lambda params, _bl: f"Insert {params['foil']} reference foil", + ) +) + + +# ---------------------------------------------------------------------------------------------- # +# Temperature controller +# ---------------------------------------------------------------------------------------------- # +def _run_temperature_controller(ctx: ExecutionContext, config: dict): + return ctx.dev["univ_temp_cont"].load_config(config=config) + + +def _describe_temperature_controller(params: dict, _beamline: str | None) -> str: + config = params["config"] + match config["power_mode"]: + case "Standby": + return "Switch temperature controller to standby" + case "Configuration": + return "Switch temperature controller to configuration mode" + case _: + return ( + f"Set temperature controller to {config['target_setpoint']} °C" + f"\nwith ramp rate of {config['ramp_rate']} {config['ramp_rate_unit']}" + ) + + +register_action( + ActionSpec( + name="temperature_controller", + label="Temperature Controller", + category="device", + run=_run_temperature_controller, + describe=_describe_temperature_controller, + ) +) + + +# ---------------------------------------------------------------------------------------------- # +# Delay +# ---------------------------------------------------------------------------------------------- # +def _run_delay(ctx: ExecutionContext, seconds: float): + # Event.wait instead of time.sleep, so that aborting the schedule interrupts the delay. + if ctx.abort_event.wait(timeout=max(0.0, float(seconds))): + raise ActionInterrupted("Delay interrupted.") + + +register_action( + ActionSpec( + name=DELAY, + label="Delay", + category="delay", + run=_run_delay, + describe=lambda params, _bl: f"Delay for {format_duration(params['seconds'])}", + report=lambda _h, item, _c: f"Waited {format_duration(item.params['seconds'])}", + ) +) + + +# ---------------------------------------------------------------------------------------------- # +# Wait until parent has completed (control item, executed by the executor itself) +# ---------------------------------------------------------------------------------------------- # +register_action( + ActionSpec( + name=WAIT_FOR_PARENT, + label="Wait until parent has completed", + category="control", + run=None, + describe=lambda _params, _bl: "Wait until parent has completed", + report=lambda *_: "Parent completed", + ) +) + +#: Actions that always block until done; "wait for completion" cannot be switched off for them. +ALWAYS_WAIT_ACTIONS = frozenset({DELAY, WAIT_FOR_PARENT}) diff --git a/debye_bec/bec_widgets/widgets/scheduler/enums.py b/debye_bec/bec_widgets/widgets/scheduler/enums.py deleted file mode 100644 index 71bc706..0000000 --- a/debye_bec/bec_widgets/widgets/scheduler/enums.py +++ /dev/null @@ -1,11 +0,0 @@ -from __future__ import annotations - -from enum import Enum - - -class ScheduleItemStatus(str, Enum): - PENDING = "pending" - RUNNING = "running" - COMPLETED = "completed" - FAILED = "failed" - ABORTED = "aborted" diff --git a/debye_bec/bec_widgets/widgets/scheduler/executor.py b/debye_bec/bec_widgets/widgets/scheduler/executor.py new file mode 100644 index 0000000..614df3b --- /dev/null +++ b/debye_bec/bec_widgets/widgets/scheduler/executor.py @@ -0,0 +1,442 @@ +""" +Execution engine of the Scheduler widget. + +`ScheduleExecutor.run` is executed on a worker thread (started by the widget through +`BECWidget.submit_task`). It never touches Qt widgets: all state changes are written to the +shared `Schedule` under the widget's lock, persisted to Redis through `persist_locked`, and the +GUI is told to redraw through `emit_changed` (which emits a Qt signal, marshalled onto the GUI +thread). + +Execution rules +--------------- +- Items run top to bottom. The next item is re-derived after every step + (`schedule_logic.pick_next_runnable`), so PENDING items can be added, edited, moved or deleted + while the schedule runs. +- `wait_for_completion=True` (default): the item is run and waited for before continuing. +- `wait_for_completion=False` ("background item"): the item is started on its own thread and the + schedule continues with the items of its block (see `schedule_logic`). Before an item outside + that block starts, and at a "Wait until parent has completed" item, the executor waits for the + background item to complete. Children keep running if the parent finishes first. +- `pause_on_state=True`: before the item starts, the executor waits until all beamline states + are VALID or WARNING. If a state turns INVALID while such an item is a running scan, the scan + is cancelled, the item is reset to PENDING and repeated once the states have recovered. Other + item types are not interrupted. +- `pause_on_failure=True`: a failure of the item pauses the schedule after the current step; + otherwise the schedule continues with the next item. +- "Pause after current item" lets running items finish; "Pause now" (abort) additionally cancels + running scans/moves and interrupts delays. +- Closing the widget stops the loop without cancelling anything server-side; the widget + reconciles the persisted state with the BEC queue when it is opened again. +""" + +from __future__ import annotations + +import threading +import time +import traceback +from dataclasses import dataclass, field +from typing import Any, Callable, Literal + +from bec_lib.logger import bec_logger + +from .actions import ( + WAIT_FOR_PARENT, + ActionInterrupted, + ActionSpec, + ExecutionContext, + default_report, + get_action, + run_action, +) +from .models import ItemStatus, ScheduleItem +from .schedule_logic import block_parents, index_of, pick_next_runnable + +logger = bec_logger.logger + +RunOutcome = Literal["finished", "aborted", "paused", "paused_on_failure", "widget_closed"] + +_ACTIVE_QUEUE_STATES = ("PENDING", "RUNNING") +_POLL_INTERVAL = 0.2 + + +def is_queue_item_active(queue_item: Any) -> bool: + """True if a BEC queue item is still pending or running server-side.""" + return str(getattr(queue_item, "status", "")).upper() in _ACTIVE_QUEUE_STATES + + +@dataclass +class _Execution: + """Bookkeeping of one item that is currently being executed.""" + + item: ScheduleItem + spec: ActionSpec + handle: Any = None + interrupted: bool = False # cancelled because a beamline state tripped -> repeat later + done: threading.Event = field(default_factory=threading.Event) + + +class ScheduleExecutor: + """ + Runs the items of a schedule (see the module docstring for the rules). + + Args: + lock: The widget's lock guarding the schedule. + get_items: Returns the live list of schedule items. Only called while holding `lock`. + persist_locked: Writes the schedule to Redis; only called while holding `lock`. + emit_changed: Thread-safe "please redraw" callback. + context: Execution context handed to the registered actions. + state_monitor: Object with `is_ok()`, `wait_until_ok(should_abort)` and + `blocking_states()` (normally a `BeamlineStateMonitor`). + notify_item: Called (without lock) with an item after it COMPLETED, FAILED or ABORTED. + get_queue_storage: Returns BEC's queue storage; used to re-attach to requests that are + still running from a previous widget session. + closing: Set by the widget when it is being closed. + """ + + def __init__( + self, + *, + lock: threading.Lock, + get_items: Callable[[], list[ScheduleItem]], + persist_locked: Callable[[], None], + emit_changed: Callable[[], None], + context: ExecutionContext, + state_monitor: Any, + notify_item: Callable[[ScheduleItem], None], + get_queue_storage: Callable[[], Any], + closing: threading.Event, + ) -> None: + self._lock = lock + self._get_items = get_items + self._persist_locked = persist_locked + self._emit_changed = emit_changed + self._ctx = context + self._state_monitor = state_monitor + self._notify_item = notify_item + self._get_queue_storage = get_queue_storage + self._closing = closing + + self._abort = threading.Event() + self._pause = threading.Event() + self._paused_on_failure = False + self._foreground: _Execution | None = None + self._background: _Execution | None = None + self._attempted: set[str] = set() + + # ------------------------------------------------------------------------------------------ # + # Control (called from the GUI thread) + # ------------------------------------------------------------------------------------------ # + def request_pause(self) -> None: + """Stop after the running item(s) have finished ("Pause after current item").""" + self._pause.set() + + def request_abort(self) -> None: + """Stop now: cancel running scans/moves and interrupt delays ("Pause now").""" + self._abort.set() + self._ctx.abort_event.set() + for execution in (self._foreground, self._background): + if execution is not None and not execution.done.is_set(): + self._cancel(execution) + + def notify_closing(self) -> None: + """The widget is closing: interrupt local waits, but do not cancel server-side work.""" + self._ctx.abort_event.set() + + def on_states_tripped(self) -> None: + """ + A beamline state became INVALID: cancel running scans that have `pause_on_state` set. + They are reset to PENDING and repeated once the states have recovered. + """ + for execution in (self._foreground, self._background): + if execution is None or execution.done.is_set(): + continue + if not (execution.item.pause_on_state and execution.spec.cancellable): + continue + if execution.spec.category != "scan": + continue + logger.info(f"Beamline state tripped: interrupting scan item {execution.item.item_id}") + execution.interrupted = True + self._cancel(execution) + + def active_item_ids(self) -> set[str]: + """Ids of the items currently executed by this executor.""" + return { + execution.item.item_id + for execution in (self._foreground, self._background) + if execution is not None and not execution.done.is_set() + } + + # ------------------------------------------------------------------------------------------ # + # Main loop (worker thread) + # ------------------------------------------------------------------------------------------ # + def run(self, first_item_id: str | None = None, retry_unfinished: bool = False) -> RunOutcome: + """ + Execute the schedule until it is finished, paused, aborted or the widget is closed. + + Args: + first_item_id: Start with this item; items above it are ignored. None starts at the + top (already COMPLETED items are always skipped). + retry_unfinished: Run FAILED and ABORTED items again (once each). + + Returns: + Why the run ended. + """ + self._abort.clear() + self._pause.clear() + self._ctx.abort_event.clear() + self._paused_on_failure = False + self._attempted = set() + self._background = None + self._foreground = None + + outcome: RunOutcome = "finished" + while True: + outcome = self._stop_reason() + if outcome != "finished": + break + if self._background is not None and self._background.done.is_set(): + self._background = None + + with self._lock: + items = self._get_items() + first_index = index_of(items, first_item_id) or 0 + item = pick_next_runnable( + items, + retry_unfinished=retry_unfinished, + first_index=first_index, + active_ids=self.active_item_ids(), + attempted_ids=self._attempted, + ) + in_background_block = False + if item is not None and self._background is not None: + parent = block_parents(items)[index_of(items, item.item_id)] + in_background_block = ( + parent is not None + and items[parent].item_id == self._background.item.item_id + ) + + if item is None: + break + if self._background is not None and not in_background_block: + # Leaving the block of the running background item: wait for it first. + self._join_background() + continue + if item.status == ItemStatus.RUNNING: + self._reattach(item) + continue + if item.action == WAIT_FOR_PARENT: + self._run_wait_for_parent(item) + continue + if item.pause_on_state and not self._wait_for_states(item): + continue + + execution = self._start(item) + if item.wait_for_completion: + self._foreground = execution + self._execute(execution) + self._foreground = None + else: + self._background = execution + threading.Thread( + target=self._execute, + args=(execution,), + name=f"scheduler-background-{item.item_id}", + daemon=True, + ).start() + + if outcome != "widget_closed": + self._join_background() + if self._closing.is_set(): + return "widget_closed" + if outcome == "finished": + # The loop may also have ended because a pause/abort happened while joining. + outcome = self._stop_reason() + return outcome + + def _stop_reason(self) -> RunOutcome: + if self._closing.is_set(): + return "widget_closed" + if self._abort.is_set(): + return "aborted" + if self._pause.is_set(): + return "paused_on_failure" if self._paused_on_failure else "paused" + return "finished" + + def _should_stop_waiting(self) -> bool: + return self._closing.is_set() or self._abort.is_set() or self._pause.is_set() + + # ------------------------------------------------------------------------------------------ # + # Steps + # ------------------------------------------------------------------------------------------ # + def _start(self, item: ScheduleItem) -> _Execution: + spec = get_action(item.action) + with self._lock: + item.reset_execution() + item.status = ItemStatus.RUNNING + item.started_at = time.time() + self._attempted.add(item.item_id) + self._persist_locked() + self._emit_changed() + return _Execution(item=item, spec=spec) + + def _execute(self, execution: _Execution) -> None: + """Run one item to completion and record the outcome. Runs on a worker thread.""" + item, spec = execution.item, execution.spec + final_status: ItemStatus | None = None + try: + handle = run_action(self._ctx, item.action, item.params) + execution.handle = handle + request_id = getattr(getattr(handle, "request", None), "requestID", None) + if request_id is not None: + with self._lock: + item.request_id = request_id + self._persist_locked() + if execution.interrupted or self._abort.is_set(): + self._cancel(execution) + wait = getattr(handle, "wait", None) + if callable(wait): + wait() + if self._closing.is_set(): + return # leave RUNNING; reconciled when the widget is opened again + + scan_id, scan_number = _scan_info(handle) if request_id else (None, None) + with self._lock: + item.scan_id, item.scan_number = scan_id, scan_number + if execution.interrupted: + self._requeue(item) + elif self._abort.is_set() and spec.cancellable: + with self._lock: + item.status = ItemStatus.ABORTED + item.report = "Aborted" + else: + report = (spec.report or default_report)(handle, item, self._ctx) + with self._lock: + item.status = ItemStatus.COMPLETED + item.report = report + except Exception as exc: # pylint: disable=broad-except + if self._closing.is_set(): + return + if execution.interrupted: + self._requeue(item) + elif self._abort.is_set() or isinstance(exc, ActionInterrupted): + with self._lock: + item.status = ItemStatus.ABORTED + item.report = "Aborted" + else: + error = traceback.format_exc() + logger.error(f"Schedule item {item.action} {item.params} failed:\n{error}") + with self._lock: + item.status = ItemStatus.FAILED + item.error = error + item.report = _last_line(error) + if item.pause_on_failure: + self._paused_on_failure = True + self._pause.set() + finally: + if not self._closing.is_set(): + with self._lock: + if item.status != ItemStatus.PENDING: + item.finished_at = time.time() + final_status = item.status + self._persist_locked() + execution.done.set() + self._emit_changed() + + if final_status in (ItemStatus.COMPLETED, ItemStatus.FAILED, ItemStatus.ABORTED): + self._notify_item(item) + + def _requeue(self, item: ScheduleItem) -> None: + with self._lock: + item.reset_execution() + item.report = "Interrupted by beamline state, will be repeated" + logger.info(f"Schedule item {item.item_id} re-queued after beamline state trip") + + def _run_wait_for_parent(self, item: ScheduleItem) -> None: + self._start(item) + self._join_background() + if self._closing.is_set(): + return + with self._lock: + if self._abort.is_set(): + item.status = ItemStatus.ABORTED + item.report = "Aborted" + else: + item.status = ItemStatus.COMPLETED + item.report = get_action(WAIT_FOR_PARENT).report(None, item, self._ctx) + item.finished_at = time.time() + self._persist_locked() + self._emit_changed() + + def _join_background(self) -> None: + """Wait until the running background item (if any) has finished.""" + execution = self._background + if execution is None: + return + while not execution.done.wait(timeout=_POLL_INTERVAL): + if self._closing.is_set(): + return + self._background = None + + def _wait_for_states(self, item: ScheduleItem) -> bool: + """Wait until all beamline states are OK. False if paused/aborted/closed meanwhile.""" + if self._state_monitor.is_ok(): + return True + with self._lock: + blocking = ", ".join(self._state_monitor.blocking_states()) + item.report = f"Waiting for beamline states: {blocking}" + self._persist_locked() + self._emit_changed() + ok = self._state_monitor.wait_until_ok(should_abort=self._should_stop_waiting) + with self._lock: + item.report = None + self._persist_locked() + self._emit_changed() + return ok + + def _reattach(self, item: ScheduleItem) -> None: + """Follow a request that is still RUNNING from a previous widget session.""" + if not item.request_id: + with self._lock: + item.status = ItemStatus.ABORTED + item.report = "Interrupted (widget was closed)" + item.finished_at = time.time() + self._persist_locked() + self._emit_changed() + return + queue_storage = self._get_queue_storage() + while not self._should_stop_waiting(): + queue_item = queue_storage.find_queue_item_by_requestID(item.request_id) + if queue_item is None or not is_queue_item_active(queue_item): + with self._lock: + item.status = ItemStatus.COMPLETED + item.report = item.report or "Done" + item.finished_at = time.time() + self._persist_locked() + self._emit_changed() + return + time.sleep(0.5) + + @staticmethod + def _cancel(execution: _Execution) -> None: + cancel = getattr(execution.handle, "cancel", None) + if not callable(cancel): + return + try: + cancel() + except Exception: # pylint: disable=broad-except + logger.exception(f"Failed to cancel schedule item {execution.item.item_id}") + + +def _scan_info(handle: Any) -> tuple[str | None, int | None]: + try: + scan = handle.scan + except Exception: # pylint: disable=broad-except + return None, None + if scan is None: + return None, None + return getattr(scan, "scan_id", None), getattr(scan, "scan_number", None) + + +def _last_line(text: str) -> str: + lines = text.strip().splitlines() + return lines[-1] if lines else text diff --git a/debye_bec/bec_widgets/widgets/scheduler/guard.py b/debye_bec/bec_widgets/widgets/scheduler/guard.py deleted file mode 100644 index 2f0dd11..0000000 --- a/debye_bec/bec_widgets/widgets/scheduler/guard.py +++ /dev/null @@ -1,188 +0,0 @@ -""" -A small, self-contained utility for "if signal X drops below A, pause; once -it's back above B, resume" (hysteresis) behavior - e.g. auto-pausing scans -while the beam current is too low. - -This module knows nothing about schedules, scans, or the rest of this -plugin. It only monitors one BEC device's live readback value -(`MessageEndpoints.device_readback`, the same endpoint the device server -publishes for every monitored device) and emits Qt signals when the value -crosses one of the two configured thresholds. `schedule_widget.py` is the -only place that connects those signals to schedule-specific behavior -(aborting/deferring a scan item) - see its module docstring for how. -""" - -from __future__ import annotations - -import threading -import time - -from bec_lib.endpoints import MessageEndpoints -from bec_lib.logger import bec_logger -from pydantic import BaseModel -from qtpy.QtCore import QObject, Signal - -logger = bec_logger.logger - -MIN_UPDATE_INTERVAL = 1 - - -class GuardSettings(BaseModel): - """Persisted configuration for one `SignalGuard`.""" - - enabled: bool = False - device_name: str | None = None - pause_below: float | None = None - resume_above: float | None = None - - -class SignalGuard(QObject): - """ - Monitors one device's readback value and reports hysteresis-based - pause/resume crossings. - - - `paused` fires the first time the value drops below `pause_below` - (not again while it stays low). - - `resumed` fires the first time it then climbs back above - `resume_above` (not again while it stays high). - A value sitting between the two thresholds never re-triggers either - signal - that gap is the point of using two thresholds instead of one, - so a value oscillating right at a single cutoff wouldn't cause rapid - pause/resume flapping. - - Callbacks from `RedisConnector.register()` run on a background - (Redis-listener) thread, not the Qt GUI thread; `paused`/`resumed` are - Qt signals, so connecting to them with the default (auto) connection - type safely marshals delivery onto whatever thread the receiver lives - on - no extra locking needed on the receiving end. - """ - - value_update = Signal(float) - paused = Signal(float) - resumed = Signal(float) - - def __init__(self, connector, parent=None, dev=None): - super().__init__(parent) - self.dev = dev - self._connector = connector - self._lock = threading.Lock() - self._clear_event = threading.Event() - self._clear_event.set() # not blocking until configured/proven otherwise - - self.enabled = False - self.device_name: str | None = None - self.pause_below: float | None = None - self.resume_above: float | None = None - self.current_value: float | None = None - self.units: str = "" - self.prec: int = 3 - - self._subscribed_endpoint = None - - self.last_val_update = time.time() - - def configure(self, settings: GuardSettings): - """(Re)configure and (re)subscribe. Safe to call repeatedly, e.g. after editing settings.""" - self._unsubscribe() - - self.enabled = settings.enabled - self.device_name = settings.device_name - self.pause_below = settings.pause_below - self.resume_above = settings.resume_above - self.current_value = None - self._clear_event.set() - - if ( - self.enabled - and self.device_name - and self.pause_below is not None - and self.resume_above is not None - ): - self._subscribed_endpoint = MessageEndpoints.device_readback(self.device_name) - self._connector.register(topics=self._subscribed_endpoint, cb=self._on_readback) - description = self.dev[self.device_name].describe()[self.device_name] - self.units = description["units"] - self.prec = description["precision"] - - def _unsubscribe(self): - if self._subscribed_endpoint is not None: - try: - self._connector.unregister(topics=self._subscribed_endpoint, cb=self._on_readback) - except Exception: # pylint: disable=broad-except - logger.exception( - f"Failed to unsubscribe SignalGuard from {self._subscribed_endpoint}" - ) - self._subscribed_endpoint = None - - def _on_readback(self, msg): - device_msg = getattr(msg, "value", None) - if device_msg is None: - return - value = _extract_value(device_msg, self.device_name) - if value is None: - return - - crossed_pause = crossed_resume = False - with self._lock: - self.current_value = value - if self._clear_event.is_set(): - if value < self.pause_below: - self._clear_event.clear() - crossed_pause = True - elif value > self.resume_above: - self._clear_event.set() - crossed_resume = True - - if crossed_pause: - logger.info( - f"SignalGuard: {self.device_name} dropped to {value} (below {self.pause_below}) - pausing." - ) - self.paused.emit(value) - elif crossed_resume: - logger.info( - f"SignalGuard: {self.device_name} recovered to {value} (above {self.resume_above}) - resuming." - ) - self.resumed.emit(value) - - current_time = time.time() - if current_time - self.last_val_update > MIN_UPDATE_INTERVAL: - self.last_val_update = current_time - self.value_update.emit(value) - - def is_clear(self) -> bool: - """True if not currently blocking (disabled, or value above the resume threshold).""" - return (not self.enabled) or self._clear_event.is_set() - - def wait_until_clear(self, should_abort, poll_interval: float = 0.5) -> bool: - """ - Blocks the calling (non-GUI) thread until `is_clear()` becomes - True, checking `should_abort()` between polls so an operator abort - can still interrupt the wait. Returns True if it cleared, False if - `should_abort()` returned True first. - """ - if self.is_clear(): - return True - while not should_abort(): - if self._clear_event.wait(timeout=poll_interval): - return True - return False - - def cleanup(self): - self._unsubscribe() - - -def _extract_value(device_msg, device_name: str | None): - """ - `DeviceMessage.signals` is keyed by signal name, e.g. {"beam_current": - {"value": ..., "timestamp": ...}, ...}. For a simple scalar device the - primary signal is usually named after the device itself; fall back to - the first signal found if not (e.g. a differently-named primary signal - on a compound device). - """ - signals = getattr(device_msg, "signals", None) or {} - if device_name in signals: - return signals[device_name].get("value") - for signal in signals.values(): - if "value" in signal: - return signal["value"] - return None diff --git a/debye_bec/bec_widgets/widgets/scheduler/guard_dialog.py b/debye_bec/bec_widgets/widgets/scheduler/guard_dialog.py deleted file mode 100644 index 3a7654f..0000000 --- a/debye_bec/bec_widgets/widgets/scheduler/guard_dialog.py +++ /dev/null @@ -1,94 +0,0 @@ -"""Small settings dialog for `guard.GuardSettings` - kept separate from the guard logic itself.""" - -from __future__ import annotations - -from qtpy.QtWidgets import ( - QCheckBox, - QComboBox, - QDialog, - QDialogButtonBox, - QDoubleSpinBox, - QFormLayout, - QLabel, - QMessageBox, - QVBoxLayout, -) - -from .guard import GuardSettings - -_DSPIN_RANGE = (-1e12, 1e12) - - -class GuardSettingsDialog(QDialog): - """Configure (or disable) the auto-pause/resume guard for scan items.""" - - def __init__(self, settings: GuardSettings, device_names: list[str], parent=None): - super().__init__(parent) - self.setWindowTitle("Auto-pause on signal") - - layout = QVBoxLayout(self) - info_label = QLabel( - "If enabled, the currently running scan is aborted as soon as the chosen " - "signal drops below the pause value, and automatically restarted once it " - "rises back above the resume value. Only scan items are affected - device " - "moves and custom/RPC commands are never interrupted by this." - ) - info_label.setWordWrap(True) - layout.addWidget(info_label) - - form = QFormLayout() - layout.addLayout(form) - - self.enabled_check = QCheckBox("Enable auto-pause") - self.enabled_check.setChecked(settings.enabled) - form.addRow("", self.enabled_check) - - self.device_combo = QComboBox() - self.device_combo.addItems(device_names) - if settings.device_name: - idx = self.device_combo.findText(settings.device_name) - if idx >= 0: - self.device_combo.setCurrentIndex(idx) - form.addRow("Signal (device)", self.device_combo) - - self.pause_below_spin = QDoubleSpinBox() - self.pause_below_spin.setDecimals(4) - self.pause_below_spin.setRange(*_DSPIN_RANGE) - if settings.pause_below is not None: - self.pause_below_spin.setValue(settings.pause_below) - form.addRow("Pause below", self.pause_below_spin) - - self.resume_above_spin = QDoubleSpinBox() - self.resume_above_spin.setDecimals(4) - self.resume_above_spin.setRange(*_DSPIN_RANGE) - if settings.resume_above is not None: - self.resume_above_spin.setValue(settings.resume_above) - form.addRow("Resume above", self.resume_above_spin) - - buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) - buttons.accepted.connect(self._on_accept) - buttons.rejected.connect(self.reject) - layout.addWidget(buttons) - - def _on_accept(self): - if self.enabled_check.isChecked(): - if not self.device_combo.currentText(): - QMessageBox.warning(self, "Missing input", "Select a device to monitor.") - return - if self.resume_above_spin.value() <= self.pause_below_spin.value(): - QMessageBox.warning( - self, - "Invalid thresholds", - "'Resume above' must be greater than 'Pause below' (hysteresis gap).", - ) - return - self.accept() - - def result_settings(self) -> GuardSettings: - """Valid after `exec_()` returns `QDialog.Accepted`.""" - return GuardSettings( - enabled=self.enabled_check.isChecked(), - device_name=self.device_combo.currentText() or None, - pause_below=self.pause_below_spin.value(), - resume_above=self.resume_above_spin.value(), - ) diff --git a/debye_bec/bec_widgets/widgets/scheduler/interlock_hiding.py b/debye_bec/bec_widgets/widgets/scheduler/interlock_hiding.py new file mode 100644 index 0000000..655be6a --- /dev/null +++ b/debye_bec/bec_widgets/widgets/scheduler/interlock_hiding.py @@ -0,0 +1,102 @@ +""" +Hide the scan-interlock controls of BEC's stock `BeamlineStateManager`. + +The Scheduler embeds `bec_widgets`' own `BeamlineStateManager` unchanged, so that beamline states +are created, edited and removed exactly as everywhere else in BEC. The Scheduler, however, pauses +its items based on the states itself ("pause on state") and does not use the BEC scan interlock. +To avoid confusion, the interlock-related controls are hidden: + +- the "Scan interlock" toggle in the manager's toolbar, and +- the per-state "watched by scan interlock" lock button (and the interlock-only "Trigger + ScanInterlock on WARNING state" checkbox in the state's settings panel). + +There is no public API for this, so this module touches private attributes of +`BeamlineStateManager` / `BeamlineStatePill`. It is deliberately kept in one place and fails +soft: if `bec_widgets` renames those attributes, the controls simply stay visible and a warning +is logged. +""" + +from __future__ import annotations + +from bec_lib.logger import bec_logger +from bec_widgets.widgets.services.beamline_states.beamline_state_manager import BeamlineStateManager + +# pylint: disable=no-name-in-module # qtpy re-exports are not visible to pylint +from qtpy.QtCore import QTimer + +logger = bec_logger.logger + +_TOOLBAR_ACTIONS = ("scan_interlock", "scan_interlock_spacer") +_PILL_WIDGETS = ("_interlock_button", "_interlock_warning_checkbox") + + +def hide_scan_interlock_controls(manager: BeamlineStateManager) -> None: + """ + Hide the scan-interlock controls of `manager` once. + + Pills are created lazily, so use `install_scan_interlock_hiding` to keep them hidden. + """ + toolbar = getattr(manager, "_toolbar", None) + for name in _TOOLBAR_ACTIONS: + try: + action = toolbar.components.get_action(name).action + action.setVisible(False) + _hide_toolbar_entry_of(toolbar, action) + except Exception: # pylint: disable=broad-except + logger.warning( + f"Scheduler: could not hide BeamlineStateManager toolbar action {name!r}" + ) + try: + toolbar.update_separators() # drop the separator left in front of the hidden button + except Exception: # pylint: disable=broad-except + pass + + for pill in list(getattr(manager, "_state_pills", {}).values()): + for attr in _PILL_WIDGETS: + widget = getattr(pill, attr, None) + if widget is None: + logger.warning(f"Scheduler: BeamlineStatePill has no attribute {attr!r} to hide") + continue + widget.setVisible(False) + + +def _hide_toolbar_entry_of(toolbar, action) -> None: + """ + Hide the toolbar entry that displays `action`. + + Actions with a text label are not added to the toolbar directly: the toolbar holds a + `QToolButton` (with `action` as its default action, or the widget itself for widget actions) + wrapped in its own `QWidgetAction`. That wrapper must be hidden for the button to disappear. + """ + for entry in toolbar.actions(): + widget = toolbar.widgetForAction(entry) + if widget is None: + continue + default_action = getattr(widget, "defaultAction", lambda: None)() + if widget is action or default_action is action: + entry.setVisible(False) + + +def install_scan_interlock_hiding(manager: BeamlineStateManager) -> None: + """ + Hide the scan-interlock controls now and every time the manager (re)creates state pills. + + Pills are persistent editors of the manager's list model, created when rows are inserted; + hiding is therefore deferred to the next event-loop iteration, after the editors exist. + """ + + def _schedule_hide(*_): + QTimer.singleShot(0, manager, lambda: hide_scan_interlock_controls(manager)) + + model = getattr(manager, "_model", None) + if model is None: + logger.warning( + "Scheduler: BeamlineStateManager has no '_model'; interlock hiding is partial" + ) + else: + model.rowsInserted.connect(_schedule_hide) + model.modelReset.connect(_schedule_hide) + model.layoutChanged.connect(_schedule_hide) + model.dataChanged.connect(_schedule_hide) + hide_scan_interlock_controls(manager) + _schedule_hide() diff --git a/debye_bec/bec_widgets/widgets/scheduler/item_dialog.py b/debye_bec/bec_widgets/widgets/scheduler/item_dialog.py index 57a9166..230f173 100644 --- a/debye_bec/bec_widgets/widgets/scheduler/item_dialog.py +++ b/debye_bec/bec_widgets/widgets/scheduler/item_dialog.py @@ -1,10 +1,19 @@ """ -The dialog behind the schedule widget's "Add..."/"Edit..." buttons. +Dialog behind the Scheduler's "Add" and "Edit" buttons. + +Each item type has one page. A page collects the *parameters* of a registered action (see +`actions.py`) - it never builds executable code. The dialog's result is a dict + + {"action": str, "params": dict, "wait_for_completion": bool, + "pause_on_failure": bool, "pause_on_state": bool} + +which maps 1:1 onto `ScheduleItem` fields. """ from __future__ import annotations import math +from typing import Callable from bec_lib.logger import bec_logger from bec_widgets.widgets.control.device_input.device_combobox.device_combobox import ( @@ -13,7 +22,7 @@ from bec_widgets.widgets.control.device_input.device_combobox.device_combobox im ) from bec_widgets.widgets.control.scan_control.scan_control import ScanControl, ScanParameterConfig -# pylint: disable=E0611 +# pylint: disable=no-name-in-module # qtpy re-exports are not visible to pylint from qtpy.QtWidgets import ( QCheckBox, QComboBox, @@ -23,26 +32,45 @@ from qtpy.QtWidgets import ( QFormLayout, QHBoxLayout, QLabel, - QLineEdit, QMessageBox, QScrollArea, + QSpinBox, QStackedWidget, QVBoxLayout, QWidget, ) -from .qt_widgets import MyButton +from .actions import ABSORBER_ACTIONS, ALWAYS_WAIT_ACTIONS, DELAY, WAIT_FOR_PARENT logger = bec_logger.logger -# Beamlines that get the XAS scan control, the Digital Twin item, and the -# beamline-specific quick-fill items (Absorber, Ionization Chamber, -# Reference foil changer, Auto-Gain). -_XAS_BEAMLINES = ("x01da", "x10da") +# UI widgets are created in the _build_* helpers called from __init__. +# pylint: disable=attribute-defined-outside-init + +#: Beamlines with the XAS scan control and the beamline-specific items (Digital Twin, Absorber, +#: Ionization Chamber, Reference foil changer, Auto Gain, Temperature Controller). +XAS_BEAMLINES = ("x01da", "x10da") + + +def _hint(text: str) -> QLabel: + label = QLabel(text) + label.setWordWrap(True) + label.setStyleSheet("color: gray;") + return label class ScheduleItemDialog(QDialog): - """Add or edit one schedule item, via ScanControl, a move form, Digital Twin, or free text.""" + """ + Add or edit one schedule item. + + Args: + scans: BEC scans shortcut. + dev: BEC device container. + parent: Parent widget. + initial: An existing item's `ScheduleItem.definition()` to edit; None to add a new item. + client: BEC client (passed to embedded BEC widgets). + beamline: Beamline name; enables the beamline-specific pages at X01DA/X10DA. + """ def __init__( self, @@ -54,325 +82,285 @@ class ScheduleItemDialog(QDialog): beamline: str | None = None, ): super().__init__(parent) - self.setWindowTitle("Schedule item") + self.setWindowTitle("Edit schedule item" if initial else "Add schedule item") self.setMinimumSize(520, 480) self._scans = scans self._dev = dev self._client = client - + self._result: dict | None = None self.beamline = beamline - self._is_xas_beamline = self.beamline in _XAS_BEAMLINES + self._is_xas_beamline = beamline in XAS_BEAMLINES + + # action key -> collect() returning params / prefill(params) + self._collectors: dict[str, Callable[[], dict]] = {} + self._prefillers: dict[str, Callable[[dict], None]] = {} if self._is_xas_beamline: - logger.info(f"Loading bl-specific modules for beamline {self.beamline}") - from ....bec_ipython_client.plugins.digital_twin_core.digital_twin_core import ( - DigitalTwinCore, - ) - from ..auto_gain.auto_gain import AutoGain - from ..digital_twin.digital_twin import DigitalTwin - from ..edge_selector import EdgeSelector - from ..scan_control_xas.scan_control_xas import ScanControlXAS - from ..temperature_controller.temperature_controller import TemperatureControllerControl - - self.DigitalTwinCore = DigitalTwinCore - self.DigitalTwin = DigitalTwin - self.AutoGain = AutoGain - self.EdgeSelector = EdgeSelector - self.ScanControlXAS = ScanControlXAS - self.TemperatureController = TemperatureControllerControl + self._import_beamline_widgets() layout = QVBoxLayout(self) - self.item_combo = QComboBox() layout.addWidget(self.item_combo) - self.stack = QStackedWidget() - layout.addWidget(self.stack) + layout.addWidget(self.stack, 1) self._build_scan_page() self._build_move_page() if self._is_xas_beamline: self._build_digital_twin_page() - self._build_abs_page() + self._build_absorber_page() self._build_ic_page() self._build_reffoil_page() self._build_auto_gain_page() self._build_temperature_controller_page() - self._build_other_page() + self._build_delay_page() + self._build_wait_for_parent_page() - self.item_combo.currentIndexChanged.connect(self.stack.setCurrentIndex) + # Item options, shared by all pages. + self.wait_for_completion_check = QCheckBox("Wait for completion") + self.wait_for_completion_check.setToolTip( + "Uncheck to run the following items while this one is still running. They are shown " + "indented until a 'Wait until parent has completed' item." + ) + self.wait_for_completion_check.setChecked(True) + self.pause_on_failure_check = QCheckBox("Pause schedule if this item fails") + self.pause_on_state_check = QCheckBox("Pause on beamline state") + self.pause_on_state_check.setToolTip( + "Wait for all beamline states to be valid (or warning) before starting this item. " + "A running scan is interrupted and repeated if a state becomes invalid." + ) + options = QHBoxLayout() + for check in ( + self.wait_for_completion_check, + self.pause_on_failure_check, + self.pause_on_state_check, + ): + options.addWidget(check) + options.addStretch() + layout.addLayout(options) - # Shared across all items (not per-page), since it's a property - # of the schedule item, not of any one form. - self.pause_on_failure_check = QCheckBox("Pause schedule if item execution fails") - layout.addWidget(self.pause_on_failure_check) + self.item_combo.currentIndexChanged.connect(self._on_page_changed) - self.item_combo.currentIndexChanged.connect(self.stack.setCurrentIndex) - - # Dispatch table keyed by the dropdown item's data (see - # _add_page) - one place to register a new item type instead of - # scattering per-item checks through _collect_result(). - self._collectors = { - "scan": self._collect_scan_result, - "move": self._collect_move_result, - "digital_twin": self._collect_digital_twin_result, - "abs": self._collect_abs_result, - "ic": self._collect_ic_result, - "reffoil": self._collect_reffoil_result, - "auto_gain": self._collect_auto_gain_result, - "temperature_controller": self._collect_temperature_controller_result, - "other": self._collect_custom_result, - } - - buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) - buttons.button(QDialogButtonBox.Ok).setText("Add") + buttons = QDialogButtonBox( + QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel + ) + buttons.button(QDialogButtonBox.StandardButton.Ok).setText("Save" if initial else "Add") buttons.setStyleSheet("QPushButton {qproperty-icon: none;}") buttons.accepted.connect(self._on_accept) buttons.rejected.connect(self.reject) layout.addWidget(buttons) - # The embedded DigitalTwin instance runs a 1s polling timer and a - # bec_dispatcher subscription for its whole lifetime - stop both - # when this dialog closes, however it closes (OK, Cancel, or the - # window's own close button), not just on accept. + # The embedded DigitalTwin runs a polling timer and dispatcher subscriptions; stop them + # however the dialog is closed. self.finished.connect(self._cleanup_digital_twin) self._apply_initial(initial or {}) + self._on_page_changed() - # ------------------------------------------------------------------ # - # Dropdown/page plumbing - # ------------------------------------------------------------------ # - def _add_page(self, key: str, label: str, widget: QWidget): - # The combo and the stack are always appended to together, so a - # combo index doubles as a stack index - no separate key->index - # map needed. `key` is what _collect_result()/_apply_initial() - # dispatch on. - self.item_combo.addItem(label, key) + def _import_beamline_widgets(self) -> None: + logger.info(f"Loading beamline-specific item pages for {self.beamline}") + # pylint: disable=import-outside-toplevel + from ..auto_gain.auto_gain import AutoGain + from ..digital_twin.digital_twin import DigitalTwin + from ..scan_control_xas.scan_control_xas import ScanControlXAS + from ..temperature_controller.temperature_controller import TemperatureControllerControl + + self._digital_twin_cls = DigitalTwin + self._auto_gain_cls = AutoGain + self._scan_control_xas_cls = ScanControlXAS + self._temperature_controller_cls = TemperatureControllerControl + + # ------------------------------------------------------------------------------------------ # + # Page plumbing + # ------------------------------------------------------------------------------------------ # + def _add_page( + self, + action: str, + label: str, + widget: QWidget, + collect: Callable[[], dict], + prefill: Callable[[dict], None] | None = None, + ) -> None: + # Combo and stack are always appended together, so a combo index is a stack index. + self.item_combo.addItem(label, action) self.stack.addWidget(widget) + self._collectors[action] = collect + if prefill is not None: + self._prefillers[action] = prefill - def _select_page(self, key: str) -> bool: - """Switch the dropdown to `key`'s page; False if that item isn't available.""" - idx = self.item_combo.findData(key) + def _select_page(self, action: str) -> bool: + """Switch to the page of `action`; False if that page is not available.""" + idx = self.item_combo.findData(action) if idx < 0: return False self.item_combo.setCurrentIndex(idx) return True - # ------------------------------------------------------------------ # - # ScanControl - embeds BEC's own ScanControl widget - # ------------------------------------------------------------------ # - def _build_scan_page(self): + def _on_page_changed(self, *_) -> None: + self.stack.setCurrentIndex(self.item_combo.currentIndex()) + action = self.item_combo.currentData() + always_wait = action in ALWAYS_WAIT_ACTIONS + if always_wait: + self.wait_for_completion_check.setChecked(True) + self.wait_for_completion_check.setEnabled(not always_wait) + is_control = action == WAIT_FOR_PARENT + self.pause_on_failure_check.setEnabled(not is_control) + self.pause_on_state_check.setEnabled(not is_control) + if is_control: + self.pause_on_failure_check.setChecked(False) + self.pause_on_state_check.setChecked(False) + + # ------------------------------------------------------------------------------------------ # + # Scan (embeds BEC's ScanControl, or the XAS variant) + # ------------------------------------------------------------------------------------------ # + def _build_scan_page(self) -> None: page = QWidget() layout = QVBoxLayout(page) - - # client=None resolves to the same process-wide BEC client - # (bec_dispatcher.client) our own widget uses - no second Redis - # connection is opened. - if self._is_xas_beamline: - self.scan_control = self.ScanControlXAS(parent=page, client=self._client) - else: - self.scan_control = ScanControl(parent=page, client=self._client) + scan_control_cls = self._scan_control_xas_cls if self._is_xas_beamline else ScanControl + self.scan_control = scan_control_cls(parent=page, client=self._client) self.scan_control.button_run_scan.hide() layout.addWidget(self.scan_control) + self._add_page("scan", "Scan", page, self._collect_scan, self._prefill_scan) - self._add_page("scan", "ScanControl", page) - - def _collect_scan_result(self) -> dict: - # Same call ScanControl.run_scan() makes before actually - # submitting, to resolve a typed-but-unconfirmed scan name. + def _collect_scan(self) -> dict: + # Same call ScanControl.run_scan() makes to resolve a typed-but-unconfirmed scan name. self.scan_control.validate_scan_selection() scan_name = self.scan_control.current_scan if not scan_name: raise ValueError("No scan selected.") - - # bec_object=False: plain, repr-able values (e.g. a device name - # string rather than the live DeviceBase instance) - needed since - # the result has to survive a round-trip through Redis as text and - # be re-evaluated later, not just used in-process immediately. + # bec_object=False: plain values (device names instead of device objects), so the + # parameters are JSON-serialisable. args, kwargs = self.scan_control.get_scan_parameters(bec_object=False) - command = _format_scan_call(scan_name, args, kwargs) - return { - "command": command, - "kind": "scan", - "form_state": {"scan_name": scan_name, "args": args, "kwargs": kwargs}, - } + return {"scan_name": scan_name, "args": list(args), "kwargs": dict(kwargs)} - def _prefill_scan_page(self, scan_name: str, args: list, kwargs: dict): - # ScanControl restores parameters for a scan from its own config - # cache (see `ScanControl.restore_scan_parameters`); pre-loading - # that cache before switching to the scan reuses that mechanism - # instead of poking at its internal argument widgets directly. + def _prefill_scan(self, params: dict) -> None: + # Pre-load ScanControl's own parameter cache and let it restore the form. + scan_name = params["scan_name"] self.scan_control.config.scans[scan_name] = ScanParameterConfig( - name=scan_name, args=args, kwargs=kwargs + name=scan_name, args=params.get("args") or [], kwargs=params.get("kwargs") or {} ) self.scan_control.current_scan = scan_name self.scan_control.restore_scan_parameters(scan_name) - # ------------------------------------------------------------------ # + # ------------------------------------------------------------------------------------------ # # Move - # ------------------------------------------------------------------ # - def _build_move_page(self): + # ------------------------------------------------------------------------------------------ # + def _build_move_page(self) -> None: page = QWidget() form = QFormLayout(page) form.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.FieldsStayAtSizeHint) - self.move_device_combo = DeviceComboBox(self, device_filter=[BECDeviceFilter.POSITIONER]) - form.addRow("Device", self.move_device_combo) - self.move_value_spin = QDoubleSpinBox() - form.addRow("Value", self.move_value_spin) - self.move_relative_check = QCheckBox("") + form.addRow("Device", self.move_device_combo) + form.addRow("Value", self.move_value_spin) form.addRow("Relative move", self.move_relative_check) + self.move_device_combo.currentTextChanged.connect(self._adjust_move_spinbox) + self._adjust_move_spinbox(self.move_device_combo.currentText()) + self._add_page("move", "Move", page, self._collect_move, self._prefill_move) - self.move_device_combo.currentIndexChanged.connect(self._adjust_spinbox) - - self._add_page("move", "Move", page) - - def _adjust_spinbox(self, _) -> None: - device = self._dev[self.move_device_combo.currentText()] - prec = device.precision - units = device.egu() - ll = device.low_limit - hl = device.high_limit - - self.move_value_spin.setDecimals(prec) - self.move_value_spin.setSuffix(f" {units}") - if (hl - ll) > 0: - self.move_value_spin.setMinimum(ll) - self.move_value_spin.setMaximum(hl) - self.move_value_spin.setSingleStep(10 ** round(math.log10((hl - ll) / 100))) - else: - self.move_value_spin.setMinimum(1e6) - self.move_value_spin.setMaximum(-1e6) + def _adjust_move_spinbox(self, device_name: str) -> None: + """Adapt precision, units and limits of the value spin box to the selected device.""" + if not device_name or device_name not in self._dev: + return + device = self._dev[device_name] + try: + precision, units = device.precision, device.egu() + low, high = device.low_limit, device.high_limit + except Exception: # pylint: disable=broad-except + logger.exception(f"Could not read limits/units of {device_name}") + return + self.move_value_spin.setDecimals(precision) + self.move_value_spin.setSuffix(f" {units}" if units else "") + if high - low > 0: + self.move_value_spin.setRange(low, high) + self.move_value_spin.setSingleStep(10 ** round(math.log10((high - low) / 100))) + else: # no limits configured + self.move_value_spin.setRange(-1e6, 1e6) self.move_value_spin.setSingleStep(1) - def _collect_move_result(self) -> dict: - device_name = self.move_device_combo.currentText() - if not device_name: + def _collect_move(self) -> dict: + device = self.move_device_combo.currentText() + if not device: raise ValueError("No movable device available/selected.") - value = self.move_value_spin.value() - units = self.move_value_spin.suffix() - relative = self.move_relative_check.isChecked() return { - "command": f"dev.{device_name}.move({value!r}, relative={relative!r})", - "kind": "move", - "form_state": { - "device_name": device_name, - "value": value, - "units": units, - "relative": relative, - }, + "device": device, + "value": self.move_value_spin.value(), + "relative": self.move_relative_check.isChecked(), + "units": self.move_value_spin.suffix().strip(), } - def _prefill_move_page(self, state: dict): - idx = self.move_device_combo.findText(state.get("device_name", "")) + def _prefill_move(self, params: dict) -> None: + idx = self.move_device_combo.findText(params.get("device", "")) if idx >= 0: self.move_device_combo.setCurrentIndex(idx) - self.move_value_spin.setValue(float(state.get("value", 0.0))) - self.move_relative_check.setChecked(bool(state.get("relative", False))) + self.move_value_spin.setValue(float(params.get("value", 0.0))) + self.move_relative_check.setChecked(bool(params.get("relative", False))) - # ------------------------------------------------------------------ # - # Digital Twin - embeds the beamline-alignment DigitalTwin widget - # ------------------------------------------------------------------ # - def _build_digital_twin_page(self): + # ------------------------------------------------------------------------------------------ # + # Digital Twin + # ------------------------------------------------------------------------------------------ # + def _build_digital_twin_page(self) -> None: page = QWidget() outer = QVBoxLayout(page) - - # DigitalTwin defaults to a large fixed size (it's normally its - # own top-level window) - wrap it in a scroll area so this dialog - # doesn't have to grow to match it. + # DigitalTwin is normally a large top-level window; wrap it in a scroll area. scroll = QScrollArea() scroll.setWidgetResizable(True) - self.digital_twin = self.DigitalTwin(parent=scroll, client=self._client) - # Hide move and abs open buttons + self.digital_twin = self._digital_twin_cls(parent=scroll, client=self._client) for mover in self.digital_twin.mover.mover_widgets: mover.btn_action.hide() self.digital_twin.mover.abs.btn_action.hide() - scroll.setWidget(self.digital_twin) outer.addWidget(scroll) - - hint = QLabel( - "Configure the beamline alignment above, then confirm with Add below - the " - "computed motor targets are captured now and moved together (in one combined " - "move) when this schedule item runs, not immediately." + outer.addWidget( + _hint( + "Configure the beamline alignment above. The motor targets are computed and moved " + "when this schedule item runs, not now." + ) + ) + self._add_page( + "digital_twin", + "Digital Twin", + page, + lambda: {"config": self.digital_twin.get_assistant_config()}, + lambda params: self.digital_twin.set_assistant_config(params["config"]), ) - hint.setWordWrap(True) - hint.setStyleSheet("color: gray;") - outer.addWidget(hint) - self._add_page("digital_twin", "Digital Twin", page) - - def _collect_digital_twin_result(self) -> dict: - # A frozen snapshot of the configured targets, not a live handle - - # editing the Digital Twin widget afterwards must not affect an - # already-added schedule item (see module docstring). - config = self.digital_twin.get_assistant_config() - cmd = f"digital_twin.move_with_config({config})" - return { - "command": cmd, - "kind": "custom", - "form_state": {"text": cmd, "source": "digital_twin", "config": config}, - } - - def _cleanup_digital_twin(self, *_): + def _cleanup_digital_twin(self, *_) -> None: digital_twin = getattr(self, "digital_twin", None) if digital_twin is None: return try: digital_twin._timer.stop() # pylint: disable=protected-access - except Exception: # pylint: disable=broad-except - logger.exception("Failed to stop the Digital Twin's reality-update timer.") - try: digital_twin.cleanup() except Exception: # pylint: disable=broad-except logger.exception("Failed to clean up the embedded Digital Twin widget.") - def _prefill_digital_twin_page(self, state: dict): - config = state["config"] - self.digital_twin.set_assistant_config(config) - - # ------------------------------------------------------------------ # + # ------------------------------------------------------------------------------------------ # # Absorber - # ------------------------------------------------------------------ # - def _build_abs_page(self): + # ------------------------------------------------------------------------------------------ # + def _build_absorber_page(self) -> None: if "abs" not in self._dev: return page = QWidget() form = QFormLayout(page) form.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.FieldsStayAtSizeHint) - self.abs_selector = QComboBox() - self.abs_selector.addItems(["Open", "Force open", "Close"]) + self.abs_selector.addItems(list(ABSORBER_ACTIONS)) form.addRow("Action", self.abs_selector) - - self._add_page("abs", "Absorber", page) - - def _collect_abs_result(self) -> dict: - action = self.abs_selector.currentText() - suffix = {"Open": "open()", "Force open": "open(force=True)", "Close": "close()"}.get( - action + self._add_page( + "absorber", + "Absorber", + page, + lambda: {"action": self.abs_selector.currentText()}, + lambda params: self._set_combo(self.abs_selector, params.get("action")), ) - if suffix is None: - raise ValueError("Select an absorber action.") - cmd = f"dev.abs.{suffix}" - return { - "command": cmd, - "kind": "custom", - "form_state": {"text": cmd, "source": "abs", "action": action}, - } - def _prefill_abs_form(self, state: dict): - idx = self.abs_selector.findText(state.get("action", "")) - if idx >= 0: - self.abs_selector.setCurrentIndex(idx) - - # ------------------------------------------------------------------ # + # ------------------------------------------------------------------------------------------ # # Ionization chamber - # ------------------------------------------------------------------ # - def _build_ic_page(self): + # ------------------------------------------------------------------------------------------ # + def _build_ic_page(self) -> None: if not all(key in self._dev for key in ("ic0", "ic1", "ic2")): return page = QWidget() @@ -381,27 +369,19 @@ class ScheduleItemDialog(QDialog): self.ic_selector = QComboBox() self.ic_selector.addItems(["IC0", "IC1", "IC2"]) - gases = ["He", "N2", "Ar", "Kr"] - self.gas1 = QComboBox() - self.gas2 = QComboBox() + self.gas1, self.gas2 = QComboBox(), QComboBox() self.gas1.addItems(gases) self.gas2.addItems(gases) - - self.conc1 = QDoubleSpinBox() - self.conc2 = QDoubleSpinBox() + self.conc1, self.conc2 = QDoubleSpinBox(), QDoubleSpinBox() for conc in (self.conc1, self.conc2): conc.setDecimals(0) conc.setSuffix(" %") - conc.setMinimum(0) - conc.setMaximum(100) - conc.setSingleStep(1) - + conc.setRange(0, 100) self.pressure = QDoubleSpinBox() self.pressure.setDecimals(3) self.pressure.setSuffix(" bar abs") - self.pressure.setMinimum(1) - self.pressure.setMaximum(3) + self.pressure.setRange(1, 3) self.pressure.setSingleStep(0.1) form.addRow("Ionization chamber", self.ic_selector) @@ -411,289 +391,192 @@ class ScheduleItemDialog(QDialog): form.addRow("Concentration 2", self.conc2) form.addRow("Pressure", self.pressure) - self.conc1.valueChanged.connect(self._equalize_ic_conc) - self.conc2.valueChanged.connect(self._equalize_ic_conc) + self.conc1.valueChanged.connect(lambda value: self.conc2.setValue(100 - value)) + self.conc2.valueChanged.connect(lambda value: self.conc1.setValue(100 - value)) + self._add_page("ic_fill", "Ionization Chamber", page, self._collect_ic, self._prefill_ic) - self._add_page("ic", "Ionization Chamber", page) - - def _equalize_ic_conc(self, new_val): - if self.conc1.value() == new_val: # conc1 was changed - self.conc2.setValue(100 - new_val) - else: - self.conc1.setValue(100 - new_val) - - def _collect_ic_result(self) -> dict: + def _collect_ic(self) -> dict: total = self.conc1.value() + self.conc2.value() if total != 100: raise ValueError(f"Gas concentrations must add up to 100% (currently {total:.0f}%).") - chamber = self.ic_selector.currentText() - ic = {"IC0": "ic0", "IC1": "ic1", "IC2": "ic2"}[chamber] - cmd = ( - f"dev.{ic}.fill(" - f"gas1='{self.gas1.currentText()}', conc1={self.conc1.value()}, " - f"gas2='{self.gas2.currentText()}', conc2={self.conc2.value()}, " - f"pressure={self.pressure.value()}, wait=True)" - ) return { - "command": cmd, - "kind": "custom", - "form_state": { - "text": cmd, - "source": "ic", - "chamber": chamber, - "gas1": self.gas1.currentText(), - "conc1": self.conc1.value(), - "gas2": self.gas2.currentText(), - "conc2": self.conc2.value(), - "pressure": self.pressure.value(), - }, + "chamber": self.ic_selector.currentText(), + "gas1": self.gas1.currentText(), + "conc1": self.conc1.value(), + "gas2": self.gas2.currentText(), + "conc2": self.conc2.value(), + "pressure": self.pressure.value(), } - def _prefill_ic_form(self, state: dict): - idx = self.ic_selector.findText(state.get("chamber", "")) - if idx >= 0: - self.ic_selector.setCurrentIndex(idx) - idx = self.gas1.findText(state.get("gas1", "")) - if idx >= 0: - self.gas1.setCurrentIndex(idx) - idx = self.gas2.findText(state.get("gas2", "")) - if idx >= 0: - self.gas2.setCurrentIndex(idx) - # Set conc2 last: conc1's valueChanged->_equalize_ic_conc would - # otherwise immediately overwrite it. - self.conc1.setValue(float(state.get("conc1", 0))) - self.conc2.setValue(float(state.get("conc2", 0))) - self.pressure.setValue(float(state.get("pressure", self.pressure.minimum()))) + def _prefill_ic(self, params: dict) -> None: + self._set_combo(self.ic_selector, params.get("chamber")) + self._set_combo(self.gas1, params.get("gas1")) + self._set_combo(self.gas2, params.get("gas2")) + self.conc1.setValue(float(params.get("conc1", 0))) # also sets conc2 = 100 - conc1 + self.pressure.setValue(float(params.get("pressure", self.pressure.minimum()))) - # ------------------------------------------------------------------ # + # ------------------------------------------------------------------------------------------ # # Reference foil changer - # ------------------------------------------------------------------ # - def _build_reffoil_page(self): + # ------------------------------------------------------------------------------------------ # + def _build_reffoil_page(self) -> None: if "reffoilchanger" not in self._dev: return page = QWidget() form = QFormLayout(page) form.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.FieldsStayAtSizeHint) - self.reffoil_selector = QComboBox() - self.reffoil_selector.addItems(self._dev.reffoilchanger.get_all_foils()) + self.reffoil_selector.addItems(self._dev["reffoilchanger"].get_all_foils()) form.addRow("Reference foil", self.reffoil_selector) + self._add_page( + "reffoil", + "Reference foil changer", + page, + self._collect_reffoil, + lambda params: self._set_combo(self.reffoil_selector, params.get("foil")), + ) - self._add_page("reffoil", "Reference foil changer", page) - - def _collect_reffoil_result(self) -> dict: + def _collect_reffoil(self) -> dict: foil = self.reffoil_selector.currentText() if not foil: raise ValueError("Select a reference foil.") - cmd = f"dev.reffoilchanger.insert(ref='{foil}', wait=True)" - return { - "command": cmd, - "kind": "custom", - "form_state": {"text": cmd, "source": "reffoil", "foil": foil}, - } + return {"foil": foil} - def _prefill_reffoil_form(self, state: dict): - idx = self.reffoil_selector.findText(state.get("foil", "")) - if idx >= 0: - self.reffoil_selector.setCurrentIndex(idx) - - # ------------------------------------------------------------------ # - # Auto-Gain - # ------------------------------------------------------------------ # - def _build_auto_gain_page(self): + # ------------------------------------------------------------------------------------------ # + # Auto Gain + # ------------------------------------------------------------------------------------------ # + def _build_auto_gain_page(self) -> None: page = QWidget() outer = QVBoxLayout(page) - scroll = QScrollArea() scroll.setWidgetResizable(True) - self.auto_gain = self.AutoGain(show_control=False, parent=scroll, client=self._client) - + self.auto_gain = self._auto_gain_cls(show_control=False, parent=scroll, client=self._client) scroll.setWidget(self.auto_gain) outer.addWidget(scroll) - - hint = QLabel( - "Configure the auto-gain functionality, then confirm with Add below - the " - "auto-gain will only be executed when this schedule item runs, not immediately." + outer.addWidget( + _hint("Configure the auto-gain above. It is executed when this schedule item runs.") ) - hint.setWordWrap(True) - hint.setStyleSheet("color: gray;") - outer.addWidget(hint) - - self._add_page("auto_gain", "Auto Gain", page) - - def _collect_auto_gain_result(self) -> dict: - config = self.auto_gain.get_config() - - cmd = ( - f"auto_gain.start(element='{config['element']}', edge='{config['edge']}', " - + f"amplifier={config['all_amplifiers']}, link_ic0_ic1={config['link_ic0_ic1']}, " - + f"comp_ring_current= {config['compensate_ring_current']}, " - + f"prohibited_gains={config['prohibited_gains']})" + self._add_page( + "auto_gain", + "Auto Gain", + page, + lambda: {"config": self.auto_gain.get_config()}, + lambda params: self.auto_gain.load_config(params["config"]), ) - return { - "command": cmd, - "kind": "custom", - "form_state": {"text": cmd, "source": "auto_gain", "config": config}, - } - def _prefill_auto_gain_form(self, state: dict): - self.auto_gain.load_config(state["config"]) - - # ------------------------------------------------------------------ # + # ------------------------------------------------------------------------------------------ # # Temperature Controller - # ------------------------------------------------------------------ # - def _build_temperature_controller_page(self): + # ------------------------------------------------------------------------------------------ # + def _build_temperature_controller_page(self) -> None: page = QWidget() outer = QVBoxLayout(page) - scroll = QScrollArea() scroll.setWidgetResizable(True) - self.temperature_controller = self.TemperatureController(config_only=True, parent=scroll) - + self.temperature_controller = self._temperature_controller_cls( + config_only=True, parent=scroll + ) scroll.setWidget(self.temperature_controller) outer.addWidget(scroll) outer.addStretch() + self._add_page( + "temperature_controller", + "Temperature Controller", + page, + lambda: {"config": self.temperature_controller.get_config()}, + lambda params: self.temperature_controller.load_config(params["config"]), + ) - # hint = QLabel( - # "Configure the auto-gain functionality, then confirm with Add below - the " - # "auto-gain will only be executed when this schedule item runs, not immediately." - # ) - # hint.setWordWrap(True) - # hint.setStyleSheet("color: gray;") - # outer.addWidget(hint) - - self._add_page("temperature_controller", "Temperature Controller", page) - - def _collect_temperature_controller_result(self) -> dict: - config = self.temperature_controller.get_config() - - cmd = f"dev.univ_temp_cont.load_config(config={config})" - return { - "command": cmd, - "kind": "custom", - "form_state": {"text": cmd, "source": "temperature_controller", "config": config}, - } - - def _prefill_temperature_controller_form(self, state: dict): - self.temperature_controller.load_config(state["config"]) - - # ------------------------------------------------------------------ # - # Sleep - # ------------------------------------------------------------------ # - def _build_sleep_page(self): + # ------------------------------------------------------------------------------------------ # + # Delay + # ------------------------------------------------------------------------------------------ # + def _build_delay_page(self) -> None: page = QWidget() form = QFormLayout(page) form.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.FieldsStayAtSizeHint) + self.delay_hours = QSpinBox(suffix=" h", maximum=999) + self.delay_minutes = QSpinBox(suffix=" min", maximum=59) + self.delay_seconds = QSpinBox(suffix=" s", maximum=59) + row = QHBoxLayout() + for spin in (self.delay_hours, self.delay_minutes, self.delay_seconds): + row.addWidget(spin) + form.addRow("Wait for", row) + form.addRow(_hint("The schedule waits for the given time before continuing.")) + self._add_page(DELAY, "Delay", page, self._collect_delay, self._prefill_delay) - self.sleep_spin = QDoubleSpinBox(suffix=" s") - form.addRow("Sleep for", self.sleep_spin) + def _collect_delay(self) -> dict: + seconds = ( + self.delay_hours.value() * 3600 + + self.delay_minutes.value() * 60 + + self.delay_seconds.value() + ) + if seconds <= 0: + raise ValueError("The delay must be longer than 0 s.") + return {"seconds": seconds} - self._add_page("sleep", "Sleep", page) + def _prefill_delay(self, params: dict) -> None: + hours, rest = divmod(int(params.get("seconds", 0)), 3600) + minutes, seconds = divmod(rest, 60) + self.delay_hours.setValue(hours) + self.delay_minutes.setValue(minutes) + self.delay_seconds.setValue(seconds) - def _collect_sleep_result(self) -> dict: - value = self.sleep_spin.value() - return { - "command": f"time.sleep({value})", - "kind": "sleep", - "form_state": { - "value": value, - }, - } - - def _prefill_sleep_page(self, state: dict): - self.sleep_spin.setValue(float(state.get("value", 0.0))) - - # ------------------------------------------------------------------ # - # Other - # ------------------------------------------------------------------ # - def _build_other_page(self): + # ------------------------------------------------------------------------------------------ # + # Wait until parent has completed + # ------------------------------------------------------------------------------------------ # + def _build_wait_for_parent_page(self) -> None: page = QWidget() layout = QVBoxLayout(page) layout.addWidget( - QLabel( - "Free-form command, evaluated against `scans` and `dev` - use this for " - "anything the other items don't cover, e.g. an RPC call to another widget." + _hint( + "Waits until the item above that runs without 'Wait for completion' (the parent) " + "has completed. Ends the indented block of items that run while the parent is " + "busy; the following items start only after the parent has finished." ) ) - self.custom_edit = QLineEdit() - # TODO Change to a different placeholder text - self.custom_edit.setPlaceholderText("scans.xas_simple_scan(12000, 14000, 2, 10)") - layout.addWidget(self.custom_edit) - layout.addStretch(1) + layout.addStretch() + self._add_page(WAIT_FOR_PARENT, "Wait until parent has completed", page, dict) - self._add_page("other", "Other", page) + # ------------------------------------------------------------------------------------------ # + # Prefill / result + # ------------------------------------------------------------------------------------------ # + @staticmethod + def _set_combo(combo: QComboBox, text: str | None) -> None: + idx = combo.findText(text or "") + if idx >= 0: + combo.setCurrentIndex(idx) - def _collect_custom_result(self) -> dict: - text = self.custom_edit.text().strip() - if not text: - raise ValueError("Command must not be empty.") - return {"command": text, "kind": "custom", "form_state": {"text": text}} - - # ------------------------------------------------------------------ # - # pre-fill (edit mode) / result extraction - # ------------------------------------------------------------------ # - def _apply_initial(self, initial: dict): + def _apply_initial(self, initial: dict) -> None: + self.wait_for_completion_check.setChecked(bool(initial.get("wait_for_completion", True))) self.pause_on_failure_check.setChecked(bool(initial.get("pause_on_failure", False))) - - logger.info(f"Got initial: {initial}") - - kind = initial.get("kind") - state = initial.get("form_state") or {} - source = state.get("source") - - if kind == "scan" and state.get("scan_name"): - self._prefill_scan_page( - state["scan_name"], state.get("args") or [], state.get("kwargs") or {} - ) - self._select_page("scan") - elif kind == "custom" and source == "digital_twin" and self._select_page("digital_twin"): - self._prefill_digital_twin_page(state) - elif kind == "move" and state.get("device_name"): - self._prefill_move_page(state) - self._select_page("move") - elif kind == "custom" and source == "abs" and self._select_page("abs"): - self._prefill_abs_form(state) - elif kind == "custom" and source == "ic" and self._select_page("ic"): - self._prefill_ic_form(state) - elif kind == "custom" and source == "reffoil" and self._select_page("reffoil"): - self._prefill_reffoil_form(state) - elif kind == "custom" and source == "auto_gain" and self._select_page("auto_gain"): - self._prefill_auto_gain_form(state) - elif kind == "custom" and source == "temperature_controller" and self._select_page("temperature_controller"): - self._prefill_temperature_controller_form(state) - elif initial.get("command"): - # "custom" kind with no (recognized/available) source, or a - # legacy/unrecognized item - fall back to showing the raw - # command text as-is. - self.custom_edit.setText(state.get("text", initial["command"])) - self._select_page("other") - else: - logger.warning(f"Unknown kind: {kind}") - - def _on_accept(self): - try: - result = self._collect_result() - except ValueError as exc: - QMessageBox.warning(self, "Can't build command", str(exc)) + self.pause_on_state_check.setChecked(bool(initial.get("pause_on_state", False))) + action = initial.get("action") + if action is None: return - self._result = result + if not self._select_page(action): + logger.warning(f"Item type {action!r} is not available at this beamline.") + return + prefill = self._prefillers.get(action) + if prefill is not None: + try: + prefill(initial.get("params") or {}) + except Exception: # pylint: disable=broad-except + logger.exception(f"Could not restore the parameters of a {action!r} item") + + def _on_accept(self) -> None: + action = self.item_combo.currentData() + try: + params = self._collectors[action]() + except ValueError as exc: + QMessageBox.warning(self, "Invalid item", str(exc)) + return + self._result = { + "action": action, + "params": params, + "wait_for_completion": self.wait_for_completion_check.isChecked(), + "pause_on_failure": self.pause_on_failure_check.isChecked(), + "pause_on_state": self.pause_on_state_check.isChecked(), + } self.accept() - def _collect_result(self) -> dict: - key = self.item_combo.currentData() - handler = self._collectors.get(key) - if handler is None: - raise ValueError(f"Unknown item type: {key!r}") - result = handler() - result["pause_on_failure"] = self.pause_on_failure_check.isChecked() - return result - - def result(self) -> dict: - """Valid after `exec_()` returns `QDialog.Accepted`.""" + def result(self) -> dict: # pylint: disable=invalid-overridden-method + """The item definition; valid after `exec_()` returned `QDialog.Accepted`.""" return self._result - - -def _format_scan_call(scan_name: str, args: list, kwargs: dict) -> str: - parts = [repr(a) for a in args] - parts += [f"{name}={value!r}" for name, value in kwargs.items()] - return f"scans.{scan_name}({', '.join(parts)})" diff --git a/debye_bec/bec_widgets/widgets/scheduler/models.py b/debye_bec/bec_widgets/widgets/scheduler/models.py new file mode 100644 index 0000000..2c4f71b --- /dev/null +++ b/debye_bec/bec_widgets/widgets/scheduler/models.py @@ -0,0 +1,170 @@ +""" +Persisted data model of the Scheduler widget. + +Everything in here is a plain pydantic model. The whole `Schedule` document is stored in Redis as +the `.value` payload of a `bec_lib.messages.VariableMessage` (see `endpoints.schedule`) and can be +saved to / loaded from a JSON file. All fields must therefore stay JSON-serialisable; in +particular, a `ScheduleItem` describes *what* to run as a registry key (`action`) plus plain +parameters (`params`) - never as executable code (see `actions.py`). +""" + +from __future__ import annotations + +import uuid +from enum import Enum +from typing import Any + +from pydantic import AliasChoices, BaseModel, ConfigDict, Field, ValidationError, field_validator + + +class ItemStatus(str, Enum): + """Execution status of one schedule item.""" + + PENDING = "pending" + RUNNING = "running" + COMPLETED = "completed" + FAILED = "failed" + ABORTED = "aborted" + + +class NotificationTarget(str, Enum): + """BEC messaging service used to send schedule notifications.""" + + SCILOG = "scilog" + SIGNAL = "signal" + + +class NotificationSettings(BaseModel): + """Per-schedule notification preferences (see `notifications.py` for the send logic).""" + + model_config = ConfigDict(extra="ignore", populate_by_name=True) + + enabled: bool = False + notify_scan: bool = True + notify_move: bool = True + # Device actions and beamline tools (digital twin, auto-gain, ...). Older schedules stored + # this flag as "notify_rpc". + notify_device: bool = Field( + default=True, validation_alias=AliasChoices("notify_device", "notify_rpc") + ) + + target: NotificationTarget = NotificationTarget.SCILOG + events: list[ItemStatus] = Field(default_factory=list) + signal_number: str | None = None # a phone number, or a pre-registered Signal scope + + @field_validator("target", mode="before") + @classmethod + def _map_legacy_target(cls, value: Any) -> Any: + """Map targets that are no longer supported (e.g. the removed "teams") to SciLog.""" + valid = {target.value for target in NotificationTarget} + if isinstance(value, NotificationTarget): + return value + if not isinstance(value, str) or value not in valid: + return NotificationTarget.SCILOG + return value + + +class ScheduleItem(BaseModel): + """ + One entry of the schedule: what to run, how to run it, and its execution bookkeeping. + + Attributes: + item_id: Stable unique id, used to address the item from the UI and over RPC. + action: Key of the action in the function registry (`actions.ACTIONS`). + params: JSON-serialisable keyword arguments passed to the action. + status: Current execution status. + wait_for_completion: If False, the item is started and the schedule immediately + continues with the following items ("background item"). The following items are + shown indented until a "Wait until parent has completed" item, the next background + item or the end of the schedule. + pause_on_failure: Pause the schedule if this item fails. + pause_on_state: Only start this item while all beamline states are VALID or WARNING. + A gated scan that is running when a state becomes INVALID is cancelled and repeated + once the states have recovered. + report: Short human-readable result, shown in the "Report" column. + request_id / scan_id / scan_number: Set for items that submit a request to the BEC + scan server; used to re-attach to a still-running request after a widget restart. + error: Full traceback of the last failure. + started_at / finished_at: Unix timestamps of the last execution. + """ + + model_config = ConfigDict(extra="ignore") + + item_id: str = Field(default_factory=lambda: str(uuid.uuid4())) + action: str + params: dict[str, Any] = Field(default_factory=dict) + status: ItemStatus = ItemStatus.PENDING + + wait_for_completion: bool = True + pause_on_failure: bool = False + pause_on_state: bool = False + + report: str | None = None + request_id: str | None = None + scan_id: str | None = None + scan_number: int | None = None + error: str | None = None + started_at: float | None = None + finished_at: float | None = None + + def reset_execution(self) -> None: + """Clear all execution bookkeeping and mark the item PENDING again.""" + self.status = ItemStatus.PENDING + self.report = None + self.request_id = None + self.scan_id = None + self.scan_number = None + self.error = None + self.started_at = None + self.finished_at = None + + def definition(self) -> dict[str, Any]: + """The user-defined part of the item (no id, no execution state), e.g. for copy/paste.""" + return { + "action": self.action, + "params": dict(self.params), + "wait_for_completion": self.wait_for_completion, + "pause_on_failure": self.pause_on_failure, + "pause_on_state": self.pause_on_state, + } + + +class Schedule(BaseModel): + """The full persisted state of one named schedule.""" + + model_config = ConfigDict(extra="ignore") + + schedule_name: str + items: list[ScheduleItem] = Field(default_factory=list) + is_running: bool = False + notes: str = "" + notifications: NotificationSettings = Field(default_factory=NotificationSettings) + + +def is_legacy_schedule(data: Any) -> bool: + """ + True if `data` looks like a schedule written by the old, eval-based Scheduler widget + (items described by a `command` string instead of an `action` registry key). + """ + if not isinstance(data, dict): + return False + items = data.get("items") or [] + return any( + isinstance(item, dict) and "command" in item and "action" not in item for item in items + ) + + +def schedule_from_document(data: Any) -> tuple[Schedule | None, str | None]: + """ + Validate a persisted schedule document (from Redis or a JSON file) without raising. + + Returns: + `(schedule, None)` if the document is valid, otherwise `(None, problem)` where `problem` + is a short human-readable description, e.g. "an old schedule format". + """ + if is_legacy_schedule(data): + return None, "an old schedule format" + try: + return Schedule.model_validate(data), None + except (ValidationError, TypeError, KeyError, AttributeError) as exc: + return None, f"an invalid schedule ({exc.__class__.__name__})" diff --git a/debye_bec/bec_widgets/widgets/scheduler/notification_dialog.py b/debye_bec/bec_widgets/widgets/scheduler/notification_dialog.py index cc7dd81..a2f3e99 100644 --- a/debye_bec/bec_widgets/widgets/scheduler/notification_dialog.py +++ b/debye_bec/bec_widgets/widgets/scheduler/notification_dialog.py @@ -1,12 +1,10 @@ -""" -Settings dialog for `notifications.NotificationSettings` - kept separate -from the send logic itself (`notifications.py`). -""" +"""Settings dialog for the schedule notifications (`models.NotificationSettings`).""" from __future__ import annotations -from bec_lib.logger import bec_logger +# pylint: disable=no-name-in-module # qtpy re-exports are not visible to pylint from qtpy.QtWidgets import ( + QButtonGroup, QCheckBox, QComboBox, QDialog, @@ -15,21 +13,18 @@ from qtpy.QtWidgets import ( QLabel, QMessageBox, QPushButton, + QRadioButton, QVBoxLayout, ) -from .enums import ScheduleItemStatus -from .notifications import NotificationSettings, NotificationTarget, send_notification - -logger = bec_logger.logger +from .models import ItemStatus, NotificationSettings, NotificationTarget +from .notifications import send_notification def _service_state(client, name: str) -> tuple[bool, list[str]]: """ - Best-effort read of a messaging service's enabled/scopes state (see - the module docstring in notifications.py for why this is the only - introspection available). Never raises - a client without `messaging` - set up yet just looks like "not enabled, no scopes". + Best-effort read of a messaging service's enabled flag and registered scopes. BEC offers no + public accessor for this, hence the private attributes. Never raises. """ messaging = getattr(client, "messaging", None) service = getattr(messaging, name, None) if messaging is not None else None @@ -38,8 +33,21 @@ def _service_state(client, name: str) -> tuple[bool, list[str]]: return bool(getattr(service, "_enabled", False)), sorted(getattr(service, "_scopes", set())) +def _dim_label(text: str) -> QLabel: + label = QLabel(text) + label.setStyleSheet("color: gray;") + label.setWordWrap(True) + return label + + class NotificationSettingsDialog(QDialog): - """Choose whether/where/for-which-item-types finish/fail notifications are sent.""" + """Choose whether, where and for which items/events notifications are sent.""" + + _EVENTS = ( + (ItemStatus.COMPLETED, "Completed"), + (ItemStatus.ABORTED, "Aborted"), + (ItemStatus.FAILED, "Failed"), + ) def __init__(self, settings: NotificationSettings, client, parent=None): super().__init__(parent) @@ -48,12 +56,12 @@ class NotificationSettingsDialog(QDialog): self.setMinimumWidth(380) layout = QVBoxLayout(self) - info_label = QLabel( - "Sends a message through the selected BEC messaging service whenever a " - "schedule item finishes or fails." + info = QLabel( + "Sends a message through the selected BEC messaging service when the schedule " + "starts or stops, and when an item finishes." ) - info_label.setWordWrap(True) - layout.addWidget(info_label) + info.setWordWrap(True) + layout.addWidget(info) self.enabled_check = QCheckBox("Send notifications") self.enabled_check.setChecked(settings.enabled) @@ -65,51 +73,34 @@ class NotificationSettingsDialog(QDialog): self.scan_check.setChecked(settings.notify_scan) self.move_check = QCheckBox("Movements") self.move_check.setChecked(settings.notify_move) - self.rpc_check = QCheckBox("RPC / custom commands") - self.rpc_check.setChecked(settings.notify_rpc) - for check in (self.scan_check, self.move_check, self.rpc_check): + self.device_check = QCheckBox("Device actions and beamline tools") + self.device_check.setChecked(settings.notify_device) + for check in (self.scan_check, self.move_check, self.device_check): kind_layout.addWidget(check) layout.addWidget(kind_box) - target_box = QGroupBox("Send via") - target_layout = QVBoxLayout(target_box) - self.scilog_check = QCheckBox("SciLog") - self.teams_check = QCheckBox("Microsoft Teams") - self.signal_check = QCheckBox("Signal Messenger") - for check in (self.scilog_check, self.teams_check, self.signal_check): - target_layout.addWidget(check) - layout.addWidget(target_box) - - event_box = QGroupBox("Send on") + event_box = QGroupBox("Send when an item is") event_layout = QVBoxLayout(event_box) - self.completed_check = QCheckBox("Completed") - self.aborted_check = QCheckBox("Aborted") - self.failed_check = QCheckBox("Failed") - for check in (self.completed_check, self.aborted_check, self.failed_check): + self._event_checks: dict[ItemStatus, QCheckBox] = {} + for status, label in self._EVENTS: + check = QCheckBox(label) + check.setChecked(status in settings.events) event_layout.addWidget(check) + self._event_checks[status] = check layout.addWidget(event_box) - # Teams: contact/channel can only be a scope an admin has already - # registered for the deployment - there's no way to address an - # arbitrary Teams user from the client, so this is a dropdown, not - # free text (see notifications.py's module docstring). - self.teams_box = QGroupBox("Teams settings") - teams_layout = QVBoxLayout(self.teams_box) - teams_enabled, teams_scopes = _service_state(client, "teams") - self.teams_combo = QComboBox() - self.teams_combo.addItems(teams_scopes) - self.teams_combo.setEnabled(teams_enabled) - teams_layout.addWidget(QLabel("Contact or channel")) - teams_layout.addWidget(self.teams_combo) - if not teams_enabled: - teams_layout.addWidget(_dim_label("Teams messaging is not enabled for this session.")) - elif not teams_scopes: - teams_layout.addWidget(_dim_label("No Teams contacts/channels are registered.")) - layout.addWidget(self.teams_box) + target_box = QGroupBox("Send via") + target_layout = QVBoxLayout(target_box) + self.scilog_radio = QRadioButton("SciLog") + self.signal_radio = QRadioButton("Signal Messenger") + self._target_group = QButtonGroup(self) + for radio in (self.scilog_radio, self.signal_radio): + self._target_group.addButton(radio) + target_layout.addWidget(radio) + layout.addWidget(target_box) - # Signal: a raw phone number works directly (BEC normalizes it - - # see the docs), so this is an editable combo box: type a number, - # or pick a pre-registered scope if any exist. + # Signal: a raw phone number works directly, so this is an editable combo box offering + # any pre-registered scopes as suggestions. self.signal_box = QGroupBox("Signal settings") signal_layout = QVBoxLayout(self.signal_box) signal_enabled, signal_scopes = _service_state(client, "signal") @@ -117,115 +108,64 @@ class NotificationSettingsDialog(QDialog): self.signal_combo.setEditable(True) self.signal_combo.addItems(signal_scopes) self.signal_combo.setEnabled(signal_enabled) - signal_layout.addWidget(QLabel("Phone number or contact")) - self.signal_combo.setEditText("") + self.signal_combo.setEditText(settings.signal_number or "") self.signal_combo.lineEdit().setPlaceholderText("e.g. +41791234567") + signal_layout.addWidget(QLabel("Phone number or contact")) signal_layout.addWidget(self.signal_combo) if not signal_enabled: signal_layout.addWidget(_dim_label("Signal messaging is not enabled for this session.")) layout.addWidget(self.signal_box) - # pre-fill from the persisted settings - { - NotificationTarget.SCILOG: self.scilog_check, - NotificationTarget.TEAMS: self.teams_check, - NotificationTarget.SIGNAL: self.signal_check, - }.get(settings.target, self.scilog_check).setChecked(True) - for event in settings.events: - { - ScheduleItemStatus.COMPLETED: self.completed_check, - ScheduleItemStatus.ABORTED: self.aborted_check, - ScheduleItemStatus.FAILED: self.failed_check, - }.get(event).setChecked(True) - if settings.teams_scope: - idx = self.teams_combo.findText(settings.teams_scope) - if idx >= 0: - self.teams_combo.setCurrentIndex(idx) - if settings.signal_number: - self.signal_combo.setEditText(settings.signal_number) - - self.scilog_check.toggled.connect(lambda *_: self._sync_target_widgets(self.scilog_check)) - self.teams_check.toggled.connect(lambda *_: self._sync_target_widgets(self.teams_check)) - self.signal_check.toggled.connect(lambda *_: self._sync_target_widgets(self.signal_check)) + if settings.target == NotificationTarget.SIGNAL: + self.signal_radio.setChecked(True) + else: + self.scilog_radio.setChecked(True) + self.signal_radio.toggled.connect(self._sync_target_widgets) self.test_btn = QPushButton("Send test message") self.test_btn.clicked.connect(self._on_test_clicked) layout.addWidget(self.test_btn) - buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) + buttons = QDialogButtonBox( + QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel + ) buttons.accepted.connect(self.accept) buttons.rejected.connect(self.reject) layout.addWidget(buttons) self._sync_target_widgets() - def _sync_target_widgets(self, current_button: QCheckBox = None): - if current_button is None: - for button in [self.scilog_check, self.teams_check, self.signal_check]: - if button.isChecked(): - current_button = button - break - if current_button.isChecked(): - for button in [self.scilog_check, self.teams_check, self.signal_check]: - if button != current_button: - button.setChecked(False) - if current_button == self.teams_check: - self.teams_box.setVisible(True) - self.signal_box.setVisible(False) - elif current_button == self.signal_check: - self.teams_box.setVisible(False) - self.signal_box.setVisible(True) - else: - self.teams_box.setVisible(False) - self.signal_box.setVisible(False) - self.layout().activate() - self.adjustSize() + def _sync_target_widgets(self, *_) -> None: + self.signal_box.setVisible(self.signal_radio.isChecked()) + self.layout().activate() + self.adjustSize() def _current_settings(self) -> NotificationSettings: - if self.scilog_check.isChecked(): - target = NotificationTarget.SCILOG - elif self.teams_check.isChecked(): - target = NotificationTarget.TEAMS - elif self.signal_check.isChecked(): - target = NotificationTarget.SIGNAL - else: - raise ValueError(f"Target {target} not supported") - - events = [] - if self.completed_check.isChecked(): - events.append(ScheduleItemStatus.COMPLETED) - if self.aborted_check.isChecked(): - events.append(ScheduleItemStatus.ABORTED) - if self.failed_check.isChecked(): - events.append(ScheduleItemStatus.FAILED) - + target = ( + NotificationTarget.SIGNAL + if self.signal_radio.isChecked() + else NotificationTarget.SCILOG + ) return NotificationSettings( enabled=self.enabled_check.isChecked(), notify_scan=self.scan_check.isChecked(), notify_move=self.move_check.isChecked(), - notify_rpc=self.rpc_check.isChecked(), + notify_device=self.device_check.isChecked(), target=target, - events=events, - teams_scope=self.teams_combo.currentText().strip() or None, + events=[status for status, check in self._event_checks.items() if check.isChecked()], signal_number=self.signal_combo.currentText().strip() or None, ) - def _on_test_clicked(self): - settings = self._current_settings() + def _on_test_clicked(self) -> None: try: - send_notification(self._client, settings, "Test message from the BEC schedule widget.") + send_notification( + self._client, self._current_settings(), "Test message from the BEC Scheduler." + ) except Exception as exc: # pylint: disable=broad-except QMessageBox.critical(self, "Test message failed", str(exc)) return QMessageBox.information(self, "Test message sent", "The test message was sent.") def result_settings(self) -> NotificationSettings: - """Valid after `exec_()` returns `QDialog.Accepted`.""" + """The edited settings; valid after `exec_()` returned `QDialog.Accepted`.""" return self._current_settings() - - -def _dim_label(text: str) -> QLabel: - label = QLabel(text) - label.setStyleSheet("color: gray;") - label.setWordWrap(True) - return label diff --git a/debye_bec/bec_widgets/widgets/scheduler/notifications.py b/debye_bec/bec_widgets/widgets/scheduler/notifications.py index bd8fbf3..fd0169b 100644 --- a/debye_bec/bec_widgets/widgets/scheduler/notifications.py +++ b/debye_bec/bec_widgets/widgets/scheduler/notifications.py @@ -1,158 +1,113 @@ """ -A small, self-contained "tell the user when something finished/failed" -feature, independent of scheduling/execution logic. +"Tell the user when something finished/failed" - independent of the scheduling logic. -Sends through BEC's own `client.messaging` container -(`bec_lib.messaging_services.MessagingContainer`, exposed as `bec.messaging` -in the IPython client) - the same mechanism documented at -https://bec.readthedocs.io/latest/how-to/general/send-messages-to-signal.html -and +Messages are sent through BEC's own messaging services (`client.messaging`), see +https://bec.readthedocs.io/latest/how-to/general/send-messages-to-signal.html and https://bec.readthedocs.io/latest/how-to/general/send-messages-to-scilog.html: - bec.messaging.signal.new("Beamline checks completed.").send(scope="+41791234567") bec.messaging.scilog.new("Beamline checks completed.").send() - bec.messaging.teams.new("Beamline checks completed.").send(scope=) + bec.messaging.signal.new("Beamline checks completed.").send(scope="+41791234567") -Notes on each service, read directly from `bec_lib.messaging_services` -rather than assumed: +- **SciLog** needs no scope: it posts to the logbook of the currently active pgroup. +- **Signal** accepts a raw phone number as scope (BEC normalises it, defaulting to the Swiss + country code) or the name of a pre-registered scope. -- **SciLog** needs no `scope` - it posts to the logbook of the currently - active pgroup automatically. -- **Signal** accepts a raw phone number as `scope` (BEC normalizes it, - defaulting to the Swiss country code for numbers without one - see the - docs above) *or* a pre-registered scope name for a group. -- **Teams** is a plain `MessagingService` with no Teams-specific methods: - a contact/channel can only be addressed by a `scope` an admin has - already registered for the deployment (`service._scopes`) - there is no - way to address an arbitrary Teams user/email directly from the client. - This is why the Teams UI in `notification_dialog.py` is a dropdown of - known scopes rather than free text, while Signal's is free text (with - any registered scopes offered as suggestions). - -`client.messaging.._enabled` / `._scopes` are the only -introspection available (no public accessor exists at the time of -writing) - used here and in the dialog to grey out a target that isn't -configured for the current session instead of only failing at send time. +The settings model (`NotificationSettings`) lives in `models.py`, as it is persisted with the +schedule. """ from __future__ import annotations -from enum import Enum +from typing import Literal from bec_lib.logger import bec_logger -from pydantic import BaseModel -from .enums import ScheduleItemStatus +from .actions import ACTIONS +from .models import NotificationSettings, NotificationTarget, ScheduleItem logger = bec_logger.logger +ScheduleState = Literal[ + "started", "finished", "aborted", "paused", "paused_on_failure", "widget_closed" +] -class NotificationTarget(str, Enum): - SCILOG = "scilog" - TEAMS = "teams" - SIGNAL = "signal" +_STATE_TEXT: dict[str, str] = { + "started": "has started", + "finished": "has finished", + "aborted": "was aborted", + "paused": "was paused", + "paused_on_failure": "was paused because an item failed", + "widget_closed": "has stopped prematurely because the widget was closed", +} -class NotificationSettings(BaseModel): - """Persisted per-schedule notification preferences.""" - - enabled: bool = False - notify_scan: bool = True - notify_move: bool = True - # "custom" is what this plugin calls the free-text tab (see - # item_dialog.py) - it covers RPC calls to other widgets as well as - # any hand-typed command, so it's exposed to the user as "RPC". - notify_rpc: bool = True - - target: NotificationTarget = NotificationTarget.SCILOG - events: list[ScheduleItemStatus] = [] - teams_scope: str | None = None # a pre-registered Teams scope (channel/contact) - signal_number: str | None = None # a phone number, or a pre-registered Signal scope - - -def _enabled_for_kind(settings: NotificationSettings, kind: str) -> bool: - if not settings.enabled: - return False +def _enabled_for_category(settings: NotificationSettings, category: str) -> bool: return { "scan": settings.notify_scan, "move": settings.notify_move, - "custom": settings.notify_rpc, - }.get(kind, False) + "device": settings.notify_device, + }.get(category, False) def send_notification(client, settings: NotificationSettings, text: str) -> None: """ - Send `text` through whichever service `settings.target` selects. + Send `text` through the service selected by `settings.target`. - Raises on failure (no target selected, missing Teams scope/Signal - number, the target service not enabled for this session, ...) - - callers that want a fire-and-forget send should catch around this. - `notify_item_finished` does; the dialog's "Send test message" button - deliberately does not, so the operator sees exactly what went wrong. + Raises: + ValueError: If the target is not configured (e.g. no Signal number). + Exception: Whatever the BEC messaging service raises. Callers that want a + fire-and-forget send must catch it; the dialog's "Send test message" button + deliberately does not, so the operator sees what went wrong. """ if settings.target == NotificationTarget.SCILOG: client.messaging.scilog.new(text).send() - elif settings.target == NotificationTarget.TEAMS: - if not settings.teams_scope: - raise ValueError("No Teams contact/channel selected.") - client.messaging.teams.new(text).send(scope=settings.teams_scope) elif settings.target == NotificationTarget.SIGNAL: if not settings.signal_number: raise ValueError("No Signal phone number/contact set.") client.messaging.signal.new(text).send(scope=settings.signal_number) else: - raise ValueError("No notification target selected.") + raise ValueError(f"Unsupported notification target {settings.target!r}.") def notify_item_finished( - client, - kind: str, - command: str, - scan_number: str, - final_status: ScheduleItemStatus, - settings: NotificationSettings, - error: str | None = None, -): + client, settings: NotificationSettings, item: ScheduleItem, description: str +) -> None: """ - Sends a message if notifications are enabled for `kind` and a target - is configured. Never raises - a notification failing to send should - not affect schedule execution. + Send a message about a finished schedule item, if notifications are enabled for the item's + category and final status. Never raises. + + Args: + client: BEC client. + settings: Notification settings of the schedule. + item: The finished item (COMPLETED, FAILED or ABORTED). + description: Human-readable description of the item (the "Command" column text). """ - if not _enabled_for_kind(settings, kind): + spec = ACTIONS.get(item.action) + if not settings.enabled or spec is None or not _enabled_for_category(settings, spec.category): return - if final_status not in settings.events: + if item.status not in settings.events: return - status_word = final_status.name - text = f"**BEC Scheduler Widget**\nStatus: {kind} {status_word}" - if scan_number is not None: - text = text + f"\nScan number: {scan_number}" - text = text + f"\n\nCommand: {command}" - if error: - text += f"\n\n{error.strip().splitlines()[-1]}" + text = f"**BEC Scheduler**\n{spec.label} {item.status.name.lower()}" + if item.scan_number is not None: + text += f"\nScan number: {item.scan_number}" + text += f"\n\n{description}" + if item.report: + text += f"\n\n{item.report}" + _send_safe(client, settings, text) - try: - send_notification(client, settings, text) - except Exception: # pylint: disable=broad-except - logger.exception(f"Failed to send schedule notification via {settings.target.value}") +def notify_schedule_state( + client, settings: NotificationSettings, schedule_name: str, state: ScheduleState +) -> None: + """Send a message when the schedule starts or stops (if notifications are on). Never raises.""" + if not settings.enabled: + return + text = f'**BEC Scheduler**\nSchedule "{schedule_name}" {_STATE_TEXT.get(state, state)}' + _send_safe(client, settings, text) -def notify_schedule_state(client, settings, schedule_name, state): - """ - Sends a message if notifications are enabled and a schedule is started or stopped. - Never raises - a notification failing to send should not affect schedule execution. - """ - text = f'**BEC Scheduler Widget**\nSchedule "{schedule_name}" ' - if state == "started": - text = text + "has started" - elif state == "aborted": - text = text + "was aborted" - elif state == "widget_closed": - text = text + "has finished prematurely because widget was closed" - elif state == "finished": - text = text + "has finished" - else: - logger.warning(f"Unknown schedule state {state}") + +def _send_safe(client, settings: NotificationSettings, text: str) -> None: try: send_notification(client, settings, text) except Exception: # pylint: disable=broad-except diff --git a/debye_bec/bec_widgets/widgets/scheduler/qt_widgets.py b/debye_bec/bec_widgets/widgets/scheduler/qt_widgets.py deleted file mode 100644 index 884da3a..0000000 --- a/debye_bec/bec_widgets/widgets/scheduler/qt_widgets.py +++ /dev/null @@ -1,159 +0,0 @@ -from bec_widgets.utils.colors import get_accent_colors -from qtpy.QtCore import QModelIndex, QSize, Qt, Signal - -# pylint: disable=E0611 -from qtpy.QtGui import QKeySequence -from qtpy.QtWidgets import QHeaderView, QPushButton, QStyle, QStyledItemDelegate, QTableView - -_ROW_MARGIN = 2 # total vertical padding (top+bottom) added around the text block - - -class _MultilineTextDelegate(QStyledItemDelegate): - """ - Paints and sizes a cell whose DisplayRole text may contain literal - '\\n' line breaks (schedule item labels are built with explicit line - breaks, e.g. the digital-twin config summary). The base - QStyledItemDelegate only reliably shows a single line, so without this - a multi-line label got clipped once rows stopped auto-sizing the way - QListWidgetItem used to. `sizeHint()` reports the full multi-line - height so the view's ResizeToContents row mode can actually fit it. - - Selection is painted here with `option.palette.highlight()` - the - same approach BecLogTableView's `_LogCellDelegate` uses - so the row - gets the theme's native (blue) highlight color instead of whatever a - stylesheet might otherwise hardcode. - """ - - def paint(self, painter, option, index): - painter.save() - if option.state & QStyle.StateFlag.State_Selected: - painter.fillRect(option.rect, option.palette.highlight()) - painter.setPen(option.palette.highlightedText().color()) - else: - painter.setPen(option.palette.text().color()) - painter.setFont(option.font) - - text = index.data(Qt.ItemDataRole.DisplayRole) or "" - rect = option.rect.adjusted(4, _ROW_MARGIN // 2, -4, -(_ROW_MARGIN // 2)) - # painter.drawText(rect, int(Qt.TextFlag.TextWordWrap), text) - painter.drawText( - rect, - int( - Qt.TextFlag.TextWordWrap - | Qt.AlignmentFlag.AlignLeft - | Qt.AlignmentFlag.AlignVCenter - ), - text, - ) - painter.restore() - - def sizeHint(self, option, index): - text = index.data(Qt.ItemDataRole.DisplayRole) or "" - lines = text.count("\n") + 1 - height = option.fontMetrics.lineSpacing() * lines + _ROW_MARGIN - width = super().sizeHint(option, index).width() - return QSize(width, height) - - -class MyTableView(QTableView): - deletePressed = Signal() - emptySpaceClicked = Signal() - copyPressed = Signal() - pastePressed = Signal() - - # Column layout is fixed: 0 = status icon, 1 = text (stretches), 2 = pause-on-failure icon. - TEXT_COLUMN = 1 - - def __init__(self, parent=None, status_col_width: int = 60, pause_col_width: int = 130): - super().__init__(parent) - - self._status_col_width = status_col_width - self._pause_col_width = pause_col_width - - self.setSelectionBehavior(QTableView.SelectionBehavior.SelectRows) - self.setSelectionMode(QTableView.SelectionMode.SingleSelection) - self.setEditTriggers(QTableView.EditTrigger.NoEditTriggers) - self.setShowGrid(False) - self.setWordWrap(True) - - self.setItemDelegateForColumn(self.TEXT_COLUMN, _MultilineTextDelegate(self)) - - self.verticalHeader().hide() - # ResizeToContents (not Fixed) so a row grows to fit however many - # lines its text cell reports via the delegate's sizeHint above - - # this is what lets multi-line item text show in full again. - self.verticalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents) - - # No `:selected` rule here on purpose - leaving it out lets Qt fall - # back to the palette highlight color (blue) for selected rows, - # matching BecLogTableView instead of a hardcoded dark gray. - # self.setStyleSheet(""" - # QTableView::item { - # border-bottom: 1px solid #444444; - # padding: 4px; - # } - # """) - - def configure_columns(self): - header = self.horizontalHeader() - - header.setSectionResizeMode(0, QHeaderView.ResizeMode.Fixed) - header.setSectionResizeMode(1, QHeaderView.ResizeMode.Stretch) - header.setSectionResizeMode(2, QHeaderView.ResizeMode.Fixed) - - header.resizeSection(0, self._status_col_width) - header.resizeSection(2, self._pause_col_width) - - header.setStretchLastSection(False) - header.setDefaultAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter) - - def keyPressEvent(self, event): - if event.key() == Qt.Key_Delete and self.currentIndex().isValid(): - self.deletePressed.emit() - event.accept() - return - - if event.matches(QKeySequence.StandardKey.Copy) and self.currentIndex().isValid(): - self.copyPressed.emit() - event.accept() - return - - if event.matches(QKeySequence.StandardKey.Paste): - self.pastePressed.emit() - event.accept() - return - - super().keyPressEvent(event) - - def mousePressEvent(self, event): - if ( - event.button() == Qt.LeftButton - and not self.indexAt(event.position().toPoint()).isValid() - ): - self.clearSelection() - self.setCurrentIndex(QModelIndex()) - self.emptySpaceClicked.emit() - return - - super().mousePressEvent(event) - - -class MyButton(QPushButton): - def __init__(self, text="", color="default", parent=None): - self.color = color - super().__init__(text, parent) - self.apply_theme() - - def apply_theme(self): - if self.isEnabled(): - colors = get_accent_colors() - color = getattr(colors, self.color).name() - self.setStyleSheet(f"QPushButton {{ background-color: {color}; color: white; }}") - else: - self.setStyleSheet( - "QPushButton {{background-color: rgb(120, 120, 120); color: white;}}" - ) - - def setEnabled(self, enable: bool = True): - super().setEnabled(enable) - self.apply_theme() diff --git a/debye_bec/bec_widgets/widgets/scheduler/schedule_item.py b/debye_bec/bec_widgets/widgets/scheduler/schedule_item.py deleted file mode 100644 index f58fb07..0000000 --- a/debye_bec/bec_widgets/widgets/scheduler/schedule_item.py +++ /dev/null @@ -1,70 +0,0 @@ -""" -Plain (non-BECMessage) data model for one schedule entry and the schedule -as a whole. - -These are ordinary pydantic models used only client-side, for validation -and convenience. They are never sent over Redis as their own type - only -ever as the `.value` payload of a `bec_lib.messages.VariableMessage` (see -`endpoints.schedule` for why). -""" - -from __future__ import annotations - -from enum import Enum -from typing import Literal - -from pydantic import BaseModel, Field - -from .enums import ScheduleItemStatus -from .guard import GuardSettings -from .notifications import NotificationSettings - -# Which tab of the Add/Edit dialog built `command`, and therefore how to -# re-open that dialog pre-filled with the same values for editing. "custom" -# covers hand-typed text (including RPC calls to other widgets), which has -# no structured `form_state` to restore beyond the raw text itself. -ScheduleItemKind = Literal["scan", "move", "custom"] - - -class ScheduleItem(BaseModel): - """A single command in the schedule, plus its execution bookkeeping.""" - - item_id: str - command: str - status: ScheduleItemStatus = ScheduleItemStatus.PENDING - - # How `command` was produced, and (for "scan"/"move") the structured - # inputs behind it, so the Add/Edit dialog can be reopened pre-filled - # instead of asking the user to re-type everything. `command` itself - # remains the single source of truth actually evaluated at execution - # time - `kind`/`form_state` only drive the UI. - kind: ScheduleItemKind = "custom" - form_state: dict | None = None - pause_on_failure: bool - - # Bookkeeping used to reconnect to a submission that is still (or was) - # in flight on the BEC scan/device server, after this widget has been - # closed and reopened. - request_id: str | None = None - scan_id: str | None = None - scan_number: int | None = None - - error: str | None = None - started_at: float | None = None - finished_at: float | None = None - - -class Schedule(BaseModel): - """The full, persisted state of one schedule-widget instance.""" - - schedule_name: str - items: list[ScheduleItem] = Field(default_factory=list) - is_running: bool = False - notes: str = "" - - # Settings for two independent, optional features (see notifications.py - # and guard.py) - persisted here alongside the schedule itself so they - # survive a widget restart too, but their *logic* lives entirely in - # those separate modules; this is just where their settings are stored. - notifications: NotificationSettings = Field(default_factory=NotificationSettings) - guard: GuardSettings = Field(default_factory=GuardSettings) diff --git a/debye_bec/bec_widgets/widgets/scheduler/schedule_logic.py b/debye_bec/bec_widgets/widgets/scheduler/schedule_logic.py index c229619..10e9adc 100644 --- a/debye_bec/bec_widgets/widgets/scheduler/schedule_logic.py +++ b/debye_bec/bec_widgets/widgets/scheduler/schedule_logic.py @@ -1,35 +1,99 @@ """ -Pure bookkeeping over a list of `ScheduleItem`s - no Qt, no locking, no I/O. -Kept separate from `schedule_widget.py` for the same reason `guard.py` and -`notifications.py` are: it's testable on its own, and the widget should -only be responsible for Qt/orchestration, not figuring out which item runs -next. +Pure bookkeeping over a list of `ScheduleItem`s - no Qt, no locking, no I/O - so that it can be +unit-tested on its own. Callers (the widget and the executor) hold their own lock while calling +these functions. -The one invariant everything here assumes and preserves: at any moment, a -schedule's items form a (possibly empty) prefix that is no longer PENDING -(RUNNING - at most one, the one currently executing - COMPLETED, FAILED or -ABORTED), followed by a suffix that is entirely PENDING. `ScheduleWidget` -is responsible for only ever mutating the PENDING suffix (see its module -docstring) and for calling these functions while holding its lock; nothing -here does its own locking. +Two concepts are handled here: + +**Protected prefix.** Items are executed from top to bottom. Everything before the first PENDING +item has already started or finished (RUNNING, COMPLETED, FAILED, ABORTED) and must not be +moved, edited or deleted; new items can never be inserted before it. Because of background items +(see below), several items of that prefix may be RUNNING at the same time. + +**Blocks.** An item with `wait_for_completion=False` ("background item") is started and the +schedule immediately continues with the following items, which form its *block* and are shown +indented. A block ends + +- with a "Wait until parent has completed" item (which belongs to the block, as its last child), +- implicitly right before the next background item (no nesting), or +- at the end of the schedule. + +The block structure is derived from the item order every time it is needed and never stored, so +moving, adding or deleting items automatically updates which block an item belongs to. """ from __future__ import annotations -from typing import Literal +from enum import Enum -from bec_lib.logger import bec_logger +from .actions import WAIT_FOR_PARENT +from .models import ItemStatus, ScheduleItem -from .schedule_item import ScheduleItem, ScheduleItemStatus -logger = bec_logger.logger +class BlockRole(str, Enum): + """Position of an item relative to the block structure (used for drawing guide lines).""" + + NONE = "none" # not part of a block + PARENT = "parent" # background item that opens a block + PARENT_EMPTY = "parent_empty" # background item without any children + CHILD = "child" # inside a block + LAST_CHILD = "last_child" # last item of a block + + +def opens_block(item: ScheduleItem) -> bool: + """True if the item is a background item, i.e. starts a block of following items.""" + return not item.wait_for_completion and item.action != WAIT_FOR_PARENT + + +def block_parents(items: list[ScheduleItem]) -> list[int | None]: + """ + For every item, the index of the background item whose block it belongs to. + + Returns: + A list with one entry per item: the index of its parent, or None if the item is not + inside a block. Background items themselves are never inside a block. + """ + parents: list[int | None] = [] + current: int | None = None + for idx, item in enumerate(items): + if opens_block(item): + current = idx + parents.append(None) + elif current is not None: + parents.append(current) + if item.action == WAIT_FOR_PARENT: + current = None + else: + parents.append(None) + return parents + + +def item_depths(items: list[ScheduleItem]) -> list[int]: + """Indentation level of every item: 1 inside a block, 0 otherwise.""" + return [0 if parent is None else 1 for parent in block_parents(items)] + + +def block_roles(items: list[ScheduleItem]) -> list[BlockRole]: + """The `BlockRole` of every item, e.g. for drawing the block guide lines in the table.""" + parents = block_parents(items) + roles: list[BlockRole] = [] + for idx, item in enumerate(items): + next_parent = parents[idx + 1] if idx + 1 < len(items) else None + if opens_block(item): + roles.append(BlockRole.PARENT if next_parent == idx else BlockRole.PARENT_EMPTY) + elif parents[idx] is not None: + last = next_parent != parents[idx] + roles.append(BlockRole.LAST_CHILD if last else BlockRole.CHILD) + else: + roles.append(BlockRole.NONE) + return roles def protected_prefix_length(items: list[ScheduleItem]) -> int: - """How many items, from the start, are no longer PENDING.""" + """Number of leading items that are no longer PENDING (see module docstring).""" count = 0 for item in items: - if item.status != ScheduleItemStatus.PENDING: + if item.status != ItemStatus.PENDING: count += 1 else: break @@ -37,39 +101,53 @@ def protected_prefix_length(items: list[ScheduleItem]) -> int: def index_of(items: list[ScheduleItem], item_id: str | None) -> int | None: + """Index of the item with `item_id`, or None if there is no such item.""" if item_id is None: return None - for i, item in enumerate(items): + for idx, item in enumerate(items): if item.item_id == item_id: - return i + return idx return None def pick_next_runnable( - items: list[ScheduleItem], repeat_aborted_item, first_item: int = 0 -) -> ScheduleItem | Literal["stop"] | None: + items: list[ScheduleItem], + retry_unfinished: bool = False, + first_index: int = 0, + active_ids: frozenset[str] | set[str] = frozenset(), + attempted_ids: frozenset[str] | set[str] = frozenset(), +) -> ScheduleItem | None: """ - What the execution loop should do next: always re-derived from scratch - (never a remembered index/object) so edits made to the PENDING suffix - between calls are picked up correctly. + Decide which item the execution loop should handle next. - Returns the next item to run/attach to, the string `"stop"` if an - earlier item failed/was aborted (execution stays parked there until the - operator intervenes), or `None` if every item is COMPLETED. + This is always re-derived from the current list (never a remembered index), so edits to the + PENDING part of the schedule between two calls are picked up. + + Args: + items: The schedule items. + retry_unfinished: If True, FAILED and ABORTED items are run again (once per run, see + `attempted_ids`); otherwise they are skipped. + first_index: Items before this index are ignored ("run from selection"). + active_ids: Ids of items that are currently being executed by this executor (e.g. a + running background item); they are skipped. + attempted_ids: Ids of items already started during the current run. A FAILED/ABORTED + item is only retried if it has not been attempted in this run yet, so a failing + item cannot be retried in an endless loop. + + Returns: + The next PENDING item, a RUNNING item that is not owned by this executor (left over + from a previous widget session; the caller should re-attach to it), a FAILED/ABORTED + item if `retry_unfinished` is set, or None if nothing is left to do. """ - logger.info( - f"pick next runnable, repeat abort item is {repeat_aborted_item} and first item is {first_item}" - ) for idx, item in enumerate(items): - logger.info(f"item: {item}") - if idx < first_item: + if idx < first_index or item.item_id in active_ids: continue - if item.status == ScheduleItemStatus.COMPLETED: - continue - if item.status == ScheduleItemStatus.ABORTED: - if not repeat_aborted_item: - continue - if item.status == ScheduleItemStatus.FAILED: - return "stop" - return item # PENDING, RUNNING or ABORTED if repeat_aborted_item is set (reconciled as still active) + if item.status in (ItemStatus.PENDING, ItemStatus.RUNNING): + return item + if ( + retry_unfinished + and item.status in (ItemStatus.FAILED, ItemStatus.ABORTED) + and item.item_id not in attempted_ids + ): + return item return None diff --git a/debye_bec/bec_widgets/widgets/scheduler/schedule_table.py b/debye_bec/bec_widgets/widgets/scheduler/schedule_table.py new file mode 100644 index 0000000..d1929ed --- /dev/null +++ b/debye_bec/bec_widgets/widgets/scheduler/schedule_table.py @@ -0,0 +1,397 @@ +""" +Table model and view showing a schedule. + +Columns: line number | status | command | report | pause on failure | pause on state | +wait for completion. All columns except "Command" and "Report" have a fixed width; those two +share the remaining width 50/50. + +Items inside the block of a background item (`wait_for_completion=False`) are indented in the +"Command" column, and a vertical guide line connects them with their parent, so it is visible +which items run while the parent is still busy (see `schedule_logic` for the block rules). +""" + +from __future__ import annotations + +import time + +from bec_qthemes import material_icon +from bec_widgets.utils.colors import get_accent_colors + +# pylint: disable=no-name-in-module # qtpy re-exports are not visible to pylint +from qtpy.QtCore import QAbstractTableModel, QModelIndex, QRect, QSize, Qt, Signal +from qtpy.QtGui import QKeySequence, QPen +from qtpy.QtWidgets import QHeaderView, QStyle, QStyledItemDelegate, QTableView + +from .actions import ACTIONS, DELAY, describe_item, format_duration +from .models import ItemStatus, ScheduleItem +from .schedule_logic import BlockRole, block_roles, item_depths + +# Qt virtual overrides (rowCount, data, paint, sizeHint, ...) keep Qt's names and docs. +# pylint: disable=invalid-name,missing-function-docstring,too-many-return-statements + + +ICON_SIZE = 20 +INDENT_WIDTH = 24 # px per indentation level in the "Command" column +_ROW_MARGIN = 6 # total vertical padding (top + bottom) of a text cell + +_STATUS_ICONS = { + ItemStatus.PENDING: ("hourglass", "#919090"), + ItemStatus.RUNNING: ("cycle", "#2980b9"), + ItemStatus.COMPLETED: ("check", "#27ae60"), + ItemStatus.FAILED: ("warning", "#e74c3c"), + ItemStatus.ABORTED: ("cancel", "#e6d922"), +} + +#: Custom data roles +DepthRole = Qt.ItemDataRole.UserRole + 1 +BlockRoleRole = Qt.ItemDataRole.UserRole + 2 + + +class ScheduleTableModel(QAbstractTableModel): + """ + Read-only table model over a snapshot of `Schedule.items`. + + The widget calls `set_items()` with a fresh copy whenever the schedule changes (a full model + reset - schedules are short, and this keeps the model free of locking). Display text comes + from the action registry (`actions.describe_item`), block indentation from + `schedule_logic.item_depths`. + """ + + COL_LINE = 0 + COL_STATUS = 1 + COL_COMMAND = 2 + COL_REPORT = 3 + COL_PAUSE_ON_FAILURE = 4 + COL_PAUSE_ON_STATE = 5 + COL_WAIT = 6 + HEADERS = ( + "Line", + "Status", + "Command", + "Report", + "Pause on failure", + "Pause on state", + "Wait for completion", + ) + ICON_COLUMNS = (COL_STATUS, COL_PAUSE_ON_FAILURE, COL_PAUSE_ON_STATE, COL_WAIT) + + def __init__(self, parent=None, beamline: str | None = None): + super().__init__(parent) + self._items: list[ScheduleItem] = [] + self._depths: list[int] = [] + self._roles: list[BlockRole] = [] + self._beamline = beamline + # The status icon of the selected row is drawn white (on the selection colour). Kept + # here rather than read from the view so it survives a model reset. + self._selected_item_id: str | None = None + + # ---- Qt model API ---- # + def rowCount(self, parent: QModelIndex = QModelIndex()) -> int: # noqa: N802 + return 0 if parent.isValid() else len(self._items) + + def columnCount(self, parent: QModelIndex = QModelIndex()) -> int: # noqa: N802 + return 0 if parent.isValid() else len(self.HEADERS) + + def headerData(self, section, orientation, role=Qt.ItemDataRole.DisplayRole): # noqa: N802 + if orientation != Qt.Orientation.Horizontal: + return None + if role == Qt.ItemDataRole.DisplayRole: + return self.HEADERS[section] + if role == Qt.ItemDataRole.TextAlignmentRole: + if section in (self.COL_COMMAND, self.COL_REPORT): + return int(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter) + return int(Qt.AlignmentFlag.AlignCenter) + return None + + def data(self, index: QModelIndex, role=Qt.ItemDataRole.DisplayRole): + if not index.isValid(): + return None + row, col = index.row(), index.column() + item = self._items[row] + + if role == Qt.ItemDataRole.DisplayRole: + if col == self.COL_LINE: + return str(row + 1) + if col == self.COL_COMMAND: + return describe_item(item, self._beamline) + if col == self.COL_REPORT: + return self._report_text(item) + return None + if role == Qt.ItemDataRole.DecorationRole: + return self._icon(item, col) + if role == Qt.ItemDataRole.ToolTipRole: + return self._tooltip(item, col) + if role == Qt.ItemDataRole.TextAlignmentRole and col == self.COL_LINE: + return int(Qt.AlignmentFlag.AlignCenter) + if role == DepthRole: + return self._depths[row] + if role == BlockRoleRole: + return self._roles[row] + return None + + # ---- content ---- # + def set_items(self, items: list[ScheduleItem], selected_item_id: str | None = None) -> None: + """ + Replace the displayed items (full model reset). + + Args: + items: Snapshot of the schedule items. + selected_item_id: Item whose status icon is drawn for the selected row. + """ + self.beginResetModel() + self._selected_item_id = selected_item_id + self._items = list(items) + self._depths = item_depths(self._items) + self._roles = block_roles(self._items) + self.endResetModel() + + def set_selected_item_id(self, item_id: str | None) -> None: + """Remember the selected item and repaint the status icons (no full reset).""" + self._selected_item_id = item_id + self._emit_column_changed(self.COL_STATUS, [Qt.ItemDataRole.DecorationRole]) + + def refresh_live_cells(self) -> None: + """Repaint the report of running delays (remaining time). Called once per second.""" + if any(item.status == ItemStatus.RUNNING and item.action == DELAY for item in self._items): + self._emit_column_changed(self.COL_REPORT, [Qt.ItemDataRole.DisplayRole]) + + def item_id(self, row: int) -> str | None: + """Id of the item in `row`, or None.""" + return self._items[row].item_id if 0 <= row < len(self._items) else None + + def row_of(self, item_id: str | None) -> int | None: + """Row of the item with `item_id`, or None.""" + for row, item in enumerate(self._items): + if item.item_id == item_id: + return row + return None + + def _emit_column_changed(self, column: int, roles: list) -> None: + if self._items: + self.dataChanged.emit( + self.index(0, column), self.index(len(self._items) - 1, column), roles + ) + + # ---- cell content helpers ---- # + @staticmethod + def _report_text(item: ScheduleItem) -> str: + if item.status == ItemStatus.RUNNING and item.action == DELAY and item.started_at: + remaining = item.params.get("seconds", 0) - (time.time() - item.started_at) + return f"{format_duration(remaining)} remaining" + if item.status == ItemStatus.RUNNING and not item.report: + return "Running..." + return item.report or "" + + def _icon(self, item: ScheduleItem, col: int): + if col == self.COL_STATUS: + name, color = _STATUS_ICONS[item.status] + if item.item_id == self._selected_item_id: + color = "#FFFFFF" + return _pixmap(name, color) + if col == self.COL_PAUSE_ON_FAILURE and item.pause_on_failure: + return _pixmap("pause_circle", "#e6d922") + if col == self.COL_PAUSE_ON_STATE and item.pause_on_state: + return _pixmap("shield", "#2980b9") + if col == self.COL_WAIT and ACTIONS.get(item.action) is not None: + if ACTIONS[item.action].category == "control": + return None + if item.wait_for_completion: + return _pixmap("hourglass_bottom", "#919090") + return _pixmap("call_split", get_accent_colors().highlight.name()) + return None + + @staticmethod + def _tooltip(item: ScheduleItem, col: int) -> str | None: + if col == ScheduleTableModel.COL_STATUS: + return item.status.name.capitalize() + if col == ScheduleTableModel.COL_REPORT and item.error: + return item.error + if col == ScheduleTableModel.COL_PAUSE_ON_FAILURE: + return "Pauses the schedule if this item fails" if item.pause_on_failure else None + if col == ScheduleTableModel.COL_PAUSE_ON_STATE: + if item.pause_on_state: + return ( + "Waits for all beamline states to be valid before starting;\n" + "running scans are interrupted and repeated if a state becomes invalid" + ) + return None + if col == ScheduleTableModel.COL_WAIT: + if not item.wait_for_completion: + return "Runs in the background: the following (indented) items start right away" + return "The schedule waits for this item to complete" + return None + + +def _pixmap(name: str, color: str): + return material_icon(name, size=(ICON_SIZE, ICON_SIZE), color=color, convert_to_pixmap=True) + + +def _paint_background(painter, option) -> None: + if option.state & QStyle.StateFlag.State_Selected: + painter.fillRect(option.rect, option.palette.highlight()) + painter.setPen(option.palette.highlightedText().color()) + else: + painter.setPen(option.palette.text().color()) + + +class _TextDelegate(QStyledItemDelegate): + """ + Paints multi-line text (labels contain explicit line breaks) and reports the full multi-line + height, so the view's ResizeToContents row mode can fit it. If `indent` is True, the text is + indented by the item's block depth and the block guide line is drawn. + + Selection is painted with the palette highlight colour, like `BecLogTableView`. + """ + + def __init__(self, parent=None, indent: bool = False): + super().__init__(parent) + self._indent = indent + + def paint(self, painter, option, index): + painter.save() + _paint_background(painter, option) + painter.setFont(option.font) + text_pen = painter.pen() + + rect = option.rect.adjusted(6, _ROW_MARGIN // 2, -6, -(_ROW_MARGIN // 2)) + if self._indent: + depth = index.data(DepthRole) or 0 + self._paint_guide(painter, option.rect, index.data(BlockRoleRole)) + rect.setLeft(rect.left() + depth * INDENT_WIDTH) + painter.setPen(text_pen) + text = index.data(Qt.ItemDataRole.DisplayRole) or "" + flags = ( + Qt.TextFlag.TextWordWrap | Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter + ) + painter.drawText(rect, int(flags), text) + painter.restore() + + @staticmethod + def _paint_guide(painter, rect: QRect, role: BlockRole | None) -> None: + if role in (None, BlockRole.NONE, BlockRole.PARENT_EMPTY): + return + pen = QPen(get_accent_colors().highlight) + pen.setWidth(2) + painter.setPen(pen) + x = rect.left() + 10 + mid = rect.center().y() + if role == BlockRole.PARENT: + painter.drawLine(x, mid + 4, x, rect.bottom()) + else: + bottom = mid if role == BlockRole.LAST_CHILD else rect.bottom() + painter.drawLine(x, rect.top(), x, bottom) + painter.drawLine(x, mid, x + INDENT_WIDTH - 8, mid) + + def sizeHint(self, option, index): # noqa: N802 + text = index.data(Qt.ItemDataRole.DisplayRole) or "" + lines = text.count("\n") + 1 + height = option.fontMetrics.lineSpacing() * lines + _ROW_MARGIN + return QSize(super().sizeHint(option, index).width(), max(height, ICON_SIZE + _ROW_MARGIN)) + + +class _CenteredIconDelegate(QStyledItemDelegate): + """Paints the DecorationRole pixmap centred in the cell.""" + + def paint(self, painter, option, index): + painter.save() + _paint_background(painter, option) + pixmap = index.data(Qt.ItemDataRole.DecorationRole) + if pixmap is not None: + x = option.rect.center().x() - ICON_SIZE // 2 + y = option.rect.center().y() - ICON_SIZE // 2 + painter.drawPixmap(x, y, pixmap) + painter.restore() + + +class _CenteredTextDelegate(QStyledItemDelegate): + """Paints short single-line text centred in the cell (line numbers).""" + + def paint(self, painter, option, index): + painter.save() + _paint_background(painter, option) + painter.drawText( + option.rect, int(Qt.AlignmentFlag.AlignCenter), index.data(Qt.ItemDataRole.DisplayRole) + ) + painter.restore() + + +class ScheduleTableView(QTableView): + """ + Table view for a `ScheduleTableModel`: single row selection, fixed-width icon columns, + "Command" and "Report" sharing the remaining width, plus keyboard shortcuts. + + Signals: + deletePressed: The Delete key was pressed on a selected row. + copyPressed / pastePressed: The standard copy/paste shortcut was pressed. + emptySpaceClicked: The user clicked below the last row (clears the selection). + """ + + deletePressed = Signal() + emptySpaceClicked = Signal() + copyPressed = Signal() + pastePressed = Signal() + + FIXED_WIDTHS = { + ScheduleTableModel.COL_LINE: 50, + ScheduleTableModel.COL_STATUS: 60, + ScheduleTableModel.COL_PAUSE_ON_FAILURE: 120, + ScheduleTableModel.COL_PAUSE_ON_STATE: 110, + ScheduleTableModel.COL_WAIT: 145, + } + + def __init__(self, parent=None): + super().__init__(parent) + self.setSelectionBehavior(QTableView.SelectionBehavior.SelectRows) + self.setSelectionMode(QTableView.SelectionMode.SingleSelection) + self.setEditTriggers(QTableView.EditTrigger.NoEditTriggers) + self.setShowGrid(False) + self.setWordWrap(True) + self.verticalHeader().hide() + # ResizeToContents so that a row grows with the number of text lines of its cells. + self.verticalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents) + + self.setItemDelegateForColumn(ScheduleTableModel.COL_LINE, _CenteredTextDelegate(self)) + self.setItemDelegateForColumn( + ScheduleTableModel.COL_COMMAND, _TextDelegate(self, indent=True) + ) + self.setItemDelegateForColumn(ScheduleTableModel.COL_REPORT, _TextDelegate(self)) + icon_delegate = _CenteredIconDelegate(self) + for col in ScheduleTableModel.ICON_COLUMNS: + self.setItemDelegateForColumn(col, icon_delegate) + + def configure_columns(self) -> None: + """Apply the column sizing; call after `setModel()`.""" + header = self.horizontalHeader() + header.setStretchLastSection(False) + for col, width in self.FIXED_WIDTHS.items(): + header.setSectionResizeMode(col, QHeaderView.ResizeMode.Fixed) + header.resizeSection(col, width) + # Both stretch columns get the same stretch factor -> the leftover width is shared 50/50. + header.setSectionResizeMode(ScheduleTableModel.COL_COMMAND, QHeaderView.ResizeMode.Stretch) + header.setSectionResizeMode(ScheduleTableModel.COL_REPORT, QHeaderView.ResizeMode.Stretch) + + def keyPressEvent(self, event): # noqa: N802 + if event.key() == Qt.Key.Key_Delete and self.currentIndex().isValid(): + self.deletePressed.emit() + event.accept() + return + if event.matches(QKeySequence.StandardKey.Copy) and self.currentIndex().isValid(): + self.copyPressed.emit() + event.accept() + return + if event.matches(QKeySequence.StandardKey.Paste): + self.pastePressed.emit() + event.accept() + return + super().keyPressEvent(event) + + def mousePressEvent(self, event): # noqa: N802 + if ( + event.button() == Qt.MouseButton.LeftButton + and not self.indexAt(event.position().toPoint()).isValid() + ): + self.clearSelection() + self.setCurrentIndex(QModelIndex()) + self.emptySpaceClicked.emit() + return + super().mousePressEvent(event) diff --git a/debye_bec/bec_widgets/widgets/scheduler/scheduler.py b/debye_bec/bec_widgets/widgets/scheduler/scheduler.py index 61a786b..37104c5 100644 --- a/debye_bec/bec_widgets/widgets/scheduler/scheduler.py +++ b/debye_bec/bec_widgets/widgets/scheduler/scheduler.py @@ -1,303 +1,111 @@ """ -ScheduleWidget - persists an ordered schedule of BEC commands (scans, -device moves, ...) to Redis, executes them one by one, and survives being -closed and reopened - including while an item is still running -server-side. Items that have not started yet can be added, reordered or -deleted while the schedule is executing; items that have already started -or finished cannot. +Scheduler widget: build, persist and execute an ordered list of BEC actions (scans, moves, +beamline tools, delays, ...). + +Overview of the package: + +- `models.py`: persisted pydantic models (`Schedule`, `ScheduleItem`, notification settings). +- `actions.py`: function registry. Items store an action *name* plus JSON parameters; nothing is + ever `eval`-ed. +- `schedule_logic.py`: pure helpers (next item to run, protected prefix, block structure). +- `executor.py`: the execution loop, running on a worker thread. +- `state_monitor.py`: watches the BEC beamline states for "pause on state". +- `interlock_hiding.py`: hides the scan-interlock controls of the embedded BEC + `BeamlineStateManager`. +- `schedule_table.py`, `item_dialog.py`, `notification_dialog.py`, `widgets.py`: UI. +- `notifications.py`: SciLog/Signal notifications. + +Persistence: the whole schedule is one `VariableMessage` document in Redis (see `endpoints.py`), +keyed by the stable `schedule_name` of the widget config. It survives closing and reopening the +widget - also while an item is still running server-side; on reopen, the persisted state is +reconciled with BEC's scan queue. Items that have not started yet (PENDING) can be added, edited, +reordered or deleted at any time, also while the schedule runs; items that have started or +finished cannot. """ from __future__ import annotations import json -import re import sys import threading import time -import traceback -import uuid -from typing import Literal, Optional +from pathlib import Path -import numpy as np from bec_lib.endpoints import MessageEndpoints from bec_lib.logger import bec_logger from bec_lib.messages import VariableMessage -from bec_qthemes import material_icon from bec_widgets.utils.bec_connector import ConnectionConfig from bec_widgets.utils.bec_dispatcher import BECDispatcher from bec_widgets.utils.bec_widget import BECWidget from bec_widgets.utils.error_popups import SafeSlot -from pydantic import ValidationError +from bec_widgets.widgets.services.beamline_states.beamline_state_manager import BeamlineStateManager -# pylint: disable=E0611 -from qtpy.QtCore import QAbstractTableModel, QModelIndex, Qt, QTimer, Signal +# pylint: disable=no-name-in-module # qtpy re-exports are not visible to pylint +from qtpy.QtCore import QModelIndex, Qt, QTimer, Signal from qtpy.QtWidgets import ( QApplication, QDialog, QFileDialog, QGroupBox, QHBoxLayout, - QHeaderView, QLabel, QLineEdit, QMessageBox, QPlainTextEdit, + QSplitter, QVBoxLayout, QWidget, ) +from .actions import ACTIONS, ALWAYS_WAIT_ACTIONS, ExecutionContext, describe_item from .endpoints import schedule as schedule_endpoint -from .enums import ScheduleItemStatus -from .guard import SignalGuard -from .guard_dialog import GuardSettingsDialog -from .item_dialog import ScheduleItemDialog +from .executor import RunOutcome, ScheduleExecutor, is_queue_item_active +from .interlock_hiding import install_scan_interlock_hiding +from .item_dialog import XAS_BEAMLINES, ScheduleItemDialog +from .models import ItemStatus, NotificationTarget, Schedule, ScheduleItem, schedule_from_document from .notification_dialog import NotificationSettingsDialog -from .notifications import ( - NotificationSettings, - NotificationTarget, - notify_item_finished, - notify_schedule_state, -) -from .qt_widgets import MyButton, MyTableView -from .schedule_item import Schedule, ScheduleItem -from .schedule_logic import index_of, pick_next_runnable, protected_prefix_length +from .notifications import notify_item_finished, notify_schedule_state +from .schedule_logic import index_of, protected_prefix_length +from .schedule_table import ScheduleTableModel, ScheduleTableView +from .state_monitor import BeamlineStateMonitor +from .widgets import AccentButton logger = bec_logger.logger -_ACTIVE_QUEUE_STATES = ("PENDING", "RUNNING") - -ICON_SIZE = 20 -_ICON_MAP = { - ScheduleItemStatus.PENDING: ("hourglass", "#919090"), - ScheduleItemStatus.RUNNING: ("cycle", "#2980b9"), - ScheduleItemStatus.COMPLETED: ("check", "#27ae60"), - ScheduleItemStatus.FAILED: ("warning", "#e74c3c"), - ScheduleItemStatus.ABORTED: ("cancel", "#e6d922"), -} - - -def build_item_display_text(item: ScheduleItem, beamline: str | None) -> str: - """ - Build the human-readable label for a schedule item, pulled out of the - old `_refresh_ui` QListWidgetItem-building loop so the table model's - `data()` can call it too. Pure function of (item, beamline) - no Qt - widgets, no locking - so it's safe to call from the model on every - repaint. - """ - text = "" - if item.form_state is not None: - if item.command.startswith("scans.xas_simple_scan"): - text = ( - f"XAS simple scan from {item.form_state['kwargs']['start']} eV to " - + f"{item.form_state['kwargs']['stop']} eV, {item.form_state['kwargs']['scan_time']}" - + f" s per spectrum, measure {item.form_state['kwargs']['scan_duration']}s" - + f"\n Comment: {item.form_state['kwargs']['metadata']['comment']}, " - + f"Sample Name: {item.form_state['kwargs']['metadata']['sample_name']}" - ) - elif re.match(r"^dev\..*\.move", item.command): - if item.form_state["relative"]: - text = f"Move {item.form_state['device_name']} by {item.form_state['value']:+g} {item.form_state['units']}" - else: - text = f"Move {item.form_state['device_name']} to {item.form_state['value']} {item.form_state['units']}" - elif item.command.startswith("dev.ic"): - text = ( - f"Fill {item.form_state['chamber']} with {item.form_state['conc1']} % " - + f"{item.form_state['gas1']} and {item.form_state['conc2']} % " - + f"{item.form_state['gas2']} to {item.form_state['pressure']} bar" - ) - elif item.command.startswith("dev.reffoilchanger"): - text = f"Insert {item.form_state['foil']} reference foil" - elif item.command.startswith("digital_twin.move_with_config({"): - config = item.form_state["config"] - if config["mo1_mode"] == "Monochromatic": - text = f"Align Beamline for {config['energy']:.0f} eV" - else: - text = "Align Beamline for pinkbeam" - text += ( - f"\n FE: {config['h_acc']*1e3:.3f} mrad x {config['v_acc']*1e3:.3f} mrad" - + f"\n CM: {config['cm_stripe']} at {config['cm_pitch']*1e3:.3f} mrad" - ) - if config["mo1_mode"] == "Monochromatic": - text += f"\n MO1: {config['mo1_xtal']}" - text += f"\n FM: {config['fm_stripe']} at {config['fm_rotx']*1e3:.3f} mrad" - if beamline == "x01da": - text += f"\n ES1: {config['ot_es1_trz']:.0f} mm" - text += f"\n SMPL_TRZ: {config['es1man_trz']:.3f} mm" - if beamline == "x01da": - text += f"\n SMPL-XRD: {config['smpl_to_xrd']:.3f} mm" - elif item.command.startswith("auto_gain.start("): - config = item.form_state["config"] - text += f"Auto-configure gains for {config['element']}, {config['edge']}-edge" - text += f"\nfor devices {config['all_amplifiers']}" - if config["link_ic0_ic1"]: - text += "\nLink IC0 and IC1 gains" - if config["compensate_ring_current"]: - text += "\nCalculate for 400 mA ring current" - if config["prohibited_gains"] != {}: - prohibited_gains = ( - "{" - + ", ".join( - f"{amp}: [" + ", ".join(f"{g:.0e}" for g in gains) + "]" - for amp, gains in config["prohibited_gains"].items() - ) - + "}" - ) - text += "\nProhibited gains: " + prohibited_gains - elif item.command.startswith("dev.univ_temp_cont.load_config("): - config = item.form_state["config"] - match config["power_mode"]: - case "Standby": - text += "Switch temperature controller to standby" - case "Configuration": - text += "Switch temperature controller to configuration mode" - case _: - text += ( - f"Set temperature controller target setpoint to {config['target_setpoint']}" - + f" °C\nwith ramp rate of {config['ramp_rate']} {config['ramp_rate_unit']}" - ) - elif item.command.startswith("dev.abs."): - action = item.form_state["action"] - text += f"{action} Absorber" - if text == "": - text = f"{item.command}" - if item.error: - text += f" ({item.error.strip().splitlines()[-1]})" - return text - - -class ScheduleTableModel(QAbstractTableModel): - """ - Read-mostly table model over `Schedule.items`, rebuilt in one shot via - `set_items()` whenever the Scheduler widget refreshes - the same - "clear and repopulate" pattern the old QListWidget code used, just - routed through Qt's model/view API instead of manual - QListWidgetItem management. Mirrors the shape of `BecLogTableView`'s - `BecLogsTableModel`: a flat Python list backing store, `data()` - dispatching on `role`/`column`, icons returned as pixmaps via - `DecorationRole`. - """ - - COL_STATUS = 0 - COL_TEXT = 1 - COL_PAUSE = 2 - _HEADERS = ("Status", "Item", "Pause on failure") - - def __init__(self, parent=None, beamline: str | None = None): - super().__init__(parent) - self._items: list[ScheduleItem] = [] - self._beamline = beamline - # Drives the "selected row -> white status icon" recolor; kept - # separate from the view's own selection model so the icon color - # survives a `set_items()` reset (which clears Qt's selection). - self._selected_item_id: str | None = None - - def rowCount(self, parent: QModelIndex = QModelIndex()) -> int: - return 0 if parent.isValid() else len(self._items) - - def columnCount(self, parent: QModelIndex = QModelIndex()) -> int: - return 0 if parent.isValid() else len(self._HEADERS) - - def headerData(self, section, orientation, role=Qt.ItemDataRole.DisplayRole): - if role == Qt.ItemDataRole.DisplayRole and orientation == Qt.Orientation.Horizontal: - return self._HEADERS[section] - return None - - def set_items(self, items: list[ScheduleItem]): - self.beginResetModel() - self._items = list(items) - self.endResetModel() - - def set_selected_item_id(self, item_id: str | None): - """ - Repaint just the status-icon column for the recolor - avoids a - full model reset, which would otherwise drop the view's own - selection/scroll state on every click. - """ - self._selected_item_id = item_id - if self._items: - top_left = self.index(0, self.COL_STATUS) - bottom_right = self.index(len(self._items) - 1, self.COL_STATUS) - self.dataChanged.emit(top_left, bottom_right, [Qt.ItemDataRole.DecorationRole]) - - def item_id(self, row: int) -> str | None: - if 0 <= row < len(self._items): - return self._items[row].item_id - return None - - def row_of(self, item_id: str | None) -> int | None: - if item_id is None: - return None - for row, item in enumerate(self._items): - if item.item_id == item_id: - return row - return None - - def item_at(self, row: int) -> ScheduleItem | None: - return self._items[row] if 0 <= row < len(self._items) else None - - def data(self, index: QModelIndex, role=Qt.ItemDataRole.DisplayRole): - if not index.isValid(): - return None - item = self._items[index.row()] - col = index.column() - - if role == Qt.ItemDataRole.DecorationRole: - if col == self.COL_STATUS: - icon_name, color = _ICON_MAP[item.status] - if item.item_id == self._selected_item_id: - color = "#FFFFFF" - return material_icon( - icon_name, size=(ICON_SIZE, ICON_SIZE), color=color, convert_to_pixmap=True - ) - if col == self.COL_PAUSE and item.pause_on_failure: - return material_icon( - "pause_circle", - size=(ICON_SIZE, ICON_SIZE), - color="#e6d922", - convert_to_pixmap=True, - ) - return None - - if role == Qt.ItemDataRole.DisplayRole and col == self.COL_TEXT: - return build_item_display_text(item, self._beamline) - - if role == Qt.ItemDataRole.ToolTipRole: - if col == self.COL_PAUSE: - return "Pauses the schedule if this item fails" if item.pause_on_failure else None - if item.error: - return item.error - - return None +# UI widgets are created in the _build_* helpers called from __init__. +# pylint: disable=attribute-defined-outside-init class ScheduleWidgetConfig(ConnectionConfig): """ - Per-instance identity/settings for `ScheduleWidget`, following the same - pattern `bec_widgets` itself uses (e.g. `ScanControlConfig`): a - `ConnectionConfig` subclass constructed in `__init__` when none is - passed, carrying whatever small settings distinguish one instance from - another. + Configuration of a `Scheduler` instance. - `schedule_name` lives here rather than as a bare constructor kwarg for - that consistency - but note this is *not* where the schedule's actual - contents (items, guard/notification settings) live: `self.config` is - used for RPC identity and optional explicit GUI-layout save/restore, - not auto-persisted across an ordinary widget close/reopen the way this - plugin's Redis-backed `Schedule` document is. Conflating the two would - quietly reintroduce the "closed widget loses its schedule" problem this - plugin exists to solve. + Attributes: + schedule_name: Stable name of the schedule. It selects the Redis document the schedule is + persisted in, so a widget opened again with the same name finds its schedule again + (unlike `gui_id`, which changes on every construction). The schedule *contents* are + not part of this config; they live in Redis (see `endpoints.schedule`). """ schedule_name: str = "default_schedule" class Scheduler(BECWidget, QWidget): - """Schedule, persist and execute a sequence of BEC scan/device commands.""" + """ + Schedule, persist and execute a sequence of BEC actions. + + The schedule is shown as a table (line, status, command, report, pause on failure, pause on + state, wait for completion). Items that run in the background (`wait_for_completion=False`) + are followed by an indented block of items that run while the background item is busy. The + BEC beamline states are shown next to the schedule; items with "pause on state" only run + while all states are valid. + """ PLUGIN = True ICON_NAME = "assignment_add" USER_ACCESS = [ "run_schedule", + "pause_schedule", "abort_schedule", "reset_schedule", "get_status", @@ -309,8 +117,7 @@ class Scheduler(BECWidget, QWidget): "move_item_down", ] - # emitted (thread-safe, may come from the background execution task) - # whenever the schedule state changes and the UI should redraw + # Thread-safe "schedule changed, redraw" signal; may be emitted from the executor thread. schedule_changed = Signal() def __init__( @@ -328,110 +135,103 @@ class Scheduler(BECWidget, QWidget): schedule_name=schedule_name or "default_schedule", ) elif schedule_name is not None: - # Allow overriding even when a config object/dict was supplied, - # mirroring how ScanControl treats its own optional constructor - # args as overrides onto whatever config ends up in self.config. if isinstance(config, dict): config = dict(config, schedule_name=schedule_name) else: config.schedule_name = schedule_name super().__init__(parent=parent, client=client, gui_id=gui_id, config=config, **kwargs) - # A stable name, NOT self.gui_id: gui_id is regenerated every time - # the widget is constructed, so it could never be used to find a - # previously persisted schedule again after the widget is reopened. - self.schedule_name = self.config.schedule_name - + self.schedule_name: str = self.config.schedule_name self.get_bec_shortcuts() # -> self.client, self.dev, self.scans, self.queue - self.beamline = self.get_beamline() self.connector = self.client.connector + self.beamline = self._detect_beamline() - if self.beamline in ["x01da", "x10da"]: - logger.info( - "Scheduler running at X01DA or X10DA, import and load digital twin and auto-gain" - ) + self._endpoint = schedule_endpoint(self.schedule_name) + self._lock = threading.Lock() # guards self.schedule + self._closing = threading.Event() + self._run_active = False # True while this instance's executor runs + self._selected_item_id: str | None = None + self._clipboard_item: dict | None = None + self._notice: str | None = None + + # Notes are saved on a debounce timer, not on every keystroke. + self._notes_save_timer = QTimer(self) + self._notes_save_timer.setSingleShot(True) + self._notes_save_timer.setInterval(500) + self._notes_save_timer.timeout.connect(self._persist_notes) + + self.schedule: Schedule = self._load_or_seed_schedule() + self._reconcile_with_live_state() + + self.state_monitor = BeamlineStateMonitor(self.client, self.bec_dispatcher, parent=self) + self._executor = ScheduleExecutor( + lock=self._lock, + get_items=lambda: self.schedule.items, + persist_locked=self._persist_locked, + emit_changed=self._emit_schedule_changed, + context=self._build_execution_context(), + state_monitor=self.state_monitor, + notify_item=self._notify_item_finished, + get_queue_storage=lambda: self.client.queue.queue_storage, + closing=self._closing, + ) + self.state_monitor.tripped.connect(self._executor.on_states_tripped) + self.state_monitor.statuses_changed.connect(self._update_state_label) + + self._build_ui() + self.schedule_changed.connect(self._refresh_ui) + self._refresh_ui() + + # Remaining time of running delays. + self._live_timer = QTimer(self) + self._live_timer.setInterval(1000) + self._live_timer.timeout.connect(self._table_model.refresh_live_cells) + self._live_timer.start() + + # Stay in sync with other users of the same schedule (another widget instance, scripts). + self.bec_dispatcher.connect_slot(self._on_remote_update, self._endpoint) + + def _detect_beamline(self) -> str | None: + """Beamline name derived from the host name, e.g. "x01da".""" + hostname = getattr(self.client, "_hostname", "") or "" + start = hostname.find("x") + return hostname[start : start + 5] if start != -1 else None + + def _build_execution_context(self) -> ExecutionContext: + digital_twin = auto_gain = None + if self.beamline in XAS_BEAMLINES: + logger.info(f"Scheduler at {self.beamline}: loading digital twin and auto-gain") + # pylint: disable=import-outside-toplevel from ....bec_ipython_client.plugins.auto_gain_core import AutoGainCore from ....bec_ipython_client.plugins.digital_twin_core.digital_twin_core import ( DigitalTwinCore, ) - self.digital_twin = DigitalTwinCore() - self.auto_gain = AutoGainCore() - else: - self.digital_twin = None - self.auto_gain = None + digital_twin = DigitalTwinCore() + auto_gain = AutoGainCore() + return ExecutionContext( + scans=self.scans, + dev=self.dev, + digital_twin=digital_twin, + auto_gain=auto_gain, + beamline=self.beamline, + ) - self._endpoint = schedule_endpoint(self.schedule_name) - self._lock = threading.Lock() # guards self.schedule (see module docstring) - self._abort_requested = False - self._pause_requested = False - self._guard_interrupt_requested = False # see "Guard interaction" in module docstring - self._current_report = None # ScanReport of the item currently executing, if any - self._current_item_kind: str | None = None # kind of the item _current_report belongs to - self._selected_item_id: str | None = None # survives list repopulation on refresh - self._clipboard_item: dict | None = None # copy/paste buffer, see _on_copy_clicked - - # Notes are auto-saved on a debounce timer rather than on every - # keystroke - see _on_notes_changed/_persist_notes. - self._notes_save_timer = QTimer(self) - self._notes_save_timer.setSingleShot(True) - self._notes_save_timer.setInterval(200) - self._notes_save_timer.timeout.connect(self._persist_notes) - - self.schedule_changed.connect(self._refresh_ui) - - self.schedule: Schedule = self._load_or_seed_schedule() - self._reconcile_with_live_state() - - self._guard = SignalGuard(self.connector, parent=self, dev=self.dev) - self._guard.paused.connect(self._on_guard_paused) - self._guard.resumed.connect(self._on_guard_resumed) - self._guard.value_update.connect(self._update_guard_label) - self._guard.configure(self.schedule.guard) - - self._build_ui() - self._refresh_ui() - - # Stay in sync with any other subscriber touching the same - # schedule (another instance of this widget, a script, ...). - self.bec_dispatcher.connect_slot(self._on_remote_update, self._endpoint) - - self._closing = threading.Event() - - def get_beamline(self): - hostname = self.client._hostname - start = hostname.find("x") - if start != -1: - return hostname[start : start + 5] - - # ------------------------------------------------------------------ # + # ------------------------------------------------------------------------------------------ # # UI - # ------------------------------------------------------------------ # - def _build_ui(self): + # ------------------------------------------------------------------------------------------ # + def _build_ui(self) -> None: layout = QVBoxLayout(self) + splitter = QSplitter(Qt.Orientation.Horizontal) + splitter.addWidget(self._build_schedule_group()) + splitter.addWidget(self._build_states_group()) + splitter.setStretchFactor(0, 3) + splitter.setStretchFactor(1, 1) + layout.addWidget(splitter, 1) + layout.addWidget(self._build_control_group()) + layout.addWidget(self._build_settings_group()) - schedule_group = QGroupBox("Schedule") - schedule_layout = QVBoxLayout(schedule_group) - - schedule_name_layout = QHBoxLayout() - name_label = QLabel("Name") - self.schedule_name_input = QLineEdit() - schedule_name_layout.addWidget(name_label) - schedule_name_layout.addWidget(self.schedule_name_input) - schedule_name_layout.addStretch() - schedule_layout.addLayout(schedule_name_layout) - - self.schedule_name_input.editingFinished.connect(self._on_schedule_name_change) - - edit_row = QHBoxLayout() - self.add_btn = MyButton("Add", "default") - self.edit_btn = MyButton("Edit", "default") - self.delete_btn = MyButton("Delete", "default") - self.move_up_btn = MyButton("Move Up", "default") - self.move_down_btn = MyButton("Move Down", "default") - self.copy_btn = MyButton("Copy", "default") - self.paste_btn = MyButton("Paste", "default") - for button in ( + self._buttons = [ self.add_btn, self.edit_btn, self.delete_btn, @@ -439,13 +239,60 @@ class Scheduler(BECWidget, QWidget): self.move_down_btn, self.copy_btn, self.paste_btn, + self.run_btn, + self.run_from_selection_btn, + self.pause_btn, + self.abort_btn, + self.reset_btn, + self.notifications_btn, + self.save_file_btn, + self.load_file_btn, + ] + self._update_notifications_label() + self._update_state_label() + + def _build_schedule_group(self) -> QGroupBox: + group = QGroupBox("Schedule") + layout = QVBoxLayout(group) + + name_row = QHBoxLayout() + name_row.addWidget(QLabel("Name")) + self.schedule_name_input = QLineEdit() + self.schedule_name_input.editingFinished.connect(self._on_schedule_name_change) + name_row.addWidget(self.schedule_name_input) + name_row.addStretch() + layout.addLayout(name_row) + + edit_row = QHBoxLayout() + self.add_btn = AccentButton("Add") + self.edit_btn = AccentButton("Edit") + self.delete_btn = AccentButton("Delete") + self.move_up_btn = AccentButton("Move Up") + self.move_down_btn = AccentButton("Move Down") + self.copy_btn = AccentButton("Copy") + self.paste_btn = AccentButton("Paste") + for button, slot in ( + (self.add_btn, self._on_add_clicked), + (self.edit_btn, self._on_edit_clicked), + (self.delete_btn, self._on_delete_clicked), + (self.move_up_btn, self._on_move_up_clicked), + (self.move_down_btn, self._on_move_down_clicked), + (self.copy_btn, self._on_copy_clicked), + (self.paste_btn, self._on_paste_clicked), ): + button.clicked.connect(slot) edit_row.addWidget(button) edit_row.addStretch() - schedule_layout.addLayout(edit_row) + layout.addLayout(edit_row) + + self.notice_label = QLabel() + self.notice_label.setWordWrap(True) + self.notice_label.setStyleSheet("color: #e6a800;") + self.notice_label.setVisible(False) + layout.addWidget(self.notice_label) self._table_model = ScheduleTableModel(parent=self, beamline=self.beamline) - self.table_view = MyTableView() + self.table_view = ScheduleTableView() self.table_view.setModel(self._table_model) self.table_view.configure_columns() self.table_view.selectionModel().currentRowChanged.connect(self._on_selection_changed) @@ -454,342 +301,185 @@ class Scheduler(BECWidget, QWidget): self.table_view.doubleClicked.connect(self._on_edit_clicked) self.table_view.copyPressed.connect(self._on_copy_clicked) self.table_view.pastePressed.connect(self._on_paste_clicked) + layout.addWidget(self.table_view, 1) - schedule_layout.addWidget(self.table_view) - - schedule_layout.addWidget(QLabel("Notes")) + layout.addWidget(QLabel("Notes")) self.notes_edit = QPlainTextEdit() self.notes_edit.setPlaceholderText("Notes about this schedule...") self.notes_edit.setPlainText(self.schedule.notes) self.notes_edit.setMaximumHeight(100) - self.notes_edit.textChanged.connect(self._on_notes_changed) - schedule_layout.addWidget(self.notes_edit) + self.notes_edit.textChanged.connect(self._notes_save_timer.start) + layout.addWidget(self.notes_edit) + return group - self.add_btn.clicked.connect(self._on_add_clicked) - self.edit_btn.clicked.connect(self._on_edit_clicked) - self.delete_btn.clicked.connect(self._on_delete_clicked) - self.move_up_btn.clicked.connect(self._on_move_up_clicked) - self.move_down_btn.clicked.connect(self._on_move_down_clicked) - self.copy_btn.clicked.connect(self._on_copy_clicked) - self.paste_btn.clicked.connect(self._on_paste_clicked) - - layout.addWidget(schedule_group) - - control_group = QGroupBox("Control") - control_layout = QVBoxLayout(control_group) + def _build_states_group(self) -> QGroupBox: + group = QGroupBox("Beamline states") + layout = QVBoxLayout(group) + layout.setContentsMargins(2, 2, 2, 2) + # BEC's stock BeamlineStateManager; only its scan-interlock controls are hidden. + self.state_manager = BeamlineStateManager(parent=group, client=self.client) + install_scan_interlock_hiding(self.state_manager) + layout.addWidget(self.state_manager) + return group + def _build_control_group(self) -> QGroupBox: + group = QGroupBox("Control") + layout = QVBoxLayout(group) run_row = QHBoxLayout() - self.run_btn = MyButton("Run / Continue", "success") - self.run_from_current_btn = MyButton("Run / Continue from current selection", "success") - self.abort_btn = MyButton("Pause now", "emergency") - self.pause_btn = MyButton("Pause after current item", "warning") - self.reset_btn = MyButton("Reset Schedule Status", "warning") - for button in ( - self.run_btn, - self.run_from_current_btn, - self.pause_btn, - self.abort_btn, - self.reset_btn, + self.run_btn = AccentButton("Run / Continue", "success") + self.run_from_selection_btn = AccentButton("Run / Continue from selection", "success") + self.pause_btn = AccentButton("Pause after current item", "warning") + self.abort_btn = AccentButton("Pause now", "emergency") + self.reset_btn = AccentButton("Reset schedule status", "warning") + for button, slot in ( + (self.run_btn, self._on_run_clicked), + (self.run_from_selection_btn, self._on_run_from_selection_clicked), + (self.pause_btn, self.pause_schedule), + (self.abort_btn, self.abort_schedule), + (self.reset_btn, self._on_reset_clicked), ): + button.clicked.connect(slot) run_row.addWidget(button) run_row.addStretch() - control_layout.addLayout(run_row) + layout.addLayout(run_row) - self.run_btn.clicked.connect(self.run_schedule) - self.run_from_current_btn.clicked.connect(self.run_schedule_from_selection) - self.abort_btn.clicked.connect(self.abort_schedule) - self.pause_btn.clicked.connect(self.pause_schedule) - self.reset_btn.clicked.connect(self.reset_schedule) - - layout.addWidget(control_group) - - settings_group = QGroupBox("Settings") - settings_layout = QVBoxLayout(settings_group) - - guard_row = QHBoxLayout() - self.guard_btn = MyButton("Auto-pause", "default") - self.guard_status_label = QLabel() - guard_row.addWidget(self.guard_btn) - guard_row.addWidget(self.guard_status_label) - guard_row.addStretch() - notifications_row = QHBoxLayout() - self.notifications_btn = MyButton("Notifications", "default") - self.notification_status_label = QLabel() - notifications_row.addWidget(self.notifications_btn) - notifications_row.addWidget(self.notification_status_label) - notifications_row.addStretch() - settings_layout.addLayout(guard_row) - settings_layout.addLayout(notifications_row) + state_row = QHBoxLayout() + state_row.addWidget(QLabel("Beamline states:")) + self.state_status_label = QLabel() + state_row.addWidget(self.state_status_label) + state_row.addStretch() + layout.addLayout(state_row) + return group + def _build_settings_group(self) -> QGroupBox: + group = QGroupBox("Settings") + row = QHBoxLayout(group) + self.notifications_btn = AccentButton("Notifications") self.notifications_btn.clicked.connect(self._on_notifications_clicked) - self.guard_btn.clicked.connect(self._on_guard_clicked) - - layout.addWidget(settings_group) - - file_group = QGroupBox("File") - file_layout = QHBoxLayout(file_group) - self.save_file_btn = MyButton("Save to file", "default") - self.load_file_btn = MyButton("Load from file", "default") - file_layout.addWidget(self.save_file_btn) - file_layout.addWidget(self.load_file_btn) - file_layout.addStretch() - + self.notification_status_label = QLabel() + self.save_file_btn = AccentButton("Save to file") self.save_file_btn.clicked.connect(self._on_save_to_file_clicked) + self.load_file_btn = AccentButton("Load from file") self.load_file_btn.clicked.connect(self._on_load_from_file_clicked) + row.addWidget(self.notifications_btn) + row.addWidget(self.notification_status_label) + row.addStretch() + row.addWidget(self.save_file_btn) + row.addWidget(self.load_file_btn) + return group - layout.addWidget(file_group) - - self._update_guard_label() - self._update_notifications_label() - - self.apply_theme() - - def apply_theme(self, theme: Optional[Literal["dark", "light"]] = None): - """ - Apply the theme - - Args: - theme (Optional[str]): Theme, either "dark", "light", or None. Defaults to None. - """ - if theme is None: - app = QApplication.instance() - theme = app.theme.theme # type: ignore - - for button in [ - self.add_btn, - self.edit_btn, - self.delete_btn, - self.move_down_btn, - self.move_up_btn, - self.copy_btn, - self.paste_btn, - self.run_btn, - self.abort_btn, - self.reset_btn, - self.notifications_btn, - self.guard_btn, - self.save_file_btn, - self.load_file_btn, - ]: + @SafeSlot(str) + def apply_theme(self, theme: str | None = None) -> None: + """Re-colour the buttons after a theme change.""" + for button in getattr(self, "_buttons", []): button.apply_theme() - # ------------------------------------------------------------------ # - # persistence - # ------------------------------------------------------------------ # - def _load_or_seed_schedule(self) -> Schedule: - logger.info("load or seed schedule") - msg: VariableMessage | None = self.connector.get(self._endpoint) - # msg = None # Overwrite any existing schedule in redis - if msg is not None: - logger.info(f"Got msg from endpoint: {msg.value}") - return Schedule.model_validate(msg.value) - - logger.info("No schedule found, create one now") - schedule = Schedule(schedule_name=self.schedule_name, items=[]) - self.connector.set_and_publish( - self._endpoint, VariableMessage(value=schedule.model_dump(mode="json")) - ) - return schedule - - def _persist_locked(self): - """Write `self.schedule` to Redis. Caller must hold `self._lock`.""" - self.connector.set_and_publish( - self._endpoint, VariableMessage(value=self.schedule.model_dump(mode="json")) - ) - - def _persist(self): - with self._lock: - self._persist_locked() - - @SafeSlot(dict, dict) - def _on_remote_update(self, msg_content: dict, _): - value = msg_content.get("value") if msg_content else None - if not value or value.get("schedule_name") != self.schedule_name: - return - with self._lock: - new_schedule = Schedule.model_validate(value) - if new_schedule.is_running: - # This instance's own execution task owns the current - # ScheduleItem objects (and will re-persist its own state - # again shortly); accepting a wholesale replacement here - # would orphan those objects. See module docstring. - logger.info( - f"Ignoring remote schedule update for {self.schedule_name} while a local run is in progress." - ) - return - self.schedule = new_schedule - self._apply_guard_settings() - self._refresh_ui() - - # ------------------------------------------------------------------ # - # reconciliation: figure out real state after being closed/reopened - # ------------------------------------------------------------------ # - def _reconcile_with_live_state(self): - """ - For any item last seen as RUNNING, ask BEC's own queue state - - not our local memory, which may be stale or from a previous - process - what actually happened to it while we were gone. - """ - queue_storage = self.client.queue.queue_storage - - # Force a fresh pull of the current queue snapshot from Redis - # rather than relying on pubsub messages we may have missed while - # this widget instance did not exist. - status_msg = self.connector.get(MessageEndpoints.scan_queue_status()) - if status_msg is not None: - queue_storage.update_with_status(status_msg) - - with self._lock: - self.schedule.is_running = False - for item in self.schedule.items: - if item.status != ScheduleItemStatus.RUNNING or not item.request_id: - continue - queue_item = queue_storage.find_queue_item_by_requestID(item.request_id) - if queue_item is not None and self._is_queue_item_active(queue_item): - # genuinely still in flight server-side; leave it - # RUNNING - run_schedule() will re-attach to it - # instead of resubmitting. - continue - item.status = ScheduleItemStatus.COMPLETED - item.finished_at = time.time() - self._persist_locked() - - @staticmethod - def _is_queue_item_active(queue_item) -> bool: - return str(getattr(queue_item, "status", "")).upper() in _ACTIVE_QUEUE_STATES - - # ------------------------------------------------------------------ # - # the "protected prefix" invariant (see module docstring) - the actual - # logic lives in schedule_logic.py, pure and independently testable; - # these thin wrappers just apply it to the current, lock-held state. - # ------------------------------------------------------------------ # - def _protected_prefix_length_locked(self) -> int: - return protected_prefix_length(self.schedule.items) - - def _index_of_locked(self, item_id: str | None) -> int | None: - return index_of(self.schedule.items, item_id) - @SafeSlot() - def _refresh_ui(self): + def _refresh_ui(self) -> None: + """Redraw the table and controls from the current schedule.""" with self._lock: schedule_name = self.schedule.schedule_name - items = list(self.schedule.items) + items = [item.model_copy() for item in self.schedule.items] notes = self.schedule.notes - self.schedule_name_input.setText(schedule_name) + if not self.schedule_name_input.hasFocus(): + self.schedule_name_input.setText(schedule_name) selection_model = self.table_view.selectionModel() selection_model.blockSignals(True) - - # Repopulate the model (full reset, same "clear and rebuild" idea - # the old QListWidget code used); the selected item's status-icon - # color is baked in via `_selected_item_id` so the highlighted row - # keeps its white icon straight after the reset repaints it. - self._table_model._selected_item_id = self._selected_item_id - self._table_model.set_items(items) - + self._table_model.set_items(items, selected_item_id=self._selected_item_id) row = self._table_model.row_of(self._selected_item_id) if row is not None: self.table_view.selectRow(row) - self.run_from_current_btn.setEnabled(True) else: self._selected_item_id = None - self.run_from_current_btn.setEnabled(False) - selection_model.blockSignals(False) - # Don't stomp on notes the operator is actively typing (e.g. a - # remote update, or our own debounce timer firing right after - # local edits) - only resync when the field isn't focused and the - # text actually differs from what we already have. + # Do not overwrite notes the operator is typing. if not self.notes_edit.hasFocus() and self.notes_edit.toPlainText() != notes: self.notes_edit.blockSignals(True) self.notes_edit.setPlainText(notes) self.notes_edit.blockSignals(False) + self.notice_label.setText(self._notice or "") + self.notice_label.setVisible(bool(self._notice)) self._update_buttons() - def _update_guard_label(self, *_): - if self._guard.enabled: - state = "OK" if self._guard.is_clear() else "PAUSED - waiting to resume" - value = self._guard.current_value - units = self._guard.units - value_text = f"{value:.{self._guard.prec}f}" if value is not None else "?" - self.guard_status_label.setText( - f"Enabled: {self._guard.device_name} = {value_text} {units} " - f"(pause < {self._guard.pause_below:g}, resume > {self._guard.resume_above:g}) [{state}]" - ) - else: - self.guard_status_label.setText("Disabled") - - def _update_buttons(self): + def _update_buttons(self) -> None: with self._lock: - items = list(self.schedule.items) - is_running = self.schedule.is_running + items = self.schedule.items + running = self._run_active or self.schedule.is_running + idx = index_of(items, self._selected_item_id) + selected = items[idx] if idx is not None else None + count = len(items) - idx = None - selected_item = None - - if self._selected_item_id is not None: - for i, item in enumerate(items): - if item.item_id == self._selected_item_id: - idx, selected_item = i, item - break - - can_edit_selected = ( - selected_item is not None and selected_item.status == ScheduleItemStatus.PENDING - ) - - self.run_btn.setEnabled(not is_running) - self.add_btn.setEnabled(True) - self.edit_btn.setEnabled(can_edit_selected) - self.delete_btn.setEnabled(can_edit_selected) - self.move_up_btn.setEnabled(can_edit_selected and idx not in (None, 0)) - self.move_down_btn.setEnabled( - can_edit_selected and idx is not None and idx < len(items) - 1 - ) - # Copy just captures the command text, so it's fine on any item - # (e.g. copying a COMPLETED item's command to reuse it later). - # Paste always goes through add_item(), which already clamps the - # insert position past the protected prefix - see _on_paste_clicked. - self.copy_btn.setEnabled(selected_item is not None) + editable = selected is not None and selected.status == ItemStatus.PENDING + self.edit_btn.setEnabled(editable) + self.delete_btn.setEnabled(editable) + self.move_up_btn.setEnabled(editable and idx not in (None, 0)) + self.move_down_btn.setEnabled(editable and idx is not None and idx < count - 1) + # Copy works on any item (e.g. reuse a completed scan); paste always inserts a new + # PENDING item behind the protected prefix. + self.copy_btn.setEnabled(selected is not None) self.paste_btn.setEnabled(self._clipboard_item is not None) - # Update execution control buttons - state = self._current_report is None # True if schedule is not running - self.run_btn.setEnabled(state) - self.run_from_current_btn.setEnabled(state and self._selected_item_id is not None) - self.abort_btn.setEnabled(not state) - self.pause_btn.setEnabled(not state) - self.reset_btn.setEnabled(state) + self.run_btn.setEnabled(not running) + self.run_from_selection_btn.setEnabled(not running and selected is not None) + self.pause_btn.setEnabled(self._run_active) + self.abort_btn.setEnabled(self._run_active) + self.reset_btn.setEnabled(not running) + self.load_file_btn.setEnabled(not running) @SafeSlot() - def _on_schedule_name_change(self): - name = self.schedule_name_input.text() - with self._lock: - self.schedule.schedule_name = name - self._persist_locked() + def _update_state_label(self) -> None: + if not hasattr(self, "state_status_label"): + return + ok = self.state_monitor.is_ok() + self.state_status_label.setText(self.state_monitor.summary()) + self.state_status_label.setStyleSheet("" if ok else "color: #e74c3c; font-weight: bold;") + def _update_notifications_label(self) -> None: + settings = self.schedule.notifications + if not settings.enabled: + self.notification_status_label.setText("Disabled") + return + kinds = [ + name + for name, enabled in ( + ("scans", settings.notify_scan), + ("moves", settings.notify_move), + ("device actions", settings.notify_device), + ) + if enabled + ] + target = "SciLog" if settings.target == NotificationTarget.SCILOG else "Signal" + events = ", ".join(event.value for event in settings.events) or "no item events" + self.notification_status_label.setText( + f"Enabled: via {target} for {', '.join(kinds) or 'no items'} on {events}" + ) + + # ---- selection ---- # @SafeSlot(QModelIndex, QModelIndex) - def _on_selection_changed(self, current: QModelIndex, previous: QModelIndex): + def _on_selection_changed(self, current: QModelIndex, _previous: QModelIndex) -> None: item_id = self._table_model.item_id(current.row()) if current.isValid() else None self._selected_item_id = item_id self._table_model.set_selected_item_id(item_id) self._update_buttons() @SafeSlot() - def _on_empty_space_clicked(self): - self.table_view.setCurrentIndex(QModelIndex()) - self._refresh_ui() + def _on_empty_space_clicked(self) -> None: + self._selected_item_id = None + self._table_model.set_selected_item_id(None) + self._update_buttons() - # ---- notes ---- # - def _on_notes_changed(self): - # Debounced: restart the timer on every keystroke, only persist - # once typing pauses, so we're not writing to Redis on every - # character. - self._notes_save_timer.start() + @SafeSlot() + def _on_schedule_name_change(self) -> None: + with self._lock: + self.schedule.schedule_name = self.schedule_name_input.text().strip() + self._persist_locked() - def _persist_notes(self): + @SafeSlot() + def _persist_notes(self) -> None: text = self.notes_edit.toPlainText() with self._lock: if self.schedule.notes == text: @@ -797,58 +487,142 @@ class Scheduler(BECWidget, QWidget): self.schedule.notes = text self._persist_locked() - # ---- save / restore to file ---- # + # ------------------------------------------------------------------------------------------ # + # Persistence + # ------------------------------------------------------------------------------------------ # + def _load_or_seed_schedule(self) -> Schedule: + """ + Load the persisted schedule from Redis, or create an empty one. + + A document that cannot be validated (e.g. written by the old, eval-based Scheduler) never + blocks the widget: it is logged and replaced by an empty schedule. + """ + msg: VariableMessage | None = self.connector.get(self._endpoint) + value = msg.value if msg is not None else None + if value is not None: + schedule, problem = schedule_from_document(value) + if schedule is not None: + return schedule + logger.warning( + f"Found {problem} in BEC at {self._endpoint.endpoint}; starting with an empty " + "schedule." + ) + logger.info(f"Discarded schedule document: {value}") + self._notice = ( + f"Found {problem} in BEC - started with an empty schedule. The previous items " + "can no longer be loaded and were written to the BEC log." + ) + schedule = Schedule(schedule_name=self.schedule_name) + self.connector.set_and_publish( + self._endpoint, VariableMessage(value=schedule.model_dump(mode="json")) + ) + return schedule + + def _persist_locked(self) -> None: + """Write the schedule to Redis. The caller must hold `self._lock`.""" + self.connector.set_and_publish( + self._endpoint, VariableMessage(value=self.schedule.model_dump(mode="json")) + ) + + @SafeSlot(dict, dict) + def _on_remote_update(self, msg_content: dict, _metadata: dict) -> None: + """Adopt a schedule published by someone else (or our own echo) while not running.""" + value = msg_content.get("value") if msg_content else None + if not value or self._run_active: + # While this instance executes, its executor owns the item objects; replacing them + # would orphan the running items. It re-publishes its own state anyway. + return + new_schedule, problem = schedule_from_document(value) + if new_schedule is None: + logger.warning(f"Ignoring {problem} published on {self._endpoint.endpoint}") + return + with self._lock: + self.schedule = new_schedule + self._update_notifications_label() + self._refresh_ui() + + def _reconcile_with_live_state(self) -> None: + """ + Fix up items persisted as RUNNING by a previous widget session: ask BEC's queue (not our + stale memory) whether their requests are still active. Requests still in flight stay + RUNNING and are re-attached to on the next run; finished ones become COMPLETED. Local + actions (delays, device calls) without a request were interrupted by the widget closing + and become ABORTED. + """ + queue_storage = self.client.queue.queue_storage + status_msg = self.connector.get(MessageEndpoints.scan_queue_status()) + if status_msg is not None: + queue_storage.update_with_status(status_msg) + + with self._lock: + self.schedule.is_running = False + for item in self.schedule.items: + if item.status != ItemStatus.RUNNING: + continue + if item.request_id: + queue_item = queue_storage.find_queue_item_by_requestID(item.request_id) + if queue_item is not None and is_queue_item_active(queue_item): + continue + item.status = ItemStatus.COMPLETED + item.report = item.report or "Done" + else: + item.status = ItemStatus.ABORTED + item.report = "Interrupted (widget was closed)" + item.finished_at = time.time() + self._persist_locked() + + def _emit_schedule_changed(self) -> None: + if not self._closing.is_set(): + self.schedule_changed.emit() + + # ---- file save / load ---- # + def _default_data_dir(self) -> str: + if not self.beamline: + return "" + return f"/sls/{self.beamline}/data/{self.client.active_account}/raw" + @SafeSlot() - def _on_save_to_file_clicked(self): + def _on_save_to_file_clicked(self) -> None: with self._lock: data = self.schedule.model_dump(mode="json") - default_name = "schedule.json" - - active_account = self.client.active_account - default_name = f"/sls/{self.beamline}/data/{active_account}/raw/{default_name}" - + start = self._default_data_dir() + default_path = str(Path(start) / "schedule.json") if start else "schedule.json" path, _ = QFileDialog.getSaveFileName( - self, "Save schedule to file", default_name, "JSON files (*.json);;All files (*)" + self, "Save schedule to file", default_path, "JSON files (*.json);;All files (*)" ) if not path: return try: - with open(path, "w", encoding="utf-8") as f: - json.dump(data, f, indent=2) + Path(path).write_text(json.dumps(data, indent=2), encoding="utf-8") except OSError as exc: QMessageBox.critical(self, "Save failed", f"Could not write to {path}:\n{exc}") return logger.info(f"Saved schedule '{self.schedule_name}' to {path}") @SafeSlot() - def _on_load_from_file_clicked(self): - with self._lock: - if self.schedule.is_running: - QMessageBox.warning( - self, - "Cannot load", - "Stop the running schedule (Abort) before loading a new one from file.", - ) - return - - start_folder = "" - - active_account = self.client.active_account - start_folder = f"/sls/{self.beamline}/data/{active_account}/raw" - + def _on_load_from_file_clicked(self) -> None: path, _ = QFileDialog.getOpenFileName( - self, "Load schedule from file", start_folder, "JSON files (*.json);;All files (*)" + self, + "Load schedule from file", + self._default_data_dir(), + "JSON files (*.json);;All files (*)", ) if not path: return - try: - with open(path, encoding="utf-8") as f: - data = json.load(f) - loaded = Schedule.model_validate(data) - except (OSError, json.JSONDecodeError, ValidationError) as exc: + data = json.loads(Path(path).read_text(encoding="utf-8")) + except (OSError, json.JSONDecodeError) as exc: + QMessageBox.critical(self, "Load failed", f"Could not read {path}:\n{exc}") + return + loaded, problem = schedule_from_document(data) + if loaded is None: + hint = " Please re-create the schedule." if "old" in problem else "" + QMessageBox.critical(self, "Load failed", f"{path} contains {problem}.{hint}") + return + unknown = sorted({item.action for item in loaded.items if item.action not in ACTIONS}) + if unknown: QMessageBox.critical( - self, "Load failed", f"Could not load a schedule from {path}:\n{exc}" + self, "Load failed", f"{path} contains unknown item types: {', '.join(unknown)}" ) return @@ -857,177 +631,49 @@ class Scheduler(BECWidget, QWidget): confirm = QMessageBox.question( self, "Load schedule", - f"Replace the current {current_count} item(s) with {len(loaded.items)} " - f"item(s) from the file?\n\nAll items will be loaded as PENDING, regardless " - f"of their status when the file was saved.", + f"Replace the current {current_count} item(s) with {len(loaded.items)} item(s) from " + "the file?\n\nAll items are loaded as PENDING.", ) if confirm != QMessageBox.StandardButton.Yes: return - # Loading a file restores a *definition* (what to run), not a past - # execution: every item comes back PENDING with a fresh item_id and - # its execution bookkeeping cleared, and is_running is always - # forced False here - regardless of what the file says - so we - # never end up "reconciling" against request/scan ids from a - # different session. schedule_name is deliberately left as this - # widget's own name, not overwritten by the file's. - new_items = [ - item.model_copy( - update={ - "item_id": str(uuid.uuid4()), - "status": ScheduleItemStatus.PENDING, - "request_id": None, - "scan_id": None, - "scan_number": None, - "error": None, - "started_at": None, - "finished_at": None, - } - ) - for item in loaded.items - ] - + # A file restores a *definition* (what to run), not a past execution. + new_items = [ScheduleItem(**item.definition()) for item in loaded.items] with self._lock: - if self.schedule.is_running: - QMessageBox.warning( - self, "Cannot load", "The schedule started running while the dialog was open." - ) + if self._run_active or self.schedule.is_running: + QMessageBox.warning(self, "Cannot load", "The schedule started running meanwhile.") return self.schedule.schedule_name = loaded.schedule_name self.schedule.items = new_items self.schedule.notes = loaded.notes - self.schedule.guard = loaded.guard self.schedule.notifications = loaded.notifications self._persist_locked() - - self._guard.configure(self.schedule.guard) self._selected_item_id = None - logger.info( - f"Loaded schedule from {path} ({len(new_items)} item(s)) into '{self.schedule_name}'" - ) + self._notice = None + logger.info(f"Loaded schedule from {path} ({len(new_items)} item(s))") + self._update_notifications_label() self._refresh_ui() - self._update_buttons() - # ---- notifications / auto-pause settings ---- # + # ---- notifications ---- # @SafeSlot() - def _on_notifications_clicked(self): + def _on_notifications_clicked(self) -> None: dialog = NotificationSettingsDialog(self.schedule.notifications, self.client, parent=self) if dialog.exec_() != QDialog.DialogCode.Accepted: return with self._lock: self.schedule.notifications = dialog.result_settings() - self._update_notifications_label() self._persist_locked() - self._refresh_ui() + self._update_notifications_label() - def _update_notifications_label(self): - if self.schedule.notifications.enabled: - types = [] - if self.schedule.notifications.notify_scan: - types.append("scan") - if self.schedule.notifications.notify_move: - types.append("move") - if self.schedule.notifications.notify_rpc: - types.append("rpc") - match self.schedule.notifications.target: - case NotificationTarget.SIGNAL: - target = "Signal Messenger" - case NotificationTarget.TEAMS: - target = "Microsoft Teams" - case NotificationTarget.SCILOG: - target = "Scilog" - events = [] - for event in self.schedule.notifications.events: - events.append(event.value) - text = f"Enabled: Notify through {target}, for item type {types}, and events {events}" - else: - text = "Disabled" - self.notification_status_label.setText(text) - - @SafeSlot() - def _on_guard_clicked(self): - device_names = sorted(self.dev.keys()) - dialog = GuardSettingsDialog(self.schedule.guard, device_names, parent=self) - if dialog.exec_() != QDialog.DialogCode.Accepted: - return - with self._lock: - self.schedule.guard = dialog.result_settings() - self._persist_locked() - self._guard.configure(self.schedule.guard) - self._refresh_ui() - self._update_guard_label() - - def _apply_guard_settings(self): - """Re-subscribe the guard after `self.schedule.guard` changes (e.g. from a remote update).""" - self._guard.configure(self.schedule.guard) - self._update_guard_label() - - @SafeSlot(float) - def _on_guard_paused(self, value: float): - logger.info( - f"Auto-pause guard tripped for schedule {self.schedule_name} (value = {value}); interrupting the running scan, if any." + def _notify_item_finished(self, item: ScheduleItem) -> None: + notify_item_finished( + self.client, self.schedule.notifications, item, describe_item(item, self.beamline) ) - if self._current_report is not None and self._current_item_kind == "scan": - self._guard_interrupt_requested = True - try: - self._current_report.cancel() - except Exception: # pylint: disable=broad-except - logger.exception("Failed to cancel the running scan for the auto-pause guard") - self.schedule_changed.emit() - self._update_guard_label() - - @SafeSlot(float) - def _on_guard_resumed(self, value: float): - logger.info( - f"Auto-pause guard cleared for schedule {self.schedule_name} (value = {value}); the execution loop will " - "resume the next scan item automatically." - ) - self.schedule_changed.emit() - - # ---- UI-triggered edit actions ---- # - @SafeSlot() - def _on_add_clicked(self): - dialog = ScheduleItemDialog( - self.scans, self.dev, parent=self, client=self.client, beamline=self.beamline - ) - if dialog.exec_() != QDialog.DialogCode.Accepted: - return - result = dialog.result() - with self._lock: - idx = self._index_of_locked(self._selected_item_id) - insert_at = None if idx is None else idx + 1 - new_id = self.add_item( - result["command"], - index=insert_at, - kind=result["kind"], - form_state=result["form_state"], - pause_on_failure=result["pause_on_failure"], - ) - self._selected_item_id = new_id - self._refresh_ui() - self._update_buttons() - - @SafeSlot() - @SafeSlot(QModelIndex) - def _on_edit_clicked(self, *_): - if self._selected_item_id is None: - return - with self._lock: - idx = self._index_of_locked(self._selected_item_id) - item = self.schedule.items[idx] if idx is not None else None - initial = ( - { - "kind": item.kind, - "command": item.command, - "form_state": item.form_state, - "pause_on_failure": item.pause_on_failure, - } - if item is not None - else None - ) - if initial is None: - return + # ------------------------------------------------------------------------------------------ # + # UI-triggered edit actions + # ------------------------------------------------------------------------------------------ # + def _open_item_dialog(self, initial: dict | None = None) -> dict | None: dialog = ScheduleItemDialog( self.scans, self.dev, @@ -1037,23 +683,46 @@ class Scheduler(BECWidget, QWidget): beamline=self.beamline, ) if dialog.exec_() != QDialog.DialogCode.Accepted: - return - result = dialog.result() - try: - self.edit_item( - self._selected_item_id, - result["command"], - kind=result["kind"], - form_state=result["form_state"], - pause_on_failure=result["pause_on_failure"], - ) - except RuntimeError as exc: - QMessageBox.warning(self, "Cannot edit item", str(exc)) - return + return None + return dialog.result() + + def _insert_after_selection(self, definition: dict) -> None: + with self._lock: + idx = index_of(self.schedule.items, self._selected_item_id) + self._selected_item_id = self.add_item(index=None if idx is None else idx + 1, **definition) self._refresh_ui() @SafeSlot() - def _on_delete_clicked(self): + def _on_add_clicked(self) -> None: + result = self._open_item_dialog() + if result is not None: + self._insert_after_selection(result) + + @SafeSlot() + @SafeSlot(QModelIndex) + def _on_edit_clicked(self, *_) -> None: + with self._lock: + idx = index_of(self.schedule.items, self._selected_item_id) + item = self.schedule.items[idx] if idx is not None else None + initial = item.definition() if item is not None else None + editable = item is not None and item.status == ItemStatus.PENDING + if initial is None: + return + if not editable: + QMessageBox.information( + self, "Cannot edit item", "Only items that have not started yet can be edited." + ) + return + result = self._open_item_dialog(initial) + if result is None: + return + try: + self.edit_item(self._selected_item_id, **result) + except RuntimeError as exc: + QMessageBox.warning(self, "Cannot edit item", str(exc)) + + @SafeSlot() + def _on_delete_clicked(self) -> None: if self._selected_item_id is None: return row = self.table_view.currentIndex().row() @@ -1062,212 +731,204 @@ class Scheduler(BECWidget, QWidget): except RuntimeError as exc: QMessageBox.warning(self, "Cannot delete item", str(exc)) return - self._selected_item_id = None - # delete_item()'s schedule_changed emit already ran _refresh_ui() - # with an empty selection; now re-select the row that took the - # deleted item's place (or the new last row, if it was last). - new_row = max(0, row - 1) - if 0 <= new_row < self._table_model.rowCount(): - self.table_view.selectRow(new_row) + # Select the row that took the deleted item's place (or the new last row). + self._selected_item_id = self._table_model.item_id( + min(row, self._table_model.rowCount() - 1) + ) self._refresh_ui() @SafeSlot() - def _on_move_up_clicked(self): - if self._selected_item_id is None: - return - try: + def _on_move_up_clicked(self) -> None: + if self._selected_item_id is not None: self.move_item_up(self._selected_item_id) - except RuntimeError as exc: - QMessageBox.warning(self, "Cannot move item", str(exc)) @SafeSlot() - def _on_move_down_clicked(self): - if self._selected_item_id is None: - return - try: + def _on_move_down_clicked(self) -> None: + if self._selected_item_id is not None: self.move_item_down(self._selected_item_id) - except RuntimeError as exc: - QMessageBox.warning(self, "Cannot move item", str(exc)) @SafeSlot() - def _on_copy_clicked(self): - """ - Copy the selected item's command onto an internal clipboard. Only - the command/kind/form_state are captured (not status, timestamps, - request/scan ids, ...) since pasting always creates a fresh, - PENDING item - this is "copy the command", not "duplicate the - history". - """ - if self._selected_item_id is None: - return + def _on_copy_clicked(self) -> None: + """Copy the definition of the selected item (not its execution state).""" with self._lock: - idx = self._index_of_locked(self._selected_item_id) + idx = index_of(self.schedule.items, self._selected_item_id) if idx is None: return - item = self.schedule.items[idx] - self._clipboard_item = { - "command": item.command, - "kind": item.kind, - "form_state": dict(item.form_state) if item.form_state else None, - "pause_on_failure": item.pause_on_failure, - } - logger.info(f"Copied schedule item: {self._clipboard_item['command']}") + self._clipboard_item = self.schedule.items[idx].definition() self._update_buttons() @SafeSlot() - def _on_paste_clicked(self): - """ - Paste the copied command as a new item, right after whatever is - currently selected (or at the end, if nothing is selected) - same - placement `_on_add_clicked` uses. This goes through `add_item()` - unchanged, so the existing protected-prefix clamping applies here - too: pasting can never land before an item that's already - running or finished. - """ - if self._clipboard_item is None: - return + def _on_paste_clicked(self) -> None: + """Insert the copied item as a new PENDING item after the selection (or at the end).""" + if self._clipboard_item is not None: + self._insert_after_selection(dict(self._clipboard_item)) + + # ---- run control buttons ---- # + def _ask_retry_unfinished(self) -> bool | None: + """Ask whether FAILED/ABORTED items should be repeated. None = cancelled.""" with self._lock: - idx = self._index_of_locked(self._selected_item_id) - insert_at = None if idx is None else idx + 1 - new_id = self.add_item( - self._clipboard_item["command"], - index=insert_at, - kind=self._clipboard_item["kind"], - form_state=self._clipboard_item["form_state"], - pause_on_failure=self._clipboard_item["pause_on_failure"], + unfinished = sum( + item.status in (ItemStatus.FAILED, ItemStatus.ABORTED) + for item in self.schedule.items + ) + if not unfinished: + return False + answer = QMessageBox.question( + self, + "Repeat unfinished items", + f"{unfinished} item(s) failed or were aborted. Repeat them?", + QMessageBox.StandardButton.Yes + | QMessageBox.StandardButton.No + | QMessageBox.StandardButton.Cancel, ) - self._selected_item_id = new_id - self._refresh_ui() - self._update_buttons() + if answer == QMessageBox.StandardButton.Cancel: + return None + return answer == QMessageBox.StandardButton.Yes - # ------------------------------------------------------------------ # - # RPC-exposed actions (USER_ACCESS) - # ------------------------------------------------------------------ # @SafeSlot() - def run_schedule(self, first_item: int = 0): - """Run the schedule, continuing from wherever it last left off.""" + def _on_run_clicked(self) -> None: + retry = self._ask_retry_unfinished() + if retry is not None: + self.run_schedule(retry_unfinished=retry) + + @SafeSlot() + def _on_run_from_selection_clicked(self) -> None: with self._lock: - if self.schedule.is_running: + idx = index_of(self.schedule.items, self._selected_item_id) + if idx is None: + return + retry = self._ask_retry_unfinished() + if retry is not None: + self.run_schedule(first_item=idx, retry_unfinished=retry) + + @SafeSlot() + def _on_reset_clicked(self) -> None: + answer = QMessageBox.question( + self, + "Reset schedule status", + "Mark all items as PENDING again and clear their reports?", + ) + if answer == QMessageBox.StandardButton.Yes: + self.reset_schedule() + + # ------------------------------------------------------------------------------------------ # + # RPC-exposed run control + # ------------------------------------------------------------------------------------------ # + def run_schedule(self, first_item: int = 0, retry_unfinished: bool = False) -> None: + """ + Start (or continue) executing the schedule. + + Already COMPLETED items are skipped. Does nothing if the schedule is already running. + + Args: + first_item: Index of the item to start with; items above it are ignored. + retry_unfinished: If True, FAILED and ABORTED items are executed again. + """ + with self._lock: + if self._run_active or self.schedule.is_running: + logger.info(f"Schedule {self.schedule_name} is already running.") return - repeat_aborted_item = False - if any(item.status == ScheduleItemStatus.ABORTED for item in self.schedule.items): - repeat_aborted_item = ( - QMessageBox.question( - self, - "Repeat aborted scan", - "Would you like to repeat the aborted scan?", - QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, - ) - == QMessageBox.StandardButton.Yes - ) - self._abort_requested = False - self._pause_requested = False + items = self.schedule.items + first_id = items[first_item].item_id if 0 <= first_item < len(items) else None self.schedule.is_running = True self._persist_locked() - self.schedule_changed.emit() + self._run_active = True self.submit_task( self._run_all, - repeat_aborted_item, - first_item, + first_id, + retry_unfinished, on_complete=self._on_run_finished, on_failed=self._on_run_failed, ) self._refresh_ui() - @SafeSlot() - def run_schedule_from_selection(self): - """Run the schedule, starting from the current selected item.""" - if self._selected_item_id is None: - return + def pause_schedule(self) -> None: + """Pause after the currently running item(s) have finished.""" + if self._run_active: + self._executor.request_pause() + + def abort_schedule(self) -> None: + """Pause now: cancel running scans/moves, interrupt delays and stop the schedule.""" + if self._run_active: + self._executor.request_abort() + + def reset_schedule(self) -> None: + """Mark all items PENDING again and clear their execution state (not while running).""" with self._lock: - idx = self._index_of_locked(self._selected_item_id) - if idx is not None: - self.run_schedule(idx) - - @SafeSlot() - def abort_schedule(self): - """Request that the schedule stop after the current item.""" - self._abort_requested = True - if self._current_report is not None: - try: - self._current_report.cancel() - except Exception: # pylint: disable=broad-except - logger.exception("Failed to cancel the currently running schedule item") - - @SafeSlot() - def pause_schedule(self): - self._pause_requested = True - - @SafeSlot() - def reset_schedule(self): - """Clear all execution state and start the schedule over from item 1.""" - with self._lock: - if self.schedule.is_running: - logger.info("Cannot reset schedule, as schedule is running!") + if self._run_active or self.schedule.is_running: + logger.info("Cannot reset the schedule while it is running.") return for item in self.schedule.items: - logger.info(f"Reset schedule item {item}") - item.status = ScheduleItemStatus.PENDING - item.request_id = None - item.scan_id = None - item.scan_number = None - item.error = None - item.started_at = None - item.finished_at = None + item.reset_execution() self._persist_locked() self.schedule_changed.emit() def get_status(self) -> dict: - """RPC-exposed: current schedule state, e.g. for another widget or a script.""" + """The complete schedule (definition and execution state) as a JSON-compatible dict.""" with self._lock: return self.schedule.model_dump(mode="json") - # ------------------------------------------------------------------ # - # RPC-exposed schedule editing (add / edit / move / delete) - # - # All of these only ever touch the PENDING suffix of the schedule - - # see the "protected prefix" section of the module docstring. They can - # be called at any time, including while the schedule is running. - # ------------------------------------------------------------------ # + # ------------------------------------------------------------------------------------------ # + # RPC-exposed schedule editing. These only ever touch PENDING items and can be called at any + # time, also while the schedule is running. + # ------------------------------------------------------------------------------------------ # + @staticmethod + def _validated_definition( + action: str, + params: dict | None, + wait_for_completion: bool, + pause_on_failure: bool, + pause_on_state: bool, + ) -> dict: + if action not in ACTIONS: + raise ValueError(f"Unknown action {action!r}. Available: {sorted(ACTIONS)}") + params = dict(params or {}) + json.dumps(params) # raises TypeError if not JSON-serialisable + return { + "action": action, + "params": params, + "wait_for_completion": wait_for_completion or action in ALWAYS_WAIT_ACTIONS, + "pause_on_failure": pause_on_failure, + "pause_on_state": pause_on_state, + } + def add_item( self, - command: str, + action: str, + params: dict | None = None, index: int | None = None, - kind: str = "custom", - form_state: dict | None = None, + wait_for_completion: bool = True, pause_on_failure: bool = False, + pause_on_state: bool = False, ) -> str: """ - RPC-exposed: insert a new, PENDING command into the schedule. - Safe to call while the schedule is running. + Insert a new PENDING item. Args: - command: command text, evaluated the same way as the existing - items (against `scans`/`dev`) once the schedule runs. - index: position to insert at (0 = first). Clamped so the item - can never land before something already running or - finished. Defaults to appending at the end. - kind/form_state: optional structured description of how - `command` was built (see `schedule_item.ScheduleItem`), - used to reopen the Edit dialog pre-filled. Leave as - defaults for a plain, hand-typed command. + action: Registered action name, e.g. "scan", "move", "delay", "wait_for_parent". + params: JSON-serialisable parameters of the action, e.g. + `{"device": "samx", "value": 1.0, "relative": False}` for "move" or + `{"seconds": 60}` for "delay". + index: Insert position (0 = first). Clamped so the item never lands before an item + that has already started. Defaults to the end. + wait_for_completion: If False, the following items start while this one runs. + pause_on_failure: Pause the schedule if this item fails. + pause_on_state: Only start while all beamline states are VALID/WARNING. Returns: - The new item's item_id. - """ - command = command.strip() - if not command: - raise ValueError("command must not be empty") + The id of the new item. - with self._lock: - protected = self._protected_prefix_length_locked() - item = ScheduleItem( - item_id=str(uuid.uuid4()), - command=command, - kind=kind, - form_state=form_state, - pause_on_failure=pause_on_failure, + Raises: + ValueError: Unknown action. + TypeError: Parameters are not JSON-serialisable. + """ + item = ScheduleItem( + **self._validated_definition( + action, params, wait_for_completion, pause_on_failure, pause_on_state ) + ) + with self._lock: + protected = protected_prefix_length(self.schedule.items) if index is None or index >= len(self.schedule.items): self.schedule.items.append(item) else: @@ -1279,299 +940,134 @@ class Scheduler(BECWidget, QWidget): def edit_item( self, item_id: str, - command: str, - kind: str = "custom", - form_state: dict | None = None, - pause_on_failure: bool = True, - ): + action: str, + params: dict | None = None, + wait_for_completion: bool = True, + pause_on_failure: bool = False, + pause_on_state: bool = False, + ) -> None: """ - RPC-exposed: change the command of an item that has not started - yet. Raises `RuntimeError` for an item that is already - running/finished. + Replace the definition of an item that has not started yet (see `add_item` for the + arguments). + + Raises: + RuntimeError: The item has already started or finished. """ - - logger.info(f"edit item with form_state: {form_state}") - - command = command.strip() - if not command: - raise ValueError("command must not be empty") - + definition = self._validated_definition( + action, params, wait_for_completion, pause_on_failure, pause_on_state + ) with self._lock: - idx = self._index_of_locked(item_id) + idx = index_of(self.schedule.items, item_id) if idx is None: return item = self.schedule.items[idx] - if item.status != ScheduleItemStatus.PENDING: + if item.status != ItemStatus.PENDING: raise RuntimeError("Only items that have not started yet (PENDING) can be edited.") - item.command = command - item.kind = kind - item.form_state = form_state - item.pause_on_failure = pause_on_failure + for key, value in definition.items(): + setattr(item, key, value) self._persist_locked() self.schedule_changed.emit() - def delete_item(self, item_id: str): + def delete_item(self, item_id: str) -> None: """ - RPC-exposed: remove an item that has not started yet. Raises - `RuntimeError` for an item that is already running/finished. + Remove an item that has not started yet. + + Raises: + RuntimeError: The item has already started or finished. """ with self._lock: - idx = self._index_of_locked(item_id) + idx = index_of(self.schedule.items, item_id) if idx is None: return - if self.schedule.items[idx].status != ScheduleItemStatus.PENDING: + if self.schedule.items[idx].status != ItemStatus.PENDING: raise RuntimeError("Only items that have not started yet (PENDING) can be deleted.") del self.schedule.items[idx] self._persist_locked() self.schedule_changed.emit() - def move_item(self, item_id: str, new_index: int): + def move_item(self, item_id: str, new_index: int) -> None: """ - RPC-exposed: move an item that has not started yet to a new - position (0 = first, but never before something already - running/finished). Raises `RuntimeError` for an item that is - already running/finished. + Move an item that has not started yet to `new_index` (never before an item that has + already started). + + Raises: + RuntimeError: The item has already started or finished. """ with self._lock: - idx = self._index_of_locked(item_id) + idx = index_of(self.schedule.items, item_id) if idx is None: return - if self.schedule.items[idx].status != ScheduleItemStatus.PENDING: + if self.schedule.items[idx].status != ItemStatus.PENDING: raise RuntimeError("Only items that have not started yet (PENDING) can be moved.") - protected = self._protected_prefix_length_locked() item = self.schedule.items.pop(idx) + protected = protected_prefix_length(self.schedule.items) new_index = max(protected, min(new_index, len(self.schedule.items))) self.schedule.items.insert(new_index, item) self._persist_locked() self.schedule_changed.emit() - def move_item_up(self, item_id: str): - """RPC-exposed: swap an item with the one directly before it.""" + def move_item_up(self, item_id: str) -> None: + """Swap an item with the one above it.""" with self._lock: - idx = self._index_of_locked(item_id) - target = idx - 1 if idx is not None and idx > 0 else None - if target is not None: - self.move_item(item_id, target) - self._refresh_ui() - self._update_buttons() + idx = index_of(self.schedule.items, item_id) + if idx: + self.move_item(item_id, idx - 1) - def move_item_down(self, item_id: str): - """RPC-exposed: swap an item with the one directly after it.""" + def move_item_down(self, item_id: str) -> None: + """Swap an item with the one below it.""" with self._lock: - idx = self._index_of_locked(item_id) - n = len(self.schedule.items) - target = idx + 1 if idx is not None and idx < n - 1 else None - if target is not None: - self.move_item(item_id, target) - self._refresh_ui() - self._update_buttons() + idx = index_of(self.schedule.items, item_id) + count = len(self.schedule.items) + if idx is not None and idx < count - 1: + self.move_item(item_id, idx + 1) - # ------------------------------------------------------------------ # - # execution - runs in a background task (BECConnector.submit_task, - # backed by a QThreadPool worker thread; UI updates below go through - # the `schedule_changed` Qt signal so they are marshalled back onto - # the GUI thread instead of touching widgets directly) - # ------------------------------------------------------------------ # - def _pick_next_runnable_locked(self, repeat_aborted_item, first_item): - """Must be called while holding `self._lock`; see `schedule_logic.pick_next_runnable`.""" - return pick_next_runnable(self.schedule.items, repeat_aborted_item, first_item) + # ------------------------------------------------------------------------------------------ # + # Execution (worker thread, via submit_task) + # ------------------------------------------------------------------------------------------ # + def _run_all(self, first_item_id: str | None, retry_unfinished: bool) -> None: + """Worker-thread body of a run: notifications around `ScheduleExecutor.run`.""" + settings = self.schedule.notifications + notify_schedule_state(self.client, settings, self.schedule.schedule_name, "started") + outcome: RunOutcome = "widget_closed" + try: + outcome = self._executor.run(first_item_id, retry_unfinished) + finally: + notify_schedule_state(self.client, settings, self.schedule.schedule_name, outcome) + with self._lock: + self.schedule.is_running = False + self._persist_locked() @SafeSlot() - def _on_run_finished(self): + def _on_run_finished(self) -> None: + self._run_active = False self._refresh_ui() @SafeSlot(str) - def _on_run_failed(self, error: str): - logger.error(f"Schedule execution task failed unexpectedly: {error}") + def _on_run_failed(self, error: str) -> None: + logger.error(f"Schedule execution failed unexpectedly: {error}") + self._run_active = False with self._lock: self.schedule.is_running = False self._persist_locked() self._refresh_ui() - def _emit_schedule_changed(self): - if self._closing.is_set(): - return - - self.schedule_changed.emit() - - def closeEvent(self, event): - """Ensure cleanup is invoked when the widget is closed via the window system.""" - self.cleanup() - super().closeEvent(event) - - def cleanup(self): - """Stop background execution loops without cancelling server-side scans.""" + def cleanup(self) -> None: + """ + Stop the execution loop without cancelling server-side work, flush unsaved notes and + release all subscriptions. Child BEC widgets (the state manager) are cleaned up by + `BECWidget.cleanup`. + """ self._closing.set() - - # Flush any not-yet-saved notes edit instead of losing it - the - # debounce timer won't get a chance to fire once we're closing. + self._executor.notify_closing() + self._live_timer.stop() if self._notes_save_timer.isActive(): self._notes_save_timer.stop() self._persist_notes() - - # Interrupt guard wait loops if any thread is blocked in self._guard.wait_until_clear() - self._guard.cleanup() - + self.state_monitor.cleanup() super().cleanup() - def _run_all(self, repeat_aborted_item, first_item): - notify_schedule_state( - self.client, self.schedule.notifications, self.schedule.schedule_name, "started" - ) - namespace = { - "scans": self.scans, - "dev": self.dev, - "digital_twin": self.digital_twin, - "auto_gain": self.auto_gain, - "np": np, - } - while ( - not self._abort_requested and not self._closing.is_set() and not self._pause_requested - ): - with self._lock: - next_item = self._pick_next_runnable_locked(repeat_aborted_item, first_item) - if next_item is None or next_item == "stop": - logger.info("Next item is None or stop") - break - if next_item.status == ScheduleItemStatus.RUNNING: - self._await_running_item(next_item) - else: - self._execute_item(next_item, namespace) - if self._abort_requested: - notify_schedule_state( - self.client, self.schedule.notifications, self.schedule.schedule_name, "aborted" - ) - elif self._closing.is_set(): - notify_schedule_state( - self.client, - self.schedule.notifications, - self.schedule.schedule_name, - "widget_closed", - ) - elif self._pause_requested: - notify_schedule_state( - self.client, self.schedule.notifications, self.schedule.schedule_name, "paused" - ) - else: - notify_schedule_state( - self.client, self.schedule.notifications, self.schedule.schedule_name, "finished" - ) - with self._lock: - logger.info("in _run_all, set is_running to false") - self.schedule.is_running = False - self._persist_locked() - self._emit_schedule_changed() - def _execute_item(self, item: ScheduleItem, namespace: dict): - if item.kind == "scan": - # Block here until the guard is clear, operator aborts, or widget closes - cleared = self._guard.wait_until_clear( - should_abort=lambda: self._abort_requested or self._closing.is_set() - ) - self._emit_schedule_changed() - if not cleared or self._closing.is_set(): - return - - with self._lock: - item.status = ScheduleItemStatus.RUNNING - item.error = None - item.started_at = time.time() - self._persist_locked() - self._emit_schedule_changed() - - try: - report = eval( - item.command, {"__builtins__": {}}, namespace - ) # noqa: S307 pylint: disable=eval-used - self._current_report = report - self._current_item_kind = item.kind - - request = getattr(report, "request", None) - with self._lock: - item.request_id = getattr(request, "requestID", None) - self._persist_locked() - - # RPC commands may not return a status, thus have no wait() - if item.kind != "custom": - report.wait() - - if self._closing.is_set(): - # The widget was closed mid-item; exit without changing status in Redis. - # Reconciliation logic will handle state on reopen. - return - - scan = getattr(report, "scan", None) - with self._lock: - item.scan_id = getattr(scan, "scan_id", None) if scan else None - item.scan_number = getattr(scan, "scan_number", None) if scan else None - item.status = ( - ScheduleItemStatus.ABORTED - if self._abort_requested - else ScheduleItemStatus.COMPLETED - ) - except Exception: # pylint: disable=broad-except - if self._closing.is_set(): - return - with self._lock: - if self._guard_interrupt_requested: - item.status = ScheduleItemStatus.PENDING - item.request_id = None - item.scan_id = None - item.scan_number = None - item.error = None - item.started_at = None - self._guard_interrupt_requested = False - elif self._abort_requested: - item.status = ScheduleItemStatus.ABORTED - else: - item.status = ScheduleItemStatus.FAILED - item.error = traceback.format_exc() - logger.error(f"Schedule item failed: {item.command}\n{item.error}") - if item.pause_on_failure: - self._abort_requested = True - finally: - if not self._closing.is_set(): - with self._lock: - if item.status != ScheduleItemStatus.PENDING: - item.finished_at = time.time() - self._persist_locked() - final_status = item.status - self._current_report = None - self._current_item_kind = None - self._emit_schedule_changed() - else: - final_status = None - - if final_status in ( - ScheduleItemStatus.COMPLETED, - ScheduleItemStatus.FAILED, - ScheduleItemStatus.ABORTED, - ): - notify_item_finished( - self.client, - item.kind, - item.command, - item.scan_number, - final_status=final_status, - settings=self.schedule.notifications, - error=item.error, - ) - - def _await_running_item(self, item: ScheduleItem): - queue_storage = self.client.queue.queue_storage - while not self._abort_requested and not self._closing.is_set(): - queue_item = queue_storage.find_queue_item_by_requestID(item.request_id) - if queue_item is None or not self._is_queue_item_active(queue_item): - with self._lock: - item.status = ScheduleItemStatus.COMPLETED - item.finished_at = time.time() - self._persist_locked() - self._emit_schedule_changed() - return - time.sleep(0.5) - - -if __name__ == "__main__": +if __name__ == "__main__": # pragma: no cover app = QApplication(sys.argv) dispatcher = BECDispatcher(gui_id="Scheduler") win = Scheduler() diff --git a/debye_bec/bec_widgets/widgets/scheduler/state_monitor.py b/debye_bec/bec_widgets/widgets/scheduler/state_monitor.py new file mode 100644 index 0000000..d43ed6b --- /dev/null +++ b/debye_bec/bec_widgets/widgets/scheduler/state_monitor.py @@ -0,0 +1,190 @@ +""" +Watch all BEC beamline states on behalf of the Scheduler ("pause on state"). + +Beamline states are created, edited and deleted with BEC's own `BeamlineStateManager` widget, +which the Scheduler embeds. This module only *reads* them: it follows +`MessageEndpoints.available_beamline_states()` to learn which states exist, subscribes to +`MessageEndpoints.beamline_state(name)` of each of them through the `BECDispatcher`, and keeps +track of whether all of them are acceptable (VALID or WARNING). + +Dispatcher callbacks run on the Qt GUI thread. The schedule executor runs on a worker thread and +only uses the thread-safe `is_ok()` / `wait_until_ok()` methods, backed by a `threading.Event`. +""" + +from __future__ import annotations + +import threading +from typing import Any, Callable + +from bec_lib.endpoints import MessageEndpoints +from bec_lib.logger import bec_logger +from bec_widgets.utils.error_popups import SafeSlot + +# pylint: disable=no-name-in-module # qtpy re-exports are not visible to pylint +from qtpy.QtCore import QObject, Signal + +logger = bec_logger.logger + +#: Beamline state statuses that do not pause gated schedule items. +OK_STATUSES = frozenset({"valid", "warning"}) + + +def _state_name(state: Any) -> str | None: + """Name of a `BeamlineStateConfig` (object or plain dict).""" + if isinstance(state, dict): + return state.get("name") + return getattr(state, "name", None) + + +class BeamlineStateMonitor(QObject): + """ + Aggregates the status of all beamline states. + + Signals: + tripped(): At least one state left VALID/WARNING (was OK before). + recovered(): All states are VALID/WARNING again (was not OK before). + statuses_changed(): Any status changed, or a state was added/removed. + + A state whose status is still unknown (e.g. right after creation, or if its device is not + reachable) counts as not OK, so that gated items never start on unverified conditions. + Without any beamline states, the monitor is always OK. + """ + + tripped = Signal() + recovered = Signal() + statuses_changed = Signal() + + def __init__(self, client, bec_dispatcher, parent: QObject | None = None) -> None: + super().__init__(parent) + self._client = client + self._dispatcher = bec_dispatcher + self._lock = threading.Lock() + self._statuses: dict[str, str] = {} + self._ok_event = threading.Event() + self._ok_event.set() + + self._dispatcher.connect_slot( + self._on_available_states, MessageEndpoints.available_beamline_states() + ) + msg = self._client.connector.get_last( + MessageEndpoints.available_beamline_states(), key="data" + ) + if msg is not None: + self._on_available_states(msg.content, msg.metadata) + + # ------------------------------------------------------------------------------------------ # + # Thread-safe queries (used by the executor's worker thread) + # ------------------------------------------------------------------------------------------ # + def is_ok(self) -> bool: + """True if every beamline state is VALID or WARNING (or no states exist).""" + return self._ok_event.is_set() + + def wait_until_ok(self, should_abort: Callable[[], bool], poll_interval: float = 0.5) -> bool: + """ + Block the calling (non-GUI) thread until `is_ok()` is True. + + Args: + should_abort: Polled between waits; the wait ends early if it returns True. + poll_interval: Seconds between two `should_abort` checks. + + Returns: + True if the states became OK, False if `should_abort()` returned True first. + """ + while not should_abort(): + if self._ok_event.wait(timeout=poll_interval): + return True + return False + + def statuses(self) -> dict[str, str]: + """A copy of the current `{state name: status}` mapping.""" + with self._lock: + return dict(self._statuses) + + def blocking_states(self) -> list[str]: + """Names of the states that currently prevent gated items from running.""" + with self._lock: + return sorted( + name for name, status in self._statuses.items() if status not in OK_STATUSES + ) + + def summary(self) -> str: + """One-line, human-readable summary, e.g. for a status label.""" + statuses = self.statuses() + if not statuses: + return "No beamline states defined" + blocking = self.blocking_states() + if not blocking: + return f"All {len(statuses)} beamline state(s) OK" + return f"Not OK: {', '.join(blocking)} - items with 'pause on state' will wait" + + # ------------------------------------------------------------------------------------------ # + # Dispatcher callbacks (GUI thread) + # ------------------------------------------------------------------------------------------ # + @SafeSlot(dict, dict) + def _on_available_states(self, content: dict, _metadata: dict | None = None) -> None: + names = {name for state in content.get("states", []) if (name := _state_name(state))} + with self._lock: + known = set(self._statuses) + for name in known - names: + self._dispatcher.disconnect_slot( + self._on_state_update, MessageEndpoints.beamline_state(name) + ) + with self._lock: + self._statuses.pop(name, None) + for name in names - known: + with self._lock: + self._statuses[name] = "unknown" + self._dispatcher.connect_slot( + self._on_state_update, MessageEndpoints.beamline_state(name) + ) + last = self._client.connector.get_last( + MessageEndpoints.beamline_state(name), key="data" + ) + if last is not None: + self._set_status(name, last.content.get("status")) + self._evaluate() + + @SafeSlot(dict, dict) + def _on_state_update(self, content: dict, _metadata: dict | None = None) -> None: + name = content.get("name") + if not name: + return + with self._lock: + if name not in self._statuses: + return # update of a state that was just removed + self._set_status(name, content.get("status")) + self._evaluate() + + def _set_status(self, name: str, status: Any) -> None: + with self._lock: + self._statuses[name] = str(status or "unknown").lower() + + def _evaluate(self) -> None: + with self._lock: + ok = all(status in OK_STATUSES for status in self._statuses.values()) + was_ok = self._ok_event.is_set() + if ok: + self._ok_event.set() + else: + self._ok_event.clear() + self.statuses_changed.emit() + if was_ok and not ok: + logger.info(f"Scheduler: beamline state(s) not OK: {self.blocking_states()}") + self.tripped.emit() + elif ok and not was_ok: + logger.info("Scheduler: all beamline states OK again.") + self.recovered.emit() + + def cleanup(self) -> None: + """Disconnect all dispatcher subscriptions and release waiting threads.""" + self._dispatcher.disconnect_slot( + self._on_available_states, MessageEndpoints.available_beamline_states() + ) + with self._lock: + names = list(self._statuses) + self._statuses.clear() + for name in names: + self._dispatcher.disconnect_slot( + self._on_state_update, MessageEndpoints.beamline_state(name) + ) + self._ok_event.set() diff --git a/debye_bec/bec_widgets/widgets/scheduler/widgets.py b/debye_bec/bec_widgets/widgets/scheduler/widgets.py new file mode 100644 index 0000000..ffff9a3 --- /dev/null +++ b/debye_bec/bec_widgets/widgets/scheduler/widgets.py @@ -0,0 +1,35 @@ +"""Small reusable Qt widgets of the Scheduler.""" + +from __future__ import annotations + +from bec_widgets.utils.colors import get_accent_colors + +# pylint: disable=no-name-in-module # qtpy re-exports are not visible to pylint +from qtpy.QtWidgets import QPushButton + + +class AccentButton(QPushButton): + """ + Push button filled with one of BEC's accent colours ("default", "success", "warning", + "emergency", ...), greyed out while disabled. + """ + + def __init__(self, text: str = "", color: str = "default", parent=None): + self.color = color + super().__init__(text, parent) + self.apply_theme() + + def apply_theme(self) -> None: + """Re-apply the colours, e.g. after a theme change.""" + if self.isEnabled(): + color = getattr(get_accent_colors(), self.color).name() + self.setStyleSheet(f"QPushButton {{ background-color: {color}; color: white; }}") + else: + self.setStyleSheet( + "QPushButton { background-color: rgb(120, 120, 120); color: white; }" + ) + + def setEnabled(self, enable: bool = True) -> None: # noqa: N802 pylint: disable=invalid-name + """Enable/disable the button and update its colours.""" + super().setEnabled(enable) + self.apply_theme() diff --git a/tests/tests_bec_widgets/test_scheduler_executor.py b/tests/tests_bec_widgets/test_scheduler_executor.py new file mode 100644 index 0000000..dceae41 --- /dev/null +++ b/tests/tests_bec_widgets/test_scheduler_executor.py @@ -0,0 +1,284 @@ +"""Tests for the Scheduler's execution loop (`ScheduleExecutor`) with fake BEC handles.""" + +# pylint: disable=missing-function-docstring,missing-class-docstring +import threading +import time +from unittest import mock + +import pytest + +from debye_bec.bec_widgets.widgets.scheduler.actions import ExecutionContext +from debye_bec.bec_widgets.widgets.scheduler.executor import ScheduleExecutor +from debye_bec.bec_widgets.widgets.scheduler.models import ItemStatus, ScheduleItem + +TIMEOUT = 5 + + +class FakeHandle: + """Mimics a BEC ScanReport: `wait()` blocks until released, `cancel()` makes it raise.""" + + def __init__(self, release: threading.Event | None = None): + self.release = release or threading.Event() + if release is None: + self.release.set() + self.cancelled = threading.Event() + self.request = None + + def wait(self): + while not self.release.wait(0.01): + if self.cancelled.is_set(): + raise RuntimeError("cancelled") + if self.cancelled.is_set(): + raise RuntimeError("cancelled") + + def cancel(self): + self.cancelled.set() + + +class FakeStateMonitor: + def __init__(self): + self.ok = threading.Event() + self.ok.set() + + def is_ok(self): + return self.ok.is_set() + + def wait_until_ok(self, should_abort, poll_interval=0.01): + while not should_abort(): + if self.ok.wait(poll_interval): + return True + return False + + def blocking_states(self): + return [] if self.ok.is_set() else ["beam"] + + +class Harness: + def __init__(self, items: list[ScheduleItem]): + self.items = items + self.scans = mock.MagicMock() + self.dev = {"samx": mock.MagicMock()} + self.dev["samx"].move.return_value = FakeHandle() + self.monitor = FakeStateMonitor() + self.notified: list[str] = [] + self.closing = threading.Event() + self.executor = ScheduleExecutor( + lock=threading.Lock(), + get_items=lambda: self.items, + persist_locked=lambda: None, + emit_changed=lambda: None, + context=ExecutionContext(scans=self.scans, dev=self.dev), + state_monitor=self.monitor, + notify_item=lambda item: self.notified.append(item.item_id), + get_queue_storage=mock.MagicMock, + closing=self.closing, + ) + self.outcome = None + self.thread = None + + def start(self, **kwargs): + def _run(): + self.outcome = self.executor.run(**kwargs) + + self.thread = threading.Thread(target=_run, daemon=True) + self.thread.start() + + def join(self): + self.thread.join(TIMEOUT) + assert not self.thread.is_alive(), "executor did not finish" + return self.outcome + + +def _wait_until(predicate, timeout=TIMEOUT): + deadline = time.time() + timeout + while time.time() < deadline: + if predicate(): + return + time.sleep(0.01) + pytest.fail("condition not reached") + + +def _scan(item_id, wait=True, pause_on_state=False): + return ScheduleItem( + item_id=item_id, + action="scan", + params={"scan_name": "line_scan", "args": [], "kwargs": {}}, + wait_for_completion=wait, + pause_on_state=pause_on_state, + ) + + +def _delay(item_id, seconds=0.05): + return ScheduleItem(item_id=item_id, action="delay", params={"seconds": seconds}) + + +def _move(item_id, pause_on_failure=False): + return ScheduleItem( + item_id=item_id, + action="move", + params={"device": "samx", "value": 1.0, "relative": False, "units": "mm"}, + pause_on_failure=pause_on_failure, + ) + + +def _by_id(harness, item_id): + return next(item for item in harness.items if item.item_id == item_id) + + +def test_sequential_items_complete(): + harness = Harness([_move("m1"), _delay("d1"), _move("m2")]) + harness.start() + assert harness.join() == "finished" + assert all(item.status == ItemStatus.COMPLETED for item in harness.items) + assert harness.notified == ["m1", "d1", "m2"] + assert _by_id(harness, "d1").report.startswith("Waited") + + +def test_background_item_runs_concurrently_with_its_block(): + release = threading.Event() + parent_handle = FakeHandle(release) + harness = Harness( + [ + _scan("parent", wait=False), + _delay("child1"), + _delay("child2"), + ScheduleItem(item_id="join", action="wait_for_parent"), + _move("after"), + ] + ) + harness.scans.line_scan.return_value = parent_handle + harness.start() + + _wait_until(lambda: _by_id(harness, "child2").status == ItemStatus.COMPLETED) + assert _by_id(harness, "parent").status == ItemStatus.RUNNING + _wait_until(lambda: _by_id(harness, "join").status == ItemStatus.RUNNING) + time.sleep(0.1) + assert _by_id(harness, "after").status == ItemStatus.PENDING # still waiting for parent + + release.set() + assert harness.join() == "finished" + assert all(item.status == ItemStatus.COMPLETED for item in harness.items) + assert _by_id(harness, "after").started_at >= _by_id(harness, "parent").finished_at + + +def test_next_background_item_implicitly_waits_for_previous(): + release = threading.Event() + first, second = FakeHandle(release), FakeHandle() + harness = Harness([_scan("bg1", wait=False), _delay("child"), _scan("bg2", wait=False)]) + harness.scans.line_scan.side_effect = [first, second] + harness.start() + _wait_until(lambda: _by_id(harness, "child").status == ItemStatus.COMPLETED) + time.sleep(0.1) + assert _by_id(harness, "bg2").status == ItemStatus.PENDING + release.set() + assert harness.join() == "finished" + assert _by_id(harness, "bg2").started_at >= _by_id(harness, "bg1").finished_at + + +def test_failure_without_pause_continues(): + harness = Harness([_move("bad"), _move("good")]) + harness.dev["samx"].move.side_effect = [RuntimeError("motor error"), FakeHandle()] + harness.start() + assert harness.join() == "finished" + assert _by_id(harness, "bad").status == ItemStatus.FAILED + assert "motor error" in _by_id(harness, "bad").report + assert _by_id(harness, "good").status == ItemStatus.COMPLETED + + +def test_pause_on_failure_pauses(): + harness = Harness([_move("bad", pause_on_failure=True), _move("next")]) + harness.dev["samx"].move.side_effect = RuntimeError("motor error") + harness.start() + assert harness.join() == "paused_on_failure" + assert _by_id(harness, "bad").status == ItemStatus.FAILED + assert _by_id(harness, "next").status == ItemStatus.PENDING + + +def test_retry_unfinished_runs_failed_item_once(): + harness = Harness([_move("bad"), _move("next")]) + harness.items[0].status = ItemStatus.FAILED + harness.dev["samx"].move.side_effect = [RuntimeError("again"), FakeHandle()] + harness.start(retry_unfinished=True) + assert harness.join() == "finished" + assert _by_id(harness, "bad").status == ItemStatus.FAILED + assert _by_id(harness, "next").status == ItemStatus.COMPLETED + assert harness.dev["samx"].move.call_count == 2 + + +def test_abort_interrupts_delay(): + harness = Harness([_delay("long", seconds=100), _move("next")]) + harness.start() + _wait_until(lambda: _by_id(harness, "long").status == ItemStatus.RUNNING) + harness.executor.request_abort() + assert harness.join() == "aborted" + assert _by_id(harness, "long").status == ItemStatus.ABORTED + assert _by_id(harness, "next").status == ItemStatus.PENDING + + +def test_pause_after_current_item(): + harness = Harness([_delay("d1", seconds=0.2), _move("next")]) + harness.start() + _wait_until(lambda: _by_id(harness, "d1").status == ItemStatus.RUNNING) + harness.executor.request_pause() + assert harness.join() == "paused" + assert _by_id(harness, "d1").status == ItemStatus.COMPLETED + assert _by_id(harness, "next").status == ItemStatus.PENDING + + +def test_pause_on_state_waits_for_states(): + harness = Harness([_move("gated")]) + harness.items[0].pause_on_state = True + harness.monitor.ok.clear() + harness.start() + _wait_until(lambda: (_by_id(harness, "gated").report or "").startswith("Waiting")) + assert _by_id(harness, "gated").status == ItemStatus.PENDING + harness.monitor.ok.set() + assert harness.join() == "finished" + assert _by_id(harness, "gated").status == ItemStatus.COMPLETED + + +def test_state_trip_cancels_and_repeats_gated_scan(): + first = FakeHandle(threading.Event()) # blocks until cancelled + second = FakeHandle() + harness = Harness([_scan("scan", pause_on_state=True)]) + harness.scans.line_scan.side_effect = [first, second] + harness.start() + _wait_until(lambda: _by_id(harness, "scan").status == ItemStatus.RUNNING) + + harness.monitor.ok.clear() + harness.executor.on_states_tripped() + _wait_until(lambda: first.cancelled.is_set()) + _wait_until(lambda: (_by_id(harness, "scan").report or "").startswith("Waiting")) + harness.monitor.ok.set() + + assert harness.join() == "finished" + assert _by_id(harness, "scan").status == ItemStatus.COMPLETED + assert harness.scans.line_scan.call_count == 2 + + +def test_state_trip_does_not_interrupt_ungated_scan(): + release = threading.Event() + handle = FakeHandle(release) + harness = Harness([_scan("scan", pause_on_state=False)]) + harness.scans.line_scan.return_value = handle + harness.start() + _wait_until(lambda: _by_id(harness, "scan").status == ItemStatus.RUNNING) + harness.executor.on_states_tripped() + assert not handle.cancelled.is_set() + release.set() + assert harness.join() == "finished" + + +def test_closing_leaves_running_item_untouched(): + harness = Harness([_scan("scan")]) + harness.scans.line_scan.return_value = FakeHandle(threading.Event()) + harness.start() + _wait_until(lambda: _by_id(harness, "scan").status == ItemStatus.RUNNING) + harness.closing.set() + harness.executor.notify_closing() + # The fake scan only ends when cancelled; closing must not cancel it server-side. + time.sleep(0.1) + assert _by_id(harness, "scan").status == ItemStatus.RUNNING + harness.scans.line_scan.return_value.cancel() + assert harness.join() == "widget_closed" + assert _by_id(harness, "scan").status == ItemStatus.RUNNING diff --git a/tests/tests_bec_widgets/test_scheduler_logic.py b/tests/tests_bec_widgets/test_scheduler_logic.py new file mode 100644 index 0000000..e53d534 --- /dev/null +++ b/tests/tests_bec_widgets/test_scheduler_logic.py @@ -0,0 +1,311 @@ +"""Tests for the Scheduler's data model, action registry and pure scheduling logic.""" + +# pylint: disable=missing-function-docstring +import json +from unittest import mock + +import pytest + +from debye_bec.bec_widgets.widgets.scheduler.actions import ( + ACTIONS, + ExecutionContext, + describe_item, + format_duration, + run_action, +) +from debye_bec.bec_widgets.widgets.scheduler.models import ( + ItemStatus, + NotificationTarget, + Schedule, + ScheduleItem, + is_legacy_schedule, + schedule_from_document, +) +from debye_bec.bec_widgets.widgets.scheduler.schedule_logic import ( + BlockRole, + block_parents, + block_roles, + item_depths, + pick_next_runnable, + protected_prefix_length, +) + +# A schedule document as written to Redis by the previous (eval-based) Scheduler version. +LEGACY_SCHEDULE = { + "schedule_name": "default_schedule", + "items": [ + { + "item_id": "a", + "command": "dev.samx.move(1.0, relative=False)", + "status": "completed", + "kind": "move", + "form_state": {"device_name": "samx", "value": 1.0, "units": "mm", "relative": False}, + "pause_on_failure": False, + } + ], + "is_running": False, + "notes": "", + "notifications": {"enabled": True, "target": "teams", "teams_scope": "x", "events": []}, + "guard": {"enabled": False, "device_name": None, "pause_below": None, "resume_above": None}, +} + +# Sample parameters for every registered action (the "describe" text must work with them). +SAMPLE_PARAMS = { + "scan": {"scan_name": "line_scan", "args": ["samx", -1, 1], "kwargs": {"steps": 10}}, + "move": {"device": "samx", "value": 1.5, "relative": True, "units": "mm"}, + "digital_twin": { + "config": { + "mo1_mode": "Monochromatic", + "energy": 9000, + "h_acc": 1e-3, + "v_acc": 1e-4, + "cm_stripe": "Rh", + "cm_pitch": 2e-3, + "mo1_xtal": "Si111", + "fm_stripe": "Pt", + "fm_rotx": 3e-3, + "ot_es1_trz": 100, + "es1man_trz": 1.0, + "smpl_to_xrd": 200.0, + } + }, + "auto_gain": { + "config": { + "element": "Cu", + "edge": "K", + "all_amplifiers": ["ic0", "ic1"], + "link_ic0_ic1": True, + "compensate_ring_current": True, + "prohibited_gains": {"ic0": [1e6]}, + } + }, + "absorber": {"action": "Open"}, + "ic_fill": { + "chamber": "IC1", + "gas1": "N2", + "conc1": 80.0, + "gas2": "Ar", + "conc2": 20.0, + "pressure": 1.5, + }, + "reffoil": {"foil": "Cu"}, + "temperature_controller": { + "config": { + "power_mode": "On", + "target_setpoint": 120, + "ramp_rate": 10, + "ramp_rate_unit": "K/min", + } + }, + "delay": {"seconds": 3725}, + "wait_for_parent": {}, +} + + +def _item(action="delay", wait=True, status=ItemStatus.PENDING, item_id=None, **params): + kwargs = {"item_id": item_id} if item_id else {} + return ScheduleItem( + action=action, + params=params or SAMPLE_PARAMS[action], + wait_for_completion=wait, + status=status, + **kwargs, + ) + + +# ---------------------------------------------------------------------------------------------- # +# models +# ---------------------------------------------------------------------------------------------- # +def test_legacy_schedule_is_detected_and_rejected(): + assert is_legacy_schedule(LEGACY_SCHEDULE) + with pytest.raises(Exception): + Schedule.model_validate(LEGACY_SCHEDULE) + + +def test_legacy_teams_target_maps_to_scilog(): + schedule = Schedule.model_validate({**LEGACY_SCHEDULE, "items": []}) + assert schedule.notifications.target == NotificationTarget.SCILOG + assert schedule.notifications.enabled + + +def test_legacy_notify_rpc_alias(): + schedule = Schedule.model_validate( + {"schedule_name": "s", "notifications": {"notify_rpc": False}} + ) + assert schedule.notifications.notify_device is False + + +def test_schedule_json_round_trip(): + schedule = Schedule( + schedule_name="s", items=[_item(action) for action in SAMPLE_PARAMS], notes="hello" + ) + data = json.loads(json.dumps(schedule.model_dump(mode="json"))) + assert Schedule.model_validate(data) == schedule + + +def test_reset_execution_and_definition(): + item = _item("move", status=ItemStatus.FAILED) + item.error, item.report, item.scan_number = "boom", "boom", 3 + item.reset_execution() + assert item.status == ItemStatus.PENDING + assert (item.error, item.report, item.scan_number) == (None, None, None) + assert set(item.definition()) == { + "action", + "params", + "wait_for_completion", + "pause_on_failure", + "pause_on_state", + } + + +# ---------------------------------------------------------------------------------------------- # +# actions +# ---------------------------------------------------------------------------------------------- # +def test_every_action_has_sample_params_and_description(): + assert set(ACTIONS) == set(SAMPLE_PARAMS) + for action, params in SAMPLE_PARAMS.items(): + json.dumps(params) + text = describe_item(_item(action), "x01da") + assert text and "Unknown action" not in text + + +def test_describe_unknown_action_does_not_raise(): + item = ScheduleItem(action="does_not_exist", params={"a": 1}) + assert "does_not_exist" in describe_item(item, None) + + +def test_run_action_unknown_raises(): + with pytest.raises(KeyError): + run_action(ExecutionContext(scans=None, dev=None), "does_not_exist", {}) + + +def test_run_scan_dispatches_to_scans(): + scans = mock.MagicMock() + ctx = ExecutionContext(scans=scans, dev=None) + run_action(ctx, "scan", SAMPLE_PARAMS["scan"]) + scans.line_scan.assert_called_once_with("samx", -1, 1, steps=10) + + +def test_run_scan_rejects_private_names(): + ctx = ExecutionContext(scans=mock.MagicMock(), dev=None) + with pytest.raises(ValueError): + run_action(ctx, "scan", {"scan_name": "_run", "args": [], "kwargs": {}}) + + +def test_run_move_dispatches_to_device(): + dev = {"samx": mock.MagicMock()} + run_action(ExecutionContext(scans=None, dev=dev), "move", SAMPLE_PARAMS["move"]) + dev["samx"].move.assert_called_once_with(1.5, relative=True) + + +def test_run_digital_twin_failure_raises(): + twin = mock.MagicMock() + twin.move_with_config.return_value = {"success": False, "failures": {"m": "x"}} + ctx = ExecutionContext(scans=None, dev=None, digital_twin=twin) + with pytest.raises(RuntimeError): + run_action(ctx, "digital_twin", SAMPLE_PARAMS["digital_twin"]) + + +def test_run_delay_is_interrupted_by_abort(): + ctx = ExecutionContext(scans=None, dev=None) + ctx.abort_event.set() + with pytest.raises(Exception): + run_action(ctx, "delay", {"seconds": 100}) + + +def test_control_action_cannot_be_run_directly(): + with pytest.raises(ValueError): + run_action(ExecutionContext(scans=None, dev=None), "wait_for_parent", {}) + + +def test_format_duration(): + assert format_duration(3725) == "1 h 02 min 05 s" + assert format_duration(150) == "2 min 30 s" + assert format_duration(5) == "5 s" + + +# ---------------------------------------------------------------------------------------------- # +# block structure +# ---------------------------------------------------------------------------------------------- # +def test_blocks_explicit_close(): + items = [ + _item("move"), + _item("scan", wait=False), + _item("temperature_controller"), + _item("delay"), + _item("wait_for_parent"), + _item("move"), + ] + assert block_parents(items) == [None, None, 1, 1, 1, None] + assert item_depths(items) == [0, 0, 1, 1, 1, 0] + assert block_roles(items) == [ + BlockRole.NONE, + BlockRole.PARENT, + BlockRole.CHILD, + BlockRole.CHILD, + BlockRole.LAST_CHILD, + BlockRole.NONE, + ] + + +def test_blocks_implicit_close_by_next_background_item_and_end(): + items = [_item("scan", wait=False), _item("delay"), _item("scan", wait=False), _item("delay")] + assert block_parents(items) == [None, 0, None, 2] + assert block_roles(items)[1] == BlockRole.LAST_CHILD + assert block_roles(items)[3] == BlockRole.LAST_CHILD + + +def test_background_item_without_children(): + items = [_item("scan", wait=False), _item("scan", wait=False)] + assert block_roles(items) == [BlockRole.PARENT_EMPTY, BlockRole.PARENT_EMPTY] + + +def test_wait_for_parent_outside_block_is_top_level(): + items = [_item("wait_for_parent"), _item("move")] + assert item_depths(items) == [0, 0] + + +# ---------------------------------------------------------------------------------------------- # +# next item / protected prefix +# ---------------------------------------------------------------------------------------------- # +def test_protected_prefix_with_several_running_items(): + items = [ + _item(status=ItemStatus.COMPLETED), + _item(status=ItemStatus.RUNNING), + _item(status=ItemStatus.RUNNING), + _item(), + ] + assert protected_prefix_length(items) == 3 + + +def test_pick_next_skips_completed_failed_and_active(): + items = [ + _item(status=ItemStatus.COMPLETED), + _item(status=ItemStatus.FAILED), + _item(status=ItemStatus.RUNNING, item_id="bg"), + _item(item_id="next"), + ] + assert pick_next_runnable(items, active_ids={"bg"}).item_id == "next" + + +def test_pick_next_retries_unfinished_once(): + items = [_item(status=ItemStatus.FAILED, item_id="f"), _item(item_id="p")] + assert pick_next_runnable(items, retry_unfinished=True).item_id == "f" + assert pick_next_runnable(items, retry_unfinished=True, attempted_ids={"f"}).item_id == "p" + + +def test_pick_next_respects_first_index_and_end(): + items = [_item(item_id="a"), _item(item_id="b")] + assert pick_next_runnable(items, first_index=1).item_id == "b" + assert pick_next_runnable([_item(status=ItemStatus.COMPLETED)]) is None + + +def test_schedule_from_document_never_raises(): + schedule, problem = schedule_from_document(LEGACY_SCHEDULE) + assert schedule is None and "old" in problem + schedule, problem = schedule_from_document({"items": "garbage"}) + assert schedule is None and problem + schedule, problem = schedule_from_document(None) + assert schedule is None and problem + schedule, problem = schedule_from_document({"schedule_name": "s", "items": []}) + assert problem is None and schedule.schedule_name == "s" diff --git a/tests/tests_bec_widgets/test_scheduler_state_monitor.py b/tests/tests_bec_widgets/test_scheduler_state_monitor.py new file mode 100644 index 0000000..f66ef30 --- /dev/null +++ b/tests/tests_bec_widgets/test_scheduler_state_monitor.py @@ -0,0 +1,117 @@ +"""Tests for the Scheduler's beamline-state monitor and the interlock hiding helper.""" + +# pylint: disable=missing-function-docstring,redefined-outer-name +from types import SimpleNamespace +from unittest import mock + +import pytest +from bec_lib.endpoints import MessageEndpoints +from qtpy.QtWidgets import QApplication + +from debye_bec.bec_widgets.widgets.scheduler.interlock_hiding import hide_scan_interlock_controls +from debye_bec.bec_widgets.widgets.scheduler.state_monitor import BeamlineStateMonitor + + +@pytest.fixture(scope="module") +def qapp(): + return QApplication.instance() or QApplication([]) + + +class FakeDispatcher: + def __init__(self): + self.slots: dict[str, object] = {} + + def connect_slot(self, slot, topic): + self.slots[topic.endpoint] = slot + + def disconnect_slot(self, slot, topic): + self.slots.pop(topic.endpoint, None) + + def publish_state(self, name, status): + self.slots[MessageEndpoints.beamline_state(name).endpoint]( + {"name": name, "status": status, "label": ""}, {} + ) + + +@pytest.fixture +def monitor(qapp): # pylint: disable=unused-argument + client = mock.MagicMock() + client.connector.get_last.return_value = None + dispatcher = FakeDispatcher() + mon = BeamlineStateMonitor(client, dispatcher) + yield mon, dispatcher + mon.cleanup() + + +def _set_available(dispatcher, *names): + dispatcher.slots[MessageEndpoints.available_beamline_states().endpoint]( + {"states": [SimpleNamespace(name=name) for name in names]}, {} + ) + + +def test_no_states_is_ok(monitor): + mon, _ = monitor + assert mon.is_ok() + assert mon.summary() == "No beamline states defined" + + +def test_new_state_is_unknown_until_reported(monitor): + mon, dispatcher = monitor + _set_available(dispatcher, "beam") + assert not mon.is_ok() + dispatcher.publish_state("beam", "valid") + assert mon.is_ok() + + +def test_warning_is_ok_invalid_trips_and_recovers(monitor): + mon, dispatcher = monitor + tripped, recovered = mock.MagicMock(), mock.MagicMock() + mon.tripped.connect(tripped) + mon.recovered.connect(recovered) + _set_available(dispatcher, "beam", "shutter") + tripped.assert_called_once() # new states are "unknown" until they report + dispatcher.publish_state("beam", "valid") + dispatcher.publish_state("shutter", "warning") + assert mon.is_ok() + recovered.assert_called_once() + + dispatcher.publish_state("shutter", "invalid") + assert not mon.is_ok() + assert mon.blocking_states() == ["shutter"] + assert tripped.call_count == 2 + + dispatcher.publish_state("shutter", "valid") + assert mon.is_ok() + assert recovered.call_count == 2 + + +def test_removed_state_is_unsubscribed(monitor): + mon, dispatcher = monitor + _set_available(dispatcher, "beam") + dispatcher.publish_state("beam", "invalid") + assert not mon.is_ok() + _set_available(dispatcher) + assert mon.is_ok() + assert MessageEndpoints.beamline_state("beam").endpoint not in dispatcher.slots + + +def test_wait_until_ok_aborts(monitor): + mon, dispatcher = monitor + _set_available(dispatcher, "beam") + assert mon.wait_until_ok(should_abort=lambda: True) is False + + +def test_hide_scan_interlock_controls(): + action = mock.MagicMock() + pill = SimpleNamespace(_interlock_button=mock.MagicMock(), _interlock_warning_checkbox=None) + manager = SimpleNamespace( + _toolbar=SimpleNamespace( + components=SimpleNamespace(get_action=lambda name: SimpleNamespace(action=action)) + ), + _state_pills={"beam": pill}, + ) + hide_scan_interlock_controls(manager) # must not raise on the missing checkbox + action.setVisible.assert_called_with(False) + pill._interlock_button.setVisible.assert_called_once_with( + False + ) # pylint: disable=protected-access