Skip to content

Commit a2a7808

Browse files
authored
Log exceptions in failed scan tasks (#282)
1 parent 68cddc8 commit a2a7808

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

src/fastcs/attributes/attr_r.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ async def update_attribute():
110110
try:
111111
self.log_event("Update attribute", topic=self)
112112
await update_callback(self)
113-
except Exception as e:
114-
logger.opt(exception=e).error("Update loop failed", attribute=self)
113+
except Exception:
114+
logger.error("Attribute update loop stopped", attribute=self)
115115
raise
116116

117117
return update_attribute

src/fastcs/attributes/attr_w.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ async def put(self, setpoint: DType_T, sync_setpoint: bool = False) -> None:
6868
"Sync setpoint failed", attribute=self, setpoint=setpoint
6969
)
7070

71+
self.log_event("Put complete", setpoint=setpoint, attribute=self)
72+
7173
async def _call_sync_setpoint_callbacks(self, setpoint: DType_T) -> None:
7274
if self._sync_setpoint_callbacks:
7375
await asyncio.gather(

src/fastcs/control_system.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from IPython.terminal.embed import InteractiveShellEmbed
88

99
from fastcs.controllers import BaseController, Controller
10-
from fastcs.exceptions import FastCSError
1110
from fastcs.logging import bind_logger
1211
from fastcs.methods import Command, Scan, ScanCallback
1312
from fastcs.tracer import Tracer
@@ -63,11 +62,8 @@ async def _start_scan_tasks(self):
6362
def _scan_done(self, task: asyncio.Task):
6463
try:
6564
task.result()
66-
except Exception as e:
67-
raise FastCSError(
68-
"Exception raised in scan method of "
69-
f"{self._controller.__class__.__name__}"
70-
) from e
65+
except Exception:
66+
logger.exception("Exception raised in scan task")
7167

7268
def _stop_scan_tasks(self):
7369
for task in self._scan_tasks:

src/fastcs/methods/scan.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
from types import MethodType
33

44
from fastcs.controllers import BaseController
5+
from fastcs.logging import bind_logger
56
from fastcs.methods.method import Controller_T, Method
67

8+
logger = bind_logger(logger_name=__name__)
9+
710
UnboundScanCallback = Callable[[Controller_T], Coroutine[None, None, None]]
811
"""A Scan callback that is unbound and must be called with a `Controller` instance"""
912
ScanCallback = Callable[[], Coroutine[None, None, None]]
@@ -36,6 +39,17 @@ def _validate(self, fn: ScanCallback) -> None:
3639
async def __call__(self):
3740
return await self._fn()
3841

42+
@property
43+
def fn(self):
44+
async def scan():
45+
try:
46+
return await self._fn()
47+
except Exception:
48+
logger.error("Scan update loop stopped", fn=self._fn)
49+
raise
50+
51+
return scan
52+
3953

4054
class UnboundScan(Method[Controller_T]):
4155
"""A wrapper of an unbound `Controller` method to be bound into a `Scan`.

tests/test_control_system.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from fastcs.control_system import FastCS, build_controller_api
88
from fastcs.controllers import Controller
99
from fastcs.datatypes import Int
10-
from fastcs.exceptions import FastCSError
1110
from fastcs.methods import Command, command, scan
1211
from fastcs.util import ONCE
1312

@@ -152,8 +151,6 @@ async def raise_exception(self):
152151
task = asyncio.create_task(fastcs.serve(interactive=False))
153152
# This allows scan time to run
154153
await asyncio.sleep(0.2)
155-
# _scan_done should raise an exception
156-
assert isinstance(exception_info["exception"], FastCSError)
157154
for task in fastcs._scan_tasks:
158155
internal_exception = task.exception()
159156
assert internal_exception

0 commit comments

Comments
 (0)