Skip to content

Commit 3ca78f8

Browse files
committed
Split out logging of tasks errors
This is to reduce the complexity of the main loop and make it easier to understand what is happening when a task ends with an exception. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 1d4f451 commit 3ca78f8

File tree

1 file changed

+54
-40
lines changed

1 file changed

+54
-40
lines changed

src/frequenz/sdk/actor/_resampling.py

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -130,49 +130,11 @@ async def _run(self) -> None:
130130

131131
if subscriptions_task in done:
132132
tasks_to_cancel.remove(subscriptions_task)
133-
try:
134-
subscriptions_task.result()
135-
# pylint: disable-next=broad-except
136-
except (Exception, asyncio.CancelledError):
137-
_logger.exception(
138-
"The subscriptions task ended with an exception, restarting..."
139-
)
140-
else:
141-
_logger.error(
142-
"The subscriptions task ended unexpectedly, restarting..."
143-
)
133+
self._log_subscriptions_task_error(subscriptions_task)
144134

145135
if resampling_task in done:
146136
tasks_to_cancel.remove(resampling_task)
147-
# The resampler shouldn't be cancelled or end without an exception
148-
try:
149-
resampling_task.result()
150-
except ResamplingError as error:
151-
for source, source_error in error.exceptions.items():
152-
_logger.error(
153-
"Error resampling source %s, removing source...", source
154-
)
155-
removed = self._resampler.remove_timeseries(source)
156-
if not removed:
157-
_logger.error(
158-
"Got an exception from an unknown source: "
159-
"source=%r, exception=%r",
160-
source,
161-
source_error,
162-
)
163-
# pylint: disable-next=broad-except
164-
except (Exception, asyncio.CancelledError):
165-
# We don't know what to do with something other than
166-
# ResamplingError, so we log it, restart, and hope for the best.
167-
_logger.exception(
168-
"The resample() function got an unexpected error, restarting..."
169-
)
170-
else:
171-
# The resample function should not end normally, so we log it,
172-
# restart, and hope for the best.
173-
_logger.error(
174-
"The resample() function ended without an exception, restarting..."
175-
)
137+
self._log_resampling_task_error(resampling_task)
176138

177139
finally:
178140
await asyncio.gather(*[cancel_and_await(t) for t in tasks_to_cancel])
@@ -187,3 +149,55 @@ async def _run(self) -> None:
187149
# state would be mostly the same as not really leaving the run()
188150
# method and just swallow any exception, which doesn't look super
189151
# smart.
152+
153+
def _log_subscriptions_task_error(
154+
self, subscriptions_task: asyncio.Task[None]
155+
) -> None:
156+
"""Log an error from a stopped subscriptions task.
157+
158+
Args:
159+
subscriptions_task: The subscriptions task.
160+
"""
161+
try:
162+
subscriptions_task.result()
163+
# pylint: disable-next=broad-except
164+
except (Exception, asyncio.CancelledError):
165+
_logger.exception(
166+
"The subscriptions task ended with an exception, restarting..."
167+
)
168+
else:
169+
_logger.error("The subscriptions task ended unexpectedly, restarting...")
170+
171+
def _log_resampling_task_error(self, resampling_task: asyncio.Task[None]) -> None:
172+
"""Log an error from a stopped resampling task.
173+
174+
Args:
175+
resampling_task: The resampling task.
176+
"""
177+
# The resampler shouldn't be cancelled or end without an exception
178+
try:
179+
resampling_task.result()
180+
except ResamplingError as error:
181+
for source, source_error in error.exceptions.items():
182+
_logger.error("Error resampling source %s, removing source...", source)
183+
removed = self._resampler.remove_timeseries(source)
184+
if not removed:
185+
_logger.error(
186+
"Got an exception from an unknown source: "
187+
"source=%r, exception=%r",
188+
source,
189+
source_error,
190+
)
191+
# pylint: disable-next=broad-except
192+
except (Exception, asyncio.CancelledError):
193+
# We don't know what to do with something other than
194+
# ResamplingError, so we log it, restart, and hope for the best.
195+
_logger.exception(
196+
"The resample() function got an unexpected error, restarting..."
197+
)
198+
else:
199+
# The resample function should not end normally, so we log it,
200+
# restart, and hope for the best.
201+
_logger.error(
202+
"The resample() function ended without an exception, restarting..."
203+
)

0 commit comments

Comments
 (0)