|
| 1 | +from iwf.command_request import CommandRequest, InternalChannelCommand, TimerCommand |
| 2 | +from iwf.command_results import CommandResults |
| 3 | +from iwf.communication import Communication |
| 4 | +from iwf.communication_schema import CommunicationSchema, CommunicationMethod |
| 5 | +from iwf.iwf_api.models import ChannelRequestStatus |
| 6 | +from iwf.persistence import Persistence |
| 7 | +from iwf.persistence_schema import PersistenceSchema, PersistenceField |
| 8 | +from iwf.rpc import rpc |
| 9 | +from iwf.state_decision import StateDecision |
| 10 | +from iwf.state_schema import StateSchema |
| 11 | +from iwf.workflow import ObjectWorkflow |
| 12 | +from iwf.workflow_context import WorkflowContext |
| 13 | +from iwf.workflow_state import WorkflowState |
| 14 | + |
| 15 | +TEST_APPROVAL_KEY = "Approval" |
| 16 | +TEST_STRING_KEY = "TestString" |
| 17 | + |
| 18 | +class BasicWorkflow(ObjectWorkflow): |
| 19 | + def get_workflow_states(self) -> StateSchema: |
| 20 | + return StateSchema.with_starting_state( |
| 21 | + BasicWorkflowState1(), |
| 22 | + BasicWorkflowState2()) |
| 23 | + |
| 24 | + |
| 25 | + def get_persistence_schema(self) -> PersistenceSchema: |
| 26 | + return PersistenceSchema.create( |
| 27 | + PersistenceField.data_attribute_def(TEST_STRING_KEY, str), |
| 28 | + ) |
| 29 | + |
| 30 | + def get_communication_schema(self) -> CommunicationSchema: |
| 31 | + return CommunicationSchema.create( |
| 32 | + CommunicationMethod.internal_channel_def(TEST_APPROVAL_KEY, str) |
| 33 | + ) |
| 34 | + |
| 35 | + @rpc() |
| 36 | + def append_string(self, st: str, persistence: Persistence) -> str: |
| 37 | + current = persistence.get_data_attribute(TEST_STRING_KEY) |
| 38 | + if current is None: |
| 39 | + current = "" |
| 40 | + current = current + ", " + st |
| 41 | + persistence.set_data_attribute(TEST_STRING_KEY, current) |
| 42 | + return current |
| 43 | + |
| 44 | + @rpc() |
| 45 | + def approve(self, communication: Communication): |
| 46 | + communication.publish_to_internal_channel(TEST_APPROVAL_KEY, "approved") |
| 47 | + |
| 48 | +class BasicWorkflowState1(WorkflowState[int]): |
| 49 | + def execute( |
| 50 | + self, |
| 51 | + ctx: WorkflowContext, |
| 52 | + data: int, |
| 53 | + command_results: CommandResults, |
| 54 | + persistence: Persistence, |
| 55 | + communication: Communication, |
| 56 | + ) -> StateDecision: |
| 57 | + output = data + 1 |
| 58 | + return StateDecision.single_next_state(BasicWorkflowState2, output) |
| 59 | + |
| 60 | +class BasicWorkflowState2(WorkflowState[int]): |
| 61 | + def wait_until( |
| 62 | + self, |
| 63 | + ctx: WorkflowContext, |
| 64 | + data: int, |
| 65 | + persistence: Persistence, |
| 66 | + communication: Communication, |
| 67 | + ) -> CommandRequest: |
| 68 | + return CommandRequest.for_any_command_completed( |
| 69 | + InternalChannelCommand.by_name(TEST_APPROVAL_KEY), |
| 70 | + TimerCommand.by_seconds(data) |
| 71 | + ) |
| 72 | + |
| 73 | + def execute( |
| 74 | + self, |
| 75 | + ctx: WorkflowContext, |
| 76 | + data: int, |
| 77 | + command_results: CommandResults, |
| 78 | + persistence: Persistence, |
| 79 | + communication: Communication, |
| 80 | + ) -> StateDecision: |
| 81 | + internal_channel_result = command_results.internal_channel_commands[0] |
| 82 | + if internal_channel_result.status == ChannelRequestStatus.RECEIVED: |
| 83 | + return StateDecision.graceful_complete_workflow(internal_channel_result.value) |
| 84 | + else: |
| 85 | + return StateDecision.single_next_state(BasicWorkflowState2, data) |
| 86 | + |
| 87 | + |
0 commit comments