|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Copyright 2025 The Dapr Authors |
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the specific language governing permissions and |
| 12 | +limitations under the License. |
| 13 | +""" |
| 14 | + |
| 15 | +from datetime import timedelta |
| 16 | +from time import sleep |
| 17 | + |
| 18 | +from dapr.ext.workflow import ( |
| 19 | + AsyncWorkflowContext, |
| 20 | + DaprWorkflowClient, |
| 21 | + RetryPolicy, |
| 22 | + WorkflowActivityContext, |
| 23 | + WorkflowRuntime, |
| 24 | +) |
| 25 | + |
| 26 | +counter = 0 |
| 27 | +retry_count = 0 |
| 28 | +child_orchestrator_string = '' |
| 29 | +instance_id = 'asyncExampleInstanceID' |
| 30 | +child_instance_id = 'asyncChildInstanceID' |
| 31 | +workflow_name = 'async_hello_world_wf' |
| 32 | +child_workflow_name = 'async_child_wf' |
| 33 | +input_data = 'Hi Async Counter!' |
| 34 | +event_name = 'event1' |
| 35 | +event_data = 'eventData' |
| 36 | + |
| 37 | +retry_policy = RetryPolicy( |
| 38 | + first_retry_interval=timedelta(seconds=1), |
| 39 | + max_number_of_attempts=3, |
| 40 | + backoff_coefficient=2, |
| 41 | + max_retry_interval=timedelta(seconds=10), |
| 42 | + retry_timeout=timedelta(seconds=100), |
| 43 | +) |
| 44 | + |
| 45 | +wfr = WorkflowRuntime() |
| 46 | + |
| 47 | + |
| 48 | +@wfr.async_workflow(name=workflow_name) |
| 49 | +async def hello_world_wf(ctx: AsyncWorkflowContext, wf_input): |
| 50 | + # activities |
| 51 | + result_1 = await ctx.call_activity(hello_act, input=1) |
| 52 | + print(f'Activity 1 returned {result_1}') |
| 53 | + result_2 = await ctx.call_activity(hello_act, input=10) |
| 54 | + print(f'Activity 2 returned {result_2}') |
| 55 | + result_3 = await ctx.call_activity(hello_retryable_act, retry_policy=retry_policy) |
| 56 | + print(f'Activity 3 returned {result_3}') |
| 57 | + result_4 = await ctx.call_child_workflow(child_retryable_wf, retry_policy=retry_policy) |
| 58 | + print(f'Child workflow returned {result_4}') |
| 59 | + |
| 60 | + # Event vs timeout using when_any |
| 61 | + first = await ctx.when_any( |
| 62 | + [ |
| 63 | + ctx.wait_for_external_event(event_name), |
| 64 | + ctx.create_timer(timedelta(seconds=30)), |
| 65 | + ] |
| 66 | + ) |
| 67 | + |
| 68 | + # 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' |
| 73 | + return 'Timeout' |
| 74 | + |
| 75 | + |
| 76 | +@wfr.activity(name='async_hello_act') |
| 77 | +def hello_act(ctx: WorkflowActivityContext, wf_input): |
| 78 | + global counter |
| 79 | + counter += wf_input |
| 80 | + return f'Activity returned {wf_input}' |
| 81 | + |
| 82 | + |
| 83 | +@wfr.activity(name='async_hello_retryable_act') |
| 84 | +def hello_retryable_act(ctx: WorkflowActivityContext): |
| 85 | + global retry_count |
| 86 | + if (retry_count % 2) == 0: |
| 87 | + retry_count += 1 |
| 88 | + raise ValueError('Retryable Error') |
| 89 | + retry_count += 1 |
| 90 | + return f'Activity returned {retry_count}' |
| 91 | + |
| 92 | + |
| 93 | +@wfr.async_workflow(name=child_workflow_name) |
| 94 | +async def child_retryable_wf(ctx: AsyncWorkflowContext): |
| 95 | + # Call activity with retry and simulate retryable workflow failure until certain state |
| 96 | + child_activity_result = await ctx.call_activity( |
| 97 | + act_for_child_wf, input='x', retry_policy=retry_policy |
| 98 | + ) |
| 99 | + print(f'Child activity returned {child_activity_result}') |
| 100 | + # In a real sample, you might check state and raise to trigger retry |
| 101 | + return 'ok' |
| 102 | + |
| 103 | + |
| 104 | +@wfr.activity(name='async_act_for_child_wf') |
| 105 | +def act_for_child_wf(ctx: WorkflowActivityContext, inp): |
| 106 | + global child_orchestrator_string |
| 107 | + child_orchestrator_string += inp |
| 108 | + |
| 109 | + |
| 110 | +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() |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == '__main__': |
| 136 | + main() |
0 commit comments