Skip to content

Commit 9af3384

Browse files
author
Sergio García Prado
committed
ISSUE #53
* Simplify `SagaStepStatus`.
1 parent 45e14e9 commit 9af3384

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

packages/core/minos-microservice-saga/minos/saga/executions/status.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def from_raw(cls, raw: Union[str, SagaStatus]) -> SagaStatus:
2929
if isinstance(raw, cls):
3030
return raw
3131

32-
# noinspection PyTypeChecker
32+
# noinspection PyTypeChecker,PyUnresolvedReferences
3333
return next(obj for obj in cls if obj.value == raw)
3434

3535
@property
@@ -41,7 +41,7 @@ def raw(self) -> str:
4141
return self.value
4242

4343

44-
class SagaStepStatus(Enum):
44+
class SagaStepStatus(str, Enum):
4545
"""Saga Step Status class."""
4646

4747
Created = "created"
@@ -50,12 +50,11 @@ class SagaStepStatus(Enum):
5050
ErroredOnExecute = "errored-on-execute"
5151
PausedByOnExecute = "paused-by-on-execute"
5252
ErroredByOnExecute = "errored-by-on-execute"
53-
RunningOnFailure = "running-on-failure"
54-
PausedOnFailure = "paused-on-failure"
55-
ErroredOnFailure = "errored-on-failure"
5653
RunningOnSuccess = "running-on-success"
54+
FinishedOnSuccess = "finished-on-success"
5755
ErroredOnSuccess = "errored-on-success"
5856
RunningOnError = "running-on-error"
57+
FinishedOnError = "finished-on-error"
5958
ErroredOnError = "errored-on-error"
6059
Finished = "finished"
6160

@@ -69,6 +68,7 @@ def from_raw(cls, raw: Union[str, SagaStepStatus]) -> SagaStepStatus:
6968
if isinstance(raw, cls):
7069
return raw
7170

71+
# noinspection PyTypeChecker,PyUnresolvedReferences
7272
return next(obj for obj in cls if obj.value == raw)
7373

7474
@property

packages/core/minos-microservice-saga/minos/saga/executions/steps/conditional.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,11 @@ async def execute(self, context: SagaContext, *args, **kwargs) -> SagaContext:
8181
if self.status == SagaStepStatus.Created:
8282
self.inner = await self._create_inner(context, *args, **kwargs)
8383

84-
self.status = SagaStepStatus.RunningOnExecute
85-
8684
if self.inner is not None:
8785
context = await self._execute_inner(context, *args, **kwargs)
8886

8987
self.status = SagaStepStatus.Finished
88+
9089
return context
9190

9291
async def _create_inner(
@@ -96,6 +95,8 @@ async def _create_inner(
9695
SagaExecution,
9796
)
9897

98+
self.status = SagaStepStatus.RunningOnExecute
99+
99100
executor = Executor(execution_uuid=execution_uuid)
100101
self.related_services.add(get_service_name())
101102

@@ -108,12 +109,15 @@ async def _create_inner(
108109
if definition is None and self.definition.else_then_alternative is not None:
109110
definition = self.definition.else_then_alternative.saga
110111

111-
if definition is None:
112-
return None
112+
execution = None
113+
if definition is not None:
114+
execution = SagaExecution.from_definition(
115+
definition, context=context, user=user, uuid=execution_uuid, *args, **kwargs
116+
)
113117

114-
return SagaExecution.from_definition(
115-
definition, context=context, user=user, uuid=execution_uuid, *args, **kwargs
116-
)
118+
self.status = SagaStepStatus.FinishedOnExecute
119+
120+
return execution
117121

118122
async def _execute_inner(
119123
self, context: SagaContext, user: Optional[UUID] = None, execution_uuid: Optional[UUID] = None, *args, **kwargs
@@ -125,6 +129,8 @@ async def _execute_inner(
125129
execution.user = user
126130
execution.context = context
127131

132+
self.status = SagaStepStatus.RunningOnSuccess
133+
128134
try:
129135
with suppress(SagaExecutionAlreadyExecutedException):
130136
await self.inner.execute(*args, **(kwargs | {"autocommit": False}))
@@ -135,6 +141,8 @@ async def _execute_inner(
135141
self.status = SagaStepStatus.ErroredByOnExecute
136142
raise exc
137143

144+
self.status = SagaStepStatus.FinishedOnSuccess
145+
138146
return execution.context
139147

140148
async def rollback(self, context: SagaContext, *args, **kwargs) -> SagaContext:

packages/core/minos-microservice-saga/minos/saga/executions/steps/local.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ async def execute(self, context: SagaContext, *args, **kwargs) -> SagaContext:
3939
if self.status != SagaStepStatus.Created:
4040
return context
4141

42+
context = await self._execute_on_execute(context, *args, **kwargs)
43+
44+
self.status = SagaStepStatus.Finished
45+
return context
46+
47+
async def _execute_on_execute(self, context: SagaContext, *args, **kwargs) -> SagaContext:
48+
4249
self.status = SagaStepStatus.RunningOnExecute
4350

4451
executor = LocalExecutor(*args, **kwargs)
@@ -51,7 +58,8 @@ async def execute(self, context: SagaContext, *args, **kwargs) -> SagaContext:
5158
self.status = SagaStepStatus.ErroredOnExecute
5259
raise exc
5360

54-
self.status = SagaStepStatus.Finished
61+
self.status = SagaStepStatus.FinishedOnExecute
62+
5563
return context
5664

5765
async def rollback(self, context: SagaContext, *args, **kwargs) -> SagaContext:

packages/core/minos-microservice-saga/minos/saga/executions/steps/remote.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ async def _execute_on_success(self, context: SagaContext, response: SagaResponse
101101
await self.rollback(context, *args, **kwargs)
102102
raise exc
103103

104+
self.status = SagaStepStatus.FinishedOnSuccess
105+
104106
return context
105107

106108
async def _execute_on_error(self, context: SagaContext, response: SagaResponse, *args, **kwargs) -> SagaContext:
@@ -115,6 +117,8 @@ async def _execute_on_error(self, context: SagaContext, response: SagaResponse,
115117
await self.rollback(context, *args, **kwargs)
116118
raise exc
117119

120+
self.status = SagaStepStatus.FinishedOnError
121+
118122
return context
119123

120124
async def rollback(self, context: SagaContext, *args, **kwargs) -> SagaContext:

0 commit comments

Comments
 (0)