fix: improve error handling in wait_for_status and _move_and_finish

This commit is contained in:
2024-07-29 09:49:25 +02:00
parent ce3718dcf6
commit d0c81d20e7
2 changed files with 19 additions and 7 deletions

View File

@@ -30,6 +30,7 @@ from ophyd import (
)
from ophyd.utils import LimitError
from ophyd_devices.utils import bec_scaninfo_mixin, bec_utils
from ophyd_devices.utils.errors import DeviceStopError, DeviceTimeoutError
logger = bec_logger.logger
@@ -410,7 +411,6 @@ class Mo1Bragg(Device, PositionerBase):
status (DeviceStatus) : status object to set the status of the motion
update_frequency (float): Optional, frequency to update the current position of the motion, defaults to 0.1s
"""
success = True
try:
# Set the target position on IOC
move_cpt.put(target_pos)
@@ -419,11 +419,10 @@ class Mo1Bragg(Device, PositionerBase):
time.sleep(0.5)
while self.motor_is_moving.get() == 0:
if self.stopped:
success = False
break
raise DeviceStopError(f"Device {self.name} was stopped")
time.sleep(update_frequency)
# pylint: disable=protected-access
status._finished(success=success)
status.set_finished()
# pylint: disable=broad-except
except Exception as exc:
content = traceback.format_exc()
@@ -799,7 +798,7 @@ class Mo1Bragg(Device, PositionerBase):
check_stopped: bool = False,
interval: float = 0.05,
all_signals: bool = False,
exception_on_timeout: Exception = TimeoutError("Timeout while waiting for signals"),
exception_on_timeout: Exception = None,
) -> DeviceStatus:
"""Wrapper around wait_for_signals to be started in thread and attach a DeviceStatus object.
This allows BEC to perform actinos in parallel and not be blocked by method calls on a device.
@@ -816,6 +815,10 @@ class Mo1Bragg(Device, PositionerBase):
Returns:
DeviceStatus: DeviceStatus object that resolves either to set_finished or set_exception
"""
if exception_on_timeout is None:
exception_on_timeout = DeviceTimeoutError(
f"Timeout error for {self.name} while waiting for signals {signal_conditions}"
)
status = DeviceStatus(device=self)
@@ -826,7 +829,7 @@ class Mo1Bragg(Device, PositionerBase):
check_stopped: bool,
interval: float,
all_signals: bool,
exception_on_timeout: Exception = TimeoutError("Timeout while waiting for signals"),
exception_on_timeout: Exception = None,
):
"""Convenient wrapper around wait_for_signals to set status based on the result.
@@ -847,9 +850,17 @@ class Mo1Bragg(Device, PositionerBase):
# pylint: disable=protected-access
status.set_finished()
else:
status.set_exception(exception_on_timeout)
if self.parent.stopped:
# INFO This will execute a callback to the parent device.stop() method
status.set_exception(exc=DeviceStopError(f"{self.name} was stopped"))
else:
# INFO This will execute a callback to the parent device.stop() method
status.set_exception(exc=exception_on_timeout)
# pylint: disable=broad-except
except Exception as exc:
content = traceback.format_exc()
logger.warning(f"Error in wait_for_signals in {self.name}; Traceback: {content}")
# INFO This will execute a callback to the parent device.stop() method
status.set_exception(exc=exc)
thread = threading.Thread(

View File

@@ -1,3 +1,4 @@
# pylint: skip-file
from unittest import mock
from bec_lib.messages import DeviceInstructionMessage