From 08851985f90b27c3387cb1347bf29ac23a6acfb3 Mon Sep 17 00:00:00 2001 From: MregXN <46479059+MregXN@users.noreply.github.com> Date: Thu, 14 Dec 2023 05:39:26 +0800 Subject: [PATCH] add call child workflow sample (#644) Signed-off-by: MregXN --- examples/workflow/README.md | 22 +++++++++++++++ examples/workflow/child_workflow.py | 44 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 examples/workflow/child_workflow.py diff --git a/examples/workflow/README.md b/examples/workflow/README.md index c97d4562f..769d6820f 100644 --- a/examples/workflow/README.md +++ b/examples/workflow/README.md @@ -143,3 +143,25 @@ Job 'job1' is unhealthy. ``` This workflow runs forever or until you press `ENTER` to stop it. Starting the app again after stopping it will cause the same workflow instance to resume where it left off. + +### Child Workflow + +This example demonstrates how to call a child workflow. The Dapr CLI can be started using the following command: + +```sh +dapr run --app-id wfexample --dapr-grpc-port 50001 +``` + +In a separate terminal window, run the following command to start the Python workflow app: + +```sh +python3 child_workflow.py +``` + +When you run the example, you will see output like this: +``` +... +*** Calling child workflow 29a7592a1e874b07aad2bb58de309a51-child +*** Child workflow 6feadc5370184b4998e50875b20084f6 called +... +``` \ No newline at end of file diff --git a/examples/workflow/child_workflow.py b/examples/workflow/child_workflow.py new file mode 100644 index 000000000..20fc1382d --- /dev/null +++ b/examples/workflow/child_workflow.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# Copyright 2023 The Dapr Authors +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import dapr.ext.workflow as wf +import time + +def main_workflow(ctx: wf.DaprWorkflowContext): + try: + instance_id = ctx.instance_id + child_instance_id = instance_id + '-child' + print(f'*** Calling child workflow {child_instance_id}') + yield ctx.call_child_workflow(workflow=child_workflow,input=None,instance_id=child_instance_id) + except Exception as e: + print(f'*** Exception: {e}') + + return + +def child_workflow(ctx: wf.DaprWorkflowContext): + instance_id = ctx.instance_id + print(f'*** Child workflow {instance_id} called') + +if __name__ == '__main__': + workflowRuntime = wf.WorkflowRuntime("localhost", "50001") + workflowRuntime.register_workflow(main_workflow) + workflowRuntime.register_workflow(child_workflow) + workflowRuntime.start() + + wf_client = wf.DaprWorkflowClient() + instance_id = wf_client.schedule_new_workflow( + workflow=main_workflow) + + # Wait for the workflow to complete + time.sleep(5) + + workflowRuntime.shutdown() \ No newline at end of file