Skip to content

Commit 373616a

Browse files
Updates workflows docs to use the new workflows client (#780)
* Updates docs Signed-off-by: Elena Kolevska <[email protected]> * More workflows docs Signed-off-by: Elena Kolevska <[email protected]> * Move workflows examples, based on review Signed-off-by: Elena Kolevska <[email protected]> * Small fixes Signed-off-by: Elena Kolevska <[email protected]> --------- Signed-off-by: Elena Kolevska <[email protected]>
1 parent 149d722 commit 373616a

File tree

3 files changed

+179
-216
lines changed

3 files changed

+179
-216
lines changed

Diff for: daprdocs/content/en/python-sdk-docs/python-client.md

-84
Original file line numberDiff line numberDiff line change
@@ -515,89 +515,5 @@ def main():
515515
- For a full list of state operations visit [How-To: Use the cryptography APIs]({{< ref howto-cryptography.md >}}).
516516
- Visit [Python SDK examples](https://github.com/dapr/python-sdk/tree/master/examples/crypto) for code samples and instructions to try out cryptography
517517

518-
### Workflow
519-
520-
```python
521-
from dapr.ext.workflow import WorkflowRuntime, DaprWorkflowContext, WorkflowActivityContext
522-
from dapr.clients import DaprClient
523-
524-
instanceId = "exampleInstanceID"
525-
workflowComponent = "dapr"
526-
workflowName = "hello_world_wf"
527-
eventName = "event1"
528-
eventData = "eventData"
529-
530-
def main():
531-
with DaprClient() as d:
532-
host = settings.DAPR_RUNTIME_HOST
533-
port = settings.DAPR_GRPC_PORT
534-
workflowRuntime = WorkflowRuntime(host, port)
535-
workflowRuntime = WorkflowRuntime()
536-
workflowRuntime.register_workflow(hello_world_wf)
537-
workflowRuntime.register_activity(hello_act)
538-
workflowRuntime.start()
539-
540-
# Start the workflow
541-
start_resp = d.start_workflow(instance_id=instanceId, workflow_component=workflowComponent,
542-
workflow_name=workflowName, input=inputData, workflow_options=workflowOptions)
543-
print(f"start_resp {start_resp.instance_id}")
544-
545-
# ...
546-
547-
# Pause Test
548-
d.pause_workflow(instance_id=instanceId, workflow_component=workflowComponent)
549-
getResponse = d.get_workflow(instance_id=instanceId, workflow_component=workflowComponent)
550-
print(f"Get response from {workflowName} after pause call: {getResponse.runtime_status}")
551-
552-
# Resume Test
553-
d.resume_workflow(instance_id=instanceId, workflow_component=workflowComponent)
554-
getResponse = d.get_workflow(instance_id=instanceId, workflow_component=workflowComponent)
555-
print(f"Get response from {workflowName} after resume call: {getResponse.runtime_status}")
556-
557-
sleep(1)
558-
# Raise event
559-
d.raise_workflow_event(instance_id=instanceId, workflow_component=workflowComponent,
560-
event_name=eventName, event_data=eventData)
561-
562-
sleep(5)
563-
# Purge Test
564-
d.purge_workflow(instance_id=instanceId, workflow_component=workflowComponent)
565-
try:
566-
getResponse = d.get_workflow(instance_id=instanceId, workflow_component=workflowComponent)
567-
except DaprInternalError as err:
568-
if nonExistentIDError in err._message:
569-
print("Instance Successfully Purged")
570-
571-
572-
# Kick off another workflow for termination purposes
573-
# This will also test using the same instance ID on a new workflow after
574-
# the old instance was purged
575-
start_resp = d.start_workflow(instance_id=instanceId, workflow_component=workflowComponent,
576-
workflow_name=workflowName, input=inputData, workflow_options=workflowOptions)
577-
print(f"start_resp {start_resp.instance_id}")
578-
579-
# Terminate Test
580-
d.terminate_workflow(instance_id=instanceId, workflow_component=workflowComponent)
581-
sleep(1)
582-
getResponse = d.get_workflow(instance_id=instanceId, workflow_component=workflowComponent)
583-
print(f"Get response from {workflowName} after terminate call: {getResponse.runtime_status}")
584-
585-
# Purge Test
586-
d.purge_workflow(instance_id=instanceId, workflow_component=workflowComponent)
587-
try:
588-
getResponse = d.get_workflow(instance_id=instanceId, workflow_component=workflowComponent)
589-
except DaprInternalError as err:
590-
if nonExistentIDError in err._message:
591-
print("Instance Successfully Purged")
592-
593-
workflowRuntime.shutdown()
594-
```
595-
596-
- Learn more about authoring and managing workflows:
597-
- [How-To: Author a workflow]({{< ref howto-author-workflow.md >}}).
598-
- [How-To: Manage a workflow]({{< ref howto-manage-workflow.md >}}).
599-
- Visit [Python SDK examples](https://github.com/dapr/python-sdk/blob/master/examples/demo_workflow/app.py) for code samples and instructions to try out Dapr Workflow.
600-
601-
602518
## Related links
603519
[Python SDK examples](https://github.com/dapr/python-sdk/tree/master/examples)

Diff for: daprdocs/content/en/python-sdk-docs/python-sdk-extensions/python-workflow-ext/_index.md

+69-6
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ description: How to get up and running with the Dapr Workflow extension
77
no_list: true
88
---
99

10-
{{% alert title="Note" color="primary" %}}
11-
Dapr Workflow is currently in alpha.
12-
{{% /alert %}}
13-
14-
The Dapr Python SDK provides a built in Dapr Workflow extension, `dapr.ext.workflow`, for creating Dapr services.
10+
The Dapr Python SDK provides a built-in Dapr Workflow extension, `dapr.ext.workflow`, for creating Dapr services.
1511

1612
## Installation
1713

@@ -31,12 +27,79 @@ The development package will contain features and behavior that will be compatib
3127
{{% /alert %}}
3228

3329
```bash
34-
pip3 install dapr-ext-workflow-dev
30+
pip install dapr-ext-workflow-dev
3531
```
3632
{{% /codetab %}}
3733

3834
{{< /tabs >}}
3935

36+
## Example
37+
38+
```python
39+
from time import sleep
40+
41+
import dapr.ext.workflow as wf
42+
43+
44+
wfr = wf.WorkflowRuntime()
45+
46+
47+
@wfr.workflow(name='random_workflow')
48+
def task_chain_workflow(ctx: wf.DaprWorkflowContext, wf_input: int):
49+
try:
50+
result1 = yield ctx.call_activity(step1, input=wf_input)
51+
result2 = yield ctx.call_activity(step2, input=result1)
52+
except Exception as e:
53+
yield ctx.call_activity(error_handler, input=str(e))
54+
raise
55+
return [result1, result2]
56+
57+
58+
@wfr.activity(name='step1')
59+
def step1(ctx, activity_input):
60+
print(f'Step 1: Received input: {activity_input}.')
61+
# Do some work
62+
return activity_input + 1
63+
64+
65+
@wfr.activity
66+
def step2(ctx, activity_input):
67+
print(f'Step 2: Received input: {activity_input}.')
68+
# Do some work
69+
return activity_input * 2
70+
71+
@wfr.activity
72+
def error_handler(ctx, error):
73+
print(f'Executing error handler: {error}.')
74+
# Do some compensating work
75+
76+
77+
if __name__ == '__main__':
78+
wfr.start()
79+
sleep(10) # wait for workflow runtime to start
80+
81+
wf_client = wf.DaprWorkflowClient()
82+
instance_id = wf_client.schedule_new_workflow(workflow=task_chain_workflow, input=42)
83+
print(f'Workflow started. Instance ID: {instance_id}')
84+
state = wf_client.wait_for_workflow_completion(instance_id)
85+
print(f'Workflow completed! Status: {state.runtime_status}')
86+
87+
wfr.shutdown()
88+
```
89+
90+
- Learn more about authoring and managing workflows:
91+
- [How-To: Author a workflow]({{< ref howto-author-workflow.md >}}).
92+
- [How-To: Manage a workflow]({{< ref howto-manage-workflow.md >}}).
93+
-
94+
- Visit [Python SDK examples](https://github.com/dapr/python-sdk/tree/main/examples/workflow) for code samples and instructions to try out Dapr Workflow:
95+
- [Simple workflow example]({{< ref python-workflow.md >}})
96+
- [Task chaining example](https://github.com/dapr/python-sdk/blob/main/examples/workflow/task_chaining.py)
97+
- [Fan-out/Fan-in example](https://github.com/dapr/python-sdk/blob/main/examples/workflow/fan_out_fan_in.py)
98+
- [Child workflow example](https://github.com/dapr/python-sdk/blob/main/examples/workflow/child_workflow.py)
99+
- [Human approval example](https://github.com/dapr/python-sdk/blob/main/examples/workflow/human_approval.py)
100+
- [Monitor example](https://github.com/dapr/python-sdk/blob/main/examples/workflow/monitor.py)
101+
102+
40103
## Next steps
41104

42105
{{< button text="Getting started with the Dapr Workflow Python SDK" page="python-workflow.md" >}}

0 commit comments

Comments
 (0)