|
11 | 11 | See the specific language governing permissions and |
12 | 12 | limitations under the License. |
13 | 13 | """ |
| 14 | +import json |
14 | 15 |
|
15 | 16 | from datetime import timedelta |
16 | 17 | from time import sleep |
|
21 | 22 | RetryPolicy, |
22 | 23 | WorkflowActivityContext, |
23 | 24 | WorkflowRuntime, |
| 25 | + WorkflowStatus, |
24 | 26 | ) |
25 | 27 |
|
26 | 28 | counter = 0 |
@@ -58,18 +60,19 @@ async def hello_world_wf(ctx: AsyncWorkflowContext, wf_input): |
58 | 60 | print(f'Child workflow returned {result_4}') |
59 | 61 |
|
60 | 62 | # Event vs timeout using when_any |
| 63 | + event_1 = ctx.wait_for_external_event(event_name) |
61 | 64 | first = await ctx.when_any( |
62 | 65 | [ |
63 | | - ctx.wait_for_external_event(event_name), |
| 66 | + event_1, |
64 | 67 | ctx.create_timer(timedelta(seconds=30)), |
65 | 68 | ] |
66 | 69 | ) |
67 | 70 |
|
68 | 71 | # Proceed only if event won |
69 | | - if isinstance(first, dict) and 'event' in first: |
70 | | - await ctx.call_activity(hello_act, input=100) |
71 | | - await ctx.call_activity(hello_act, input=1000) |
72 | | - return 'Completed' |
| 72 | + if first == event_1: |
| 73 | + result_5 = await ctx.call_activity(hello_act, input=100) |
| 74 | + result_6 = await ctx.call_activity(hello_act, input=1000) |
| 75 | + return dict(result_1=result_1, result_2=result_2, result_3=result_3, result_4=result_4, result_5=result_5, result_6=result_6) |
73 | 76 | return 'Timeout' |
74 | 77 |
|
75 | 78 |
|
@@ -108,28 +111,37 @@ def act_for_child_wf(ctx: WorkflowActivityContext, inp): |
108 | 111 |
|
109 | 112 |
|
110 | 113 | def main(): |
111 | | - wfr.start() |
112 | | - wf_client = DaprWorkflowClient() |
113 | | - |
114 | | - wf_client.schedule_new_workflow( |
115 | | - workflow=hello_world_wf, input=input_data, instance_id=instance_id |
116 | | - ) |
117 | | - |
118 | | - wf_client.wait_for_workflow_start(instance_id) |
119 | | - |
120 | | - # Let initial activities run |
121 | | - sleep(5) |
122 | | - |
123 | | - # Raise event to continue |
124 | | - wf_client.raise_workflow_event( |
125 | | - instance_id=instance_id, event_name=event_name, data={'ok': True} |
126 | | - ) |
127 | | - |
128 | | - # Wait for completion |
129 | | - state = wf_client.wait_for_workflow_completion(instance_id, timeout_in_seconds=60) |
130 | | - print(f'Workflow status: {state.runtime_status.name}') |
131 | | - |
132 | | - wfr.shutdown() |
| 114 | + wf_state = {} |
| 115 | + with wfr: |
| 116 | + wf_client = DaprWorkflowClient() |
| 117 | + |
| 118 | + wf_client.schedule_new_workflow( |
| 119 | + workflow=hello_world_wf, input=input_data, instance_id=instance_id |
| 120 | + ) |
| 121 | + |
| 122 | + wf_client.wait_for_workflow_start(instance_id) |
| 123 | + |
| 124 | + # Let initial activities run |
| 125 | + sleep(5) |
| 126 | + |
| 127 | + # Raise event to continue |
| 128 | + wf_client.raise_workflow_event( |
| 129 | + instance_id=instance_id, event_name=event_name, data={'ok': True} |
| 130 | + ) |
| 131 | + |
| 132 | + # Wait for completion |
| 133 | + wf_state = wf_client.wait_for_workflow_completion(instance_id, timeout_in_seconds=60) |
| 134 | + |
| 135 | + # simple test |
| 136 | + if wf_state.runtime_status != WorkflowStatus.COMPLETED: |
| 137 | + print('Workflow failed with status ', wf_state.runtime_status) |
| 138 | + exit(1) |
| 139 | + output = json.loads(wf_state.serialized_output) |
| 140 | + if (output["result_1"] != 'Activity returned 1' or output["result_2"] != 'Activity returned 10' or |
| 141 | + output["result_3"] != 'Activity returned 2' or output["result_4"] != 'ok' or |
| 142 | + output["result_5"] != 'Activity returned 100' or output["result_6"] != 'Activity returned 1000'): |
| 143 | + print('Workflow result is incorrect!') |
| 144 | + exit(1) |
133 | 145 |
|
134 | 146 |
|
135 | 147 | if __name__ == '__main__': |
|
0 commit comments