|
| 1 | +import inspect |
| 2 | +import time |
| 3 | +import unittest |
| 4 | + |
| 5 | +from iwf.client import Client |
| 6 | +from iwf.command_request import CommandRequest |
| 7 | +from iwf.command_results import CommandResults |
| 8 | +from iwf.communication import Communication |
| 9 | +from iwf.persistence_schema import PersistenceSchema, PersistenceField |
| 10 | +from iwf.workflow_options import WorkflowOptions |
| 11 | + |
| 12 | +from iwf.iwf_api.models import RetryPolicy, WaitUntilApiFailurePolicy, IDReusePolicy |
| 13 | +from iwf.persistence import Persistence |
| 14 | +from iwf.state_decision import StateDecision |
| 15 | +from iwf.state_schema import StateSchema |
| 16 | +from iwf.tests.worker_server import registry |
| 17 | +from iwf.workflow import ObjectWorkflow |
| 18 | +from iwf.workflow_context import WorkflowContext |
| 19 | +from iwf.workflow_state import T, WorkflowState |
| 20 | +from iwf.workflow_state_options import WorkflowStateOptions |
| 21 | + |
| 22 | +output_da = "output_da" |
| 23 | + |
| 24 | + |
| 25 | +class InitState(WorkflowState[str]): |
| 26 | + def wait_until( |
| 27 | + self, |
| 28 | + ctx: WorkflowContext, |
| 29 | + input: T, |
| 30 | + persistence: Persistence, |
| 31 | + communication: Communication, |
| 32 | + ) -> CommandRequest: |
| 33 | + persistence.set_data_attribute( |
| 34 | + output_da, str(input) + "_InitState_waitUntil_completed" |
| 35 | + ) |
| 36 | + return CommandRequest.empty() |
| 37 | + |
| 38 | + def execute( |
| 39 | + self, |
| 40 | + ctx: WorkflowContext, |
| 41 | + input: T, |
| 42 | + command_results: CommandResults, |
| 43 | + persistence: Persistence, |
| 44 | + communication: Communication, |
| 45 | + ) -> StateDecision: |
| 46 | + data = persistence.get_data_attribute(output_da) |
| 47 | + data += "_InitState_execute_completed" |
| 48 | + return StateDecision.single_next_state( |
| 49 | + NonInitState, |
| 50 | + data, |
| 51 | + WorkflowStateOptions( |
| 52 | + wait_until_api_retry_policy=RetryPolicy(maximum_attempts=2), |
| 53 | + proceed_to_execute_when_wait_until_retry_exhausted=WaitUntilApiFailurePolicy.PROCEED_ON_FAILURE, |
| 54 | + ), |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +class NonInitState(WorkflowState[str]): |
| 59 | + def wait_until( |
| 60 | + self, |
| 61 | + ctx: WorkflowContext, |
| 62 | + input: T, |
| 63 | + persistence: Persistence, |
| 64 | + communication: Communication, |
| 65 | + ) -> CommandRequest: |
| 66 | + raise RuntimeError("test failure") |
| 67 | + |
| 68 | + def execute( |
| 69 | + self, |
| 70 | + ctx: WorkflowContext, |
| 71 | + input: T, |
| 72 | + command_results: CommandResults, |
| 73 | + persistence: Persistence, |
| 74 | + communication: Communication, |
| 75 | + ) -> StateDecision: |
| 76 | + data = str(input) + "_NonInitState_execute_completed" |
| 77 | + return StateDecision.graceful_complete_workflow(data) |
| 78 | + |
| 79 | + def get_state_options(self) -> WorkflowStateOptions: |
| 80 | + return WorkflowStateOptions( |
| 81 | + wait_until_api_retry_policy=RetryPolicy(maximum_attempts=1), |
| 82 | + proceed_to_execute_when_wait_until_retry_exhausted=WaitUntilApiFailurePolicy.FAIL_WORKFLOW_ON_FAILURE, |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +class StateOptionsOverrideWorkflow(ObjectWorkflow): |
| 87 | + def get_persistence_schema(self) -> PersistenceSchema: |
| 88 | + return PersistenceSchema.create( |
| 89 | + PersistenceField.data_attribute_def(output_da, str), |
| 90 | + ) |
| 91 | + |
| 92 | + def get_workflow_states(self) -> StateSchema: |
| 93 | + return StateSchema.with_starting_state(InitState(), NonInitState()) |
| 94 | + |
| 95 | + |
| 96 | +class TestStateOptionsOverrideWorkflow(unittest.TestCase): |
| 97 | + @classmethod |
| 98 | + def setUpClass(cls): |
| 99 | + wf = StateOptionsOverrideWorkflow() |
| 100 | + registry.add_workflow(wf) |
| 101 | + cls.client = Client(registry) |
| 102 | + |
| 103 | + def test_override(self): |
| 104 | + wf_id = f"{inspect.currentframe().f_code.co_name}-{time.time_ns()}" |
| 105 | + self.client.start_workflow( |
| 106 | + StateOptionsOverrideWorkflow, |
| 107 | + wf_id, |
| 108 | + 10, |
| 109 | + "input", |
| 110 | + WorkflowOptions(workflow_id_reuse_policy=IDReusePolicy.DISALLOW_REUSE), |
| 111 | + ) |
| 112 | + output = self.client.wait_for_workflow_completion(wf_id) |
| 113 | + |
| 114 | + assert ( |
| 115 | + output |
| 116 | + == "input_InitState_waitUntil_completed_InitState_execute_completed_NonInitState_execute_completed" |
| 117 | + ) |
0 commit comments