|
| 1 | +from dataclasses import dataclass |
| 2 | + |
| 3 | +from iwf.command_results import CommandResults |
| 4 | +from iwf.communication import Communication |
| 5 | +from iwf.iwf_api.models import RetryPolicy |
| 6 | +from iwf.persistence import Persistence |
| 7 | +from iwf.state_decision import StateDecision |
| 8 | +from iwf.state_schema import StateSchema |
| 9 | +from iwf.workflow import ObjectWorkflow |
| 10 | +from iwf.workflow_context import WorkflowContext |
| 11 | +from iwf.workflow_state import WorkflowState |
| 12 | +from iwf.workflow_state_options import WorkflowStateOptions |
| 13 | + |
| 14 | + |
| 15 | +@dataclass |
| 16 | +class TransferRequest: |
| 17 | + from_account: str |
| 18 | + to_account: str |
| 19 | + amount: int |
| 20 | + notes: str |
| 21 | + |
| 22 | + |
| 23 | +class VerifyState(WorkflowState[TransferRequest]): |
| 24 | + def execute( |
| 25 | + self, |
| 26 | + ctx: WorkflowContext, |
| 27 | + request: TransferRequest, |
| 28 | + command_results: CommandResults, |
| 29 | + persistence: Persistence, |
| 30 | + communication: Communication, |
| 31 | + ) -> StateDecision: |
| 32 | + print(f"API to check balance for account {request.from_account} for amount{request.amount}") |
| 33 | + |
| 34 | + has_sufficient_funds = True |
| 35 | + if not has_sufficient_funds: |
| 36 | + return StateDecision.force_fail_workflow("insufficient funds") |
| 37 | + |
| 38 | + return StateDecision.single_next_state(CreateDebitMemoState, request) |
| 39 | + |
| 40 | + |
| 41 | +class CreateDebitMemoState(WorkflowState[TransferRequest]): |
| 42 | + def execute( |
| 43 | + self, |
| 44 | + ctx: WorkflowContext, |
| 45 | + request: TransferRequest, |
| 46 | + command_results: CommandResults, |
| 47 | + persistence: Persistence, |
| 48 | + communication: Communication, |
| 49 | + ) -> StateDecision: |
| 50 | + print(f"API to create debit memo for account {request.from_account} for amount{request.amount} with notes{request.notes}") |
| 51 | + |
| 52 | + return StateDecision.single_next_state(DebitState, request) |
| 53 | + |
| 54 | + def get_state_options(self) -> WorkflowStateOptions: |
| 55 | + return WorkflowStateOptions( |
| 56 | + execute_failure_handling_state=CompensateState, |
| 57 | + execute_api_retry_policy=RetryPolicy( |
| 58 | + maximum_attempts_duration_seconds=3600, |
| 59 | + ) |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +class DebitState(WorkflowState[TransferRequest]): |
| 64 | + def execute( |
| 65 | + self, |
| 66 | + ctx: WorkflowContext, |
| 67 | + request: TransferRequest, |
| 68 | + command_results: CommandResults, |
| 69 | + persistence: Persistence, |
| 70 | + communication: Communication, |
| 71 | + ) -> StateDecision: |
| 72 | + print(f"API to debit account {request.from_account} for amount{request.amount}") |
| 73 | + |
| 74 | + return StateDecision.single_next_state(CreateCreditMemoState, request) |
| 75 | + |
| 76 | + def get_state_options(self) -> WorkflowStateOptions: |
| 77 | + return WorkflowStateOptions( |
| 78 | + execute_failure_handling_state=CompensateState, |
| 79 | + execute_api_retry_policy=RetryPolicy( |
| 80 | + maximum_attempts_duration_seconds=3600, |
| 81 | + ) |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +class CreateCreditMemoState(WorkflowState[TransferRequest]): |
| 86 | + def execute( |
| 87 | + self, |
| 88 | + ctx: WorkflowContext, |
| 89 | + request: TransferRequest, |
| 90 | + command_results: CommandResults, |
| 91 | + persistence: Persistence, |
| 92 | + communication: Communication, |
| 93 | + ) -> StateDecision: |
| 94 | + print(f"API to create credit memo for account {request.to_account} for amount{request.amount} with notes{request.notes}") |
| 95 | + |
| 96 | + return StateDecision.single_next_state(CreditState, request) |
| 97 | + |
| 98 | + def get_state_options(self) -> WorkflowStateOptions: |
| 99 | + return WorkflowStateOptions( |
| 100 | + execute_failure_handling_state=CompensateState, |
| 101 | + execute_api_retry_policy=RetryPolicy( |
| 102 | + maximum_attempts_duration_seconds=3600, |
| 103 | + ) |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +class CreditState(WorkflowState[TransferRequest]): |
| 108 | + def execute( |
| 109 | + self, |
| 110 | + ctx: WorkflowContext, |
| 111 | + request: TransferRequest, |
| 112 | + command_results: CommandResults, |
| 113 | + persistence: Persistence, |
| 114 | + communication: Communication, |
| 115 | + ) -> StateDecision: |
| 116 | + print(f"API to credit account {request.to_account} for amount{request.amount}") |
| 117 | + |
| 118 | + return StateDecision.graceful_complete_workflow(f"transfer is done from account{request.from_account} " |
| 119 | + f"to account{request.to_account} for amount{request.amount}") |
| 120 | + |
| 121 | + def get_state_options(self) -> WorkflowStateOptions: |
| 122 | + return WorkflowStateOptions( |
| 123 | + execute_failure_handling_state=CompensateState, |
| 124 | + execute_api_retry_policy=RetryPolicy( |
| 125 | + maximum_attempts_duration_seconds=3600, |
| 126 | + ) |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +class CompensateState(WorkflowState[TransferRequest]): |
| 131 | + def execute( |
| 132 | + self, |
| 133 | + ctx: WorkflowContext, |
| 134 | + request: TransferRequest, |
| 135 | + command_results: CommandResults, |
| 136 | + persistence: Persistence, |
| 137 | + communication: Communication, |
| 138 | + ) -> StateDecision: |
| 139 | + # NOTE: to improve, we can use iWF data attributes to track whether each step has been attempted to execute |
| 140 | + # and check a flag to see if we should undo it or not |
| 141 | + |
| 142 | + print(f"API to undo credit account {request.to_account} for amount{request.amount}") |
| 143 | + print(f"API to undo create credit memo account {request.to_account} for amount{request.amount}") |
| 144 | + print(f"API to undo debit account {request.from_account} for amount{request.amount}") |
| 145 | + print(f"API to undo create debit memo {request.from_account} for amount{request.amount}") |
| 146 | + |
| 147 | + return StateDecision.force_fail_workflow("fail to transfer") |
| 148 | + |
| 149 | + |
| 150 | +class MoneyTransferWorkflow(ObjectWorkflow): |
| 151 | + def get_workflow_states(self) -> StateSchema: |
| 152 | + return StateSchema.with_starting_state( |
| 153 | + VerifyState(), |
| 154 | + CreateDebitMemoState(), |
| 155 | + DebitState(), |
| 156 | + CreateCreditMemoState(), |
| 157 | + CreditState(), |
| 158 | + CompensateState()) |
0 commit comments