-
Notifications
You must be signed in to change notification settings - Fork 148
Add history propagation #1025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add history propagation #1025
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright 2026 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. | ||
|
|
||
| """History propagation example. | ||
|
|
||
| The parent workflow runs a couple of activities, then calls a child workflow | ||
| with ``propagation=PropagationScope.OWN_HISTORY`` and an activity with | ||
| ``propagation=PropagationScope.LINEAGE``. The child workflow and the | ||
| downstream activity read the parent's recorded history via | ||
| ``ctx.get_propagated_history()`` and inspect specific events by name. | ||
|
|
||
| This requires a Dapr sidecar built with history propagation enabled | ||
| (durabletask-go PR #85 and later). With an older sidecar, the propagation | ||
| field is silently dropped and ``get_propagated_history()`` returns ``None``. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
|
|
||
| import dapr.ext.workflow as wf | ||
|
|
||
| wfr = wf.WorkflowRuntime() | ||
|
|
||
|
|
||
| @wfr.activity(name='validate_merchant') | ||
| def validate_merchant(ctx: wf.WorkflowActivityContext, merchant_id: str) -> dict: | ||
| print(f'*** validating merchant {merchant_id}', flush=True) | ||
| return {'merchant_id': merchant_id, 'valid': True} | ||
|
|
||
|
|
||
| @wfr.activity(name='log_summary') | ||
| def log_summary(ctx: wf.WorkflowActivityContext, _: None) -> str: | ||
| """Activity that reads the parent workflow's propagated history.""" | ||
| history = ctx.get_propagated_history() | ||
| if history is None: | ||
| print('*** log_summary: no propagated history (sidecar may not support it)', flush=True) | ||
| return 'no-history' | ||
|
|
||
| workflows = history.get_workflows() | ||
| if not workflows: | ||
| print('*** log_summary: propagated history has no workflows', flush=True) | ||
| return 'empty-history' | ||
|
|
||
| parent = workflows[-1] | ||
| try: | ||
| validate = parent.get_activity_by_name('validate_merchant') | ||
| except wf.PropagationNotFoundError: | ||
| print('*** log_summary: parent did not run validate_merchant', flush=True) | ||
| return 'parent-missing-validate' | ||
|
|
||
| print( | ||
| f'*** log_summary saw parent on app {parent.app_id} ' | ||
| f'with validate_merchant -> completed={validate.completed} output={validate.output}', | ||
| flush=True, | ||
| ) | ||
| return 'logged' | ||
|
|
||
|
|
||
| @wfr.workflow(name='process_payment') | ||
| def process_payment(ctx: wf.DaprWorkflowContext, _: None): | ||
| """Child workflow: introspect the parent's history before deciding.""" | ||
| history = ctx.get_propagated_history() | ||
| if history is None: | ||
| print('*** process_payment: no propagated history', flush=True) | ||
| return 'no-history' | ||
|
|
||
| workflows = history.get_workflows() | ||
| if not workflows: | ||
| print('*** process_payment: propagated history has no workflows', flush=True) | ||
| return 'empty-history' | ||
|
|
||
| parent = workflows[-1] | ||
| try: | ||
| validate = parent.get_activity_by_name('validate_merchant') | ||
| except wf.PropagationNotFoundError: | ||
| print('*** process_payment: parent did not run validate_merchant', flush=True) | ||
| return 'parent-missing-validate' | ||
|
|
||
| if not validate.completed: | ||
| print('*** process_payment: parent validate_merchant is not complete yet', flush=True) | ||
| return 'parent-incomplete' | ||
|
|
||
| merchant = json.loads(validate.output or '{}') | ||
| print( | ||
| f'*** process_payment received parent context for merchant {merchant.get("merchant_id")!r}', | ||
| flush=True, | ||
| ) | ||
| return 'paid' | ||
|
|
||
|
|
||
| @wfr.workflow(name='merchant_checkout') | ||
| def merchant_checkout(ctx: wf.DaprWorkflowContext, merchant_id: str): | ||
| """Parent workflow: runs an activity, then propagates its history.""" | ||
| yield ctx.call_activity(validate_merchant, input=merchant_id) | ||
|
|
||
| child_result = yield ctx.call_child_workflow( | ||
| process_payment, | ||
| input=None, | ||
| propagation=wf.PropagationScope.OWN_HISTORY, | ||
| ) | ||
| print(f'*** child workflow result: {child_result}', flush=True) | ||
|
|
||
| audit = yield ctx.call_activity( | ||
| log_summary, | ||
| input=None, | ||
| propagation=wf.PropagationScope.LINEAGE, | ||
| ) | ||
| print(f'*** audit activity result: {audit}', flush=True) | ||
| return {'child': child_result, 'audit': audit} | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| wfr.start() | ||
|
|
||
| wf_client = wf.DaprWorkflowClient() | ||
| instance_id = wf_client.schedule_new_workflow(workflow=merchant_checkout, input='merchant-42') | ||
|
|
||
| state = wf_client.wait_for_workflow_completion(instance_id, timeout_in_seconds=30) | ||
| print( | ||
| f'*** workflow completed: status={state.runtime_status.name} output={state.serialized_output}', | ||
| flush=True, | ||
| ) | ||
|
|
||
| wfr.shutdown() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
ext/dapr-ext-workflow/dapr/ext/workflow/_durabletask/internal/attestation_pb2.py
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.