Skip to content

Commit 2eee569

Browse files
committed
ENH: Allow not raising errors from exception events in event handler
skipci
1 parent 9caa15f commit 2eee569

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

octue/cloud/events/handler.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class AbstractEventHandler:
3737
:param str|None exclude_logs_containing: if provided, skip handling log messages containing this string
3838
:param bool only_handle_result: if `True`, skip handling non-result events and only handle the "result" event when received (turning this on speeds up event handling)
3939
:param bool validate_events: if `True`, validate events before attempting to handle them (turning this off speeds up event handling)
40+
:param bool raise_errors: if `True`, raise any exceptions received; otherwise, just log them (just logging them allows a partial result event to be received afterwards and handled)
4041
:return None:
4142
"""
4243

@@ -50,6 +51,7 @@ def __init__(
5051
exclude_logs_containing=None,
5152
only_handle_result=False,
5253
validate_events=True,
54+
raise_errors=True,
5355
):
5456
self.handle_monitor_message = handle_monitor_message
5557
self.record_events = record_events
@@ -58,6 +60,7 @@ def __init__(
5860
self.exclude_logs_containing = exclude_logs_containing
5961
self.only_handle_result = only_handle_result
6062
self.validate_events = validate_events
63+
self.raise_errors = raise_errors
6164

6265
self.handled_events = []
6366
self._start_time = None
@@ -221,7 +224,7 @@ def _handle_log_message(self, event, attributes):
221224
logger.handle(record)
222225

223226
def _handle_exception(self, event, attributes):
224-
"""Raise the exception from the child.
227+
"""Raise or log the exception from the child.
225228
226229
:param dict event:
227230
:param dict attributes: the event's attributes
@@ -243,7 +246,12 @@ def _handle_exception(self, event, attributes):
243246
except KeyError:
244247
exception_type = type(event["exception_type"], (Exception,), {})
245248

246-
raise exception_type(exception_message)
249+
error = exception_type(exception_message)
250+
251+
if self.raise_errors:
252+
raise error
253+
254+
logger.error("", exc_info=error)
247255

248256
def _handle_result(self, event, attributes):
249257
"""Extract any output values and output manifest from the result, deserialising the manifest if present.

octue/cloud/events/replayer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class EventReplayer(AbstractEventHandler):
1919
:param str|None exclude_logs_containing: if provided, skip handling log messages containing this string
2020
:param bool only_handle_result: if `True`, skip non-result events and only handle the "result" event if present (turning this on speeds up event handling)
2121
:param bool validate_events: if `True`, validate events before attempting to handle them (this is off by default to speed up event handling)
22+
:param bool raise_errors: if `True`, raise any exceptions received; otherwise, just log them (just logging them allows a partial result event to be received afterwards and handled)
2223
:return None:
2324
"""
2425

@@ -32,6 +33,7 @@ def __init__(
3233
exclude_logs_containing=None,
3334
only_handle_result=False,
3435
validate_events=False,
36+
raise_errors=True,
3537
):
3638
event_handlers = event_handlers or {
3739
"question": self._handle_question,
@@ -52,6 +54,7 @@ def __init__(
5254
exclude_logs_containing=exclude_logs_containing,
5355
only_handle_result=only_handle_result,
5456
validate_events=validate_events,
57+
raise_errors=raise_errors,
5558
)
5659

5760
def handle_events(self, events):

octue/cloud/pub_sub/events.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class GoogleCloudPubSubEventHandler(AbstractEventHandler):
4646
:param str|None exclude_logs_containing: if provided, skip handling log messages containing this string
4747
:param bool only_handle_result: if `True`, skip non-result events and only handle the "result" event if present (turning this on speeds up event handling)
4848
:param bool validate_events: if `True`, validate events before attempting to handle them (turn this off to speed up event handling at risk of failure if an invalid event is received)
49+
:param bool raise_errors: if `True`, raise any exceptions received; otherwise, just log them (just logging them allows a partial result event to be received afterwards and handled)
4950
:return None:
5051
"""
5152

@@ -60,6 +61,7 @@ def __init__(
6061
exclude_logs_containing=None,
6162
only_handle_result=False,
6263
validate_events=True,
64+
raise_errors=True,
6365
):
6466
self.subscription = subscription
6567

@@ -72,6 +74,7 @@ def __init__(
7274
exclude_logs_containing=exclude_logs_containing,
7375
only_handle_result=only_handle_result,
7476
validate_events=validate_events,
77+
raise_errors=raise_errors,
7578
)
7679

7780
self._heartbeat_checker = None

0 commit comments

Comments
 (0)