-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsaga.py
More file actions
196 lines (161 loc) · 7.45 KB
/
saga.py
File metadata and controls
196 lines (161 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import traceback
from dataclasses import dataclass, field
from inspect import isawaitable, signature
from typing import Any, Callable, NewType, Union
TracebackStr = NewType('TracebackStr', str)
class SagaError(Exception):
def __init__(
self,
steps: list['Action'],
failed_step_index: int,
action_exception: Exception,
action_traceback: TracebackStr,
compensation_exception_tracebacks: dict[int, tuple[Exception, TracebackStr]],
):
self.steps = steps
self.failed_step_index = failed_step_index
self.action_exception = action_exception
self.action_traceback = action_traceback
self.compensation_exception_tracebacks = compensation_exception_tracebacks
super().__init__(self.build_header_message())
def __str__(self):
header_msg = (
f'Saga execution failed at step {self.failed_step_index}: '
f'{type(self.action_exception).__name__}: {self.action_exception}'
)
if self.compensation_exception_tracebacks:
header_msg += f' (and {len(self.compensation_exception_tracebacks)} compensation step(s) failed)'
registered_steps_msg = 'Registered Steps:\n' + '\n'.join(
f' [{i}] action: {step.action_call_repr}; compensation: {step.compensation_call_repr}'
for i, step in enumerate(self.steps)
)
action_error_msg = (
f'Transaction failed at step index {self.failed_step_index}: '
f'An unexpected {type(self.action_exception).__name__} occurred, triggering the compensation process.'
f'\n{self.format_traceback_indentation(self.action_traceback, 2)}'
)
compensation_error_msgs = ''
if any(self.compensation_exception_tracebacks.values()):
compensation_error_msgs = 'Compensations encountered errors:\n' + '\n'.join(
[
f' - (step index {step}): Compensation failed due to a {type(exc).__name__}: {exc}'
f'\n{self.format_traceback_indentation(traceback_str, 6)}'
for step, (exc, traceback_str) in self.compensation_exception_tracebacks.items()
]
)
return '\n\n'.join([header_msg, registered_steps_msg, action_error_msg, compensation_error_msgs]).strip()
def build_header_message(self) -> str:
msg = (
f'Saga execution failed at step {self.failed_step_index}: '
f'{type(self.action_exception).__name__}: {self.action_exception}'
)
if self.compensation_exception_tracebacks:
msg += f' (and {len(self.compensation_exception_tracebacks)} compensation step(s) failed)'
return msg
def format_traceback_indentation(self, traceback_str: str, indent: int = 2) -> str:
if '\n' in traceback_str:
return '\n'.join([' ' * indent + '╎' + line for line in traceback_str.splitlines()])
else:
return traceback_str
@dataclass
class Action:
action: Callable[..., Any]
compensation: Callable[..., Any]
_result: Any = field(init=False, default=None)
_action_call_repr: str = field(init=False)
_compensation_call_repr: str = field(init=False)
_compensation_args: Union[tuple[Any], list[Any]] = field(init=False, default_factory=list)
def __post_init__(self):
self._action_call_repr = self._format_signature_preview(self.action)
self._compensation_call_repr = self._format_signature_preview(self.compensation)
def _format_signature_preview(self, func: Callable) -> str:
func_name = func.__name__
try:
sig = signature(func)
parts = []
for name, param in sig.parameters.items():
if param.default is not param.empty:
parts.append(f'{name}={param.default!r}')
else:
parts.append(f'{name}=<?>')
return f'{func_name}({", ".join(parts)})'
except Exception:
return f'{func_name}(...)'
def _format_function_call(self, func: Callable, *args) -> str:
func_name = func.__name__
try:
sig = signature(func)
if not sig.parameters:
return f'{func_name}()'
if not args:
return self._format_signature_preview(func)
bound = sig.bind(*args)
bound.apply_defaults()
arg_str = ', '.join(f'{k}={v!r}' for k, v in bound.arguments.items())
return f'{func_name}({arg_str})'
except Exception:
return f'{func_name}(...)'
@property
def action_call_repr(self) -> str:
return str(self._action_call_repr)
@property
def compensation_call_repr(self) -> str:
return str(self._compensation_call_repr)
@property
def result(self) -> Any:
return self._result
async def act(self, *args) -> Any:
self._action_call_repr = self._format_function_call(self.action, *args)
result = self.action(*(args if self.action.__code__.co_varnames else []))
if isawaitable(result):
result = await result
return result
async def compensate(self) -> Any:
args = self._compensation_args if self.compensation.__code__.co_varnames else []
self._compensation_call_repr = self._format_function_call(self.compensation, *args)
result = self.compensation(*args)
if isawaitable(result):
result = await result
return result
@dataclass
class Saga:
steps: list[Action]
async def execute(self) -> 'Saga':
args = []
for index, action in enumerate(self.steps):
if isinstance(action, Action):
try:
actioned_result = await action.act(*args)
if actioned_result is None:
args = []
elif isinstance(actioned_result, (list, tuple)):
args = actioned_result
else:
args = (actioned_result,)
action._compensation_args = args
action._result = actioned_result
except Exception as exc:
action_traceback_str = TracebackStr(traceback.format_exc())
compensation_exceptions = await self._run_compensations(index)
raise SagaError(self.steps, index, exc, action_traceback_str, compensation_exceptions)
return self
async def _run_compensations(self, last_action_index: int) -> dict[int, tuple[Exception, TracebackStr]]:
compensation_exceptions = {}
for compensation_index in range(last_action_index - 1, -1, -1):
try:
action = self.steps[compensation_index]
await action.compensate()
except Exception as exc:
_, _, traceback_str = traceback.format_exc().partition(
'During handling of the above exception, another exception occurred:\n\n'
)
compensation_exceptions[compensation_index] = (exc, TracebackStr(traceback_str))
return compensation_exceptions
class OrchestrationBuilder:
def __init__(self):
self.steps: list[Action] = []
def add_step(self, action: Callable[..., Any], compensation: Callable[..., Any]) -> 'OrchestrationBuilder':
self.steps.append(Action(action, compensation))
return self
async def execute(self) -> Saga:
return await Saga(self.steps).execute()