|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2026 The Dapr Authors |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | + |
| 13 | +"""History propagation example. |
| 14 | +
|
| 15 | +The parent workflow runs a couple of activities, then calls a child workflow |
| 16 | +with ``propagation=PropagationScope.OWN_HISTORY`` and an activity with |
| 17 | +``propagation=PropagationScope.LINEAGE``. The child workflow and the |
| 18 | +downstream activity read the parent's recorded history via |
| 19 | +``ctx.get_propagated_history()`` and inspect specific events by name. |
| 20 | +
|
| 21 | +This requires a Dapr sidecar built with history propagation enabled |
| 22 | +(durabletask-go PR #85 and later). With an older sidecar, the propagation |
| 23 | +field is silently dropped and ``get_propagated_history()`` returns ``None``. |
| 24 | +""" |
| 25 | + |
| 26 | +from __future__ import annotations |
| 27 | + |
| 28 | +import json |
| 29 | + |
| 30 | +import dapr.ext.workflow as wf |
| 31 | + |
| 32 | +wfr = wf.WorkflowRuntime() |
| 33 | + |
| 34 | + |
| 35 | +@wfr.activity(name='validate_merchant') |
| 36 | +def validate_merchant(ctx: wf.WorkflowActivityContext, merchant_id: str) -> dict: |
| 37 | + print(f'*** validating merchant {merchant_id}', flush=True) |
| 38 | + return {'merchant_id': merchant_id, 'valid': True} |
| 39 | + |
| 40 | + |
| 41 | +@wfr.activity(name='log_summary') |
| 42 | +def log_summary(ctx: wf.WorkflowActivityContext, _: None) -> str: |
| 43 | + """Activity that reads the parent workflow's propagated history.""" |
| 44 | + history = ctx.get_propagated_history() |
| 45 | + if history is None: |
| 46 | + print('*** log_summary: no propagated history (sidecar may not support it)', flush=True) |
| 47 | + return 'no-history' |
| 48 | + |
| 49 | + workflows = history.get_workflows() |
| 50 | + if not workflows: |
| 51 | + print('*** log_summary: propagated history has no workflows', flush=True) |
| 52 | + return 'empty-history' |
| 53 | + |
| 54 | + parent = workflows[-1] |
| 55 | + try: |
| 56 | + validate = parent.get_activity_by_name('validate_merchant') |
| 57 | + except wf.PropagationNotFoundError: |
| 58 | + print('*** log_summary: parent did not run validate_merchant', flush=True) |
| 59 | + return 'parent-missing-validate' |
| 60 | + |
| 61 | + print( |
| 62 | + f'*** log_summary saw parent on app {parent.app_id} ' |
| 63 | + f'with validate_merchant -> completed={validate.completed} output={validate.output}', |
| 64 | + flush=True, |
| 65 | + ) |
| 66 | + return 'logged' |
| 67 | + |
| 68 | + |
| 69 | +@wfr.workflow(name='process_payment') |
| 70 | +def process_payment(ctx: wf.DaprWorkflowContext, _: None): |
| 71 | + """Child workflow: introspect the parent's history before deciding.""" |
| 72 | + history = ctx.get_propagated_history() |
| 73 | + if history is None: |
| 74 | + print('*** process_payment: no propagated history', flush=True) |
| 75 | + return 'no-history' |
| 76 | + |
| 77 | + workflows = history.get_workflows() |
| 78 | + if not workflows: |
| 79 | + print('*** process_payment: propagated history has no workflows', flush=True) |
| 80 | + return 'empty-history' |
| 81 | + |
| 82 | + parent = workflows[-1] |
| 83 | + try: |
| 84 | + validate = parent.get_activity_by_name('validate_merchant') |
| 85 | + except wf.PropagationNotFoundError: |
| 86 | + print('*** process_payment: parent did not run validate_merchant', flush=True) |
| 87 | + return 'parent-missing-validate' |
| 88 | + |
| 89 | + if not validate.completed: |
| 90 | + print('*** process_payment: parent validate_merchant is not complete yet', flush=True) |
| 91 | + return 'parent-incomplete' |
| 92 | + |
| 93 | + merchant = json.loads(validate.output or '{}') |
| 94 | + print( |
| 95 | + f'*** process_payment received parent context for merchant {merchant.get("merchant_id")!r}', |
| 96 | + flush=True, |
| 97 | + ) |
| 98 | + return 'paid' |
| 99 | + |
| 100 | + |
| 101 | +@wfr.workflow(name='merchant_checkout') |
| 102 | +def merchant_checkout(ctx: wf.DaprWorkflowContext, merchant_id: str): |
| 103 | + """Parent workflow: runs an activity, then propagates its history.""" |
| 104 | + yield ctx.call_activity(validate_merchant, input=merchant_id) |
| 105 | + |
| 106 | + child_result = yield ctx.call_child_workflow( |
| 107 | + process_payment, |
| 108 | + input=None, |
| 109 | + propagation=wf.PropagationScope.OWN_HISTORY, |
| 110 | + ) |
| 111 | + print(f'*** child workflow result: {child_result}', flush=True) |
| 112 | + |
| 113 | + audit = yield ctx.call_activity( |
| 114 | + log_summary, |
| 115 | + input=None, |
| 116 | + propagation=wf.PropagationScope.LINEAGE, |
| 117 | + ) |
| 118 | + print(f'*** audit activity result: {audit}', flush=True) |
| 119 | + return {'child': child_result, 'audit': audit} |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == '__main__': |
| 123 | + wfr.start() |
| 124 | + |
| 125 | + wf_client = wf.DaprWorkflowClient() |
| 126 | + instance_id = wf_client.schedule_new_workflow(workflow=merchant_checkout, input='merchant-42') |
| 127 | + |
| 128 | + state = wf_client.wait_for_workflow_completion(instance_id, timeout_in_seconds=30) |
| 129 | + print( |
| 130 | + f'*** workflow completed: status={state.runtime_status.name} output={state.serialized_output}', |
| 131 | + flush=True, |
| 132 | + ) |
| 133 | + |
| 134 | + wfr.shutdown() |
0 commit comments