-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoney_transfer_workflow.py
161 lines (133 loc) · 5.85 KB
/
money_transfer_workflow.py
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
from dataclasses import dataclass
from iwf.command_results import CommandResults
from iwf.communication import Communication
from iwf.iwf_api.models import RetryPolicy
from iwf.persistence import Persistence
from iwf.state_decision import StateDecision
from iwf.state_schema import StateSchema
from iwf.workflow import ObjectWorkflow
from iwf.workflow_context import WorkflowContext
from iwf.workflow_state import WorkflowState
from iwf.workflow_state_options import WorkflowStateOptions
@dataclass
class TransferRequest:
from_account: str
to_account: str
amount: int
notes: str
class VerifyState(WorkflowState[TransferRequest]):
def execute(
self,
ctx: WorkflowContext,
request: TransferRequest,
command_results: CommandResults,
persistence: Persistence,
communication: Communication,
) -> StateDecision:
print(f"API to check balance for account {request.from_account} for amount{request.amount}")
has_sufficient_funds = True
if not has_sufficient_funds:
return StateDecision.force_fail_workflow("insufficient funds")
return StateDecision.single_next_state(CreateDebitMemoState, request)
class CreateDebitMemoState(WorkflowState[TransferRequest]):
def execute(
self,
ctx: WorkflowContext,
request: TransferRequest,
command_results: CommandResults,
persistence: Persistence,
communication: Communication,
) -> StateDecision:
print(f"API to create debit memo for account {request.from_account} for amount{request.amount} with notes{request.notes}")
# uncomment this to test error
# raise Exception("test error")
return StateDecision.single_next_state(DebitState, request)
def get_state_options(self) -> WorkflowStateOptions:
return WorkflowStateOptions(
execute_failure_handling_state=CompensateState,
execute_api_retry_policy=RetryPolicy(
maximum_attempts_duration_seconds=3600,
# replace with this to try a shorter retry
# maximum_attempts_duration_seconds=3,
)
)
class DebitState(WorkflowState[TransferRequest]):
def execute(
self,
ctx: WorkflowContext,
request: TransferRequest,
command_results: CommandResults,
persistence: Persistence,
communication: Communication,
) -> StateDecision:
print(f"API to debit account {request.from_account} for amount{request.amount}")
return StateDecision.single_next_state(CreateCreditMemoState, request)
def get_state_options(self) -> WorkflowStateOptions:
return WorkflowStateOptions(
execute_failure_handling_state=CompensateState,
execute_api_retry_policy=RetryPolicy(
maximum_attempts_duration_seconds=3600,
)
)
class CreateCreditMemoState(WorkflowState[TransferRequest]):
def execute(
self,
ctx: WorkflowContext,
request: TransferRequest,
command_results: CommandResults,
persistence: Persistence,
communication: Communication,
) -> StateDecision:
print(f"API to create credit memo for account {request.to_account} for amount{request.amount} with notes{request.notes}")
return StateDecision.single_next_state(CreditState, request)
def get_state_options(self) -> WorkflowStateOptions:
return WorkflowStateOptions(
execute_failure_handling_state=CompensateState,
execute_api_retry_policy=RetryPolicy(
maximum_attempts_duration_seconds=3600,
)
)
class CreditState(WorkflowState[TransferRequest]):
def execute(
self,
ctx: WorkflowContext,
request: TransferRequest,
command_results: CommandResults,
persistence: Persistence,
communication: Communication,
) -> StateDecision:
print(f"API to credit account {request.to_account} for amount{request.amount}")
return StateDecision.graceful_complete_workflow(f"transfer is done from account{request.from_account} "
f"to account{request.to_account} for amount{request.amount}")
def get_state_options(self) -> WorkflowStateOptions:
return WorkflowStateOptions(
execute_failure_handling_state=CompensateState,
execute_api_retry_policy=RetryPolicy(
maximum_attempts_duration_seconds=3600,
)
)
class CompensateState(WorkflowState[TransferRequest]):
def execute(
self,
ctx: WorkflowContext,
request: TransferRequest,
command_results: CommandResults,
persistence: Persistence,
communication: Communication,
) -> StateDecision:
# NOTE: to improve, we can use iWF data attributes to track whether each step has been attempted to execute
# and check a flag to see if we should undo it or not
print(f"API to undo credit account {request.to_account} for amount{request.amount}")
print(f"API to undo create credit memo account {request.to_account} for amount{request.amount}")
print(f"API to undo debit account {request.from_account} for amount{request.amount}")
print(f"API to undo create debit memo {request.from_account} for amount{request.amount}")
return StateDecision.force_fail_workflow("fail to transfer")
class MoneyTransferWorkflow(ObjectWorkflow):
def get_workflow_states(self) -> StateSchema:
return StateSchema.with_starting_state(
VerifyState(),
CreateDebitMemoState(),
DebitState(),
CreateCreditMemoState(),
CreditState(),
CompensateState())