Skip to content

Commit c6cf076

Browse files
authored
Adds Workflow Update to Samples (temporalio#99)
* Adds Workflow Update to Samples * Move Workflow Update to single file * Update the name of the file and run format
1 parent ecdfcd6 commit c6cf076

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Some examples require extra dependencies. See each sample's directory for specif
6767
* [worker_specific_task_queues](worker_specific_task_queues) - Use unique task queues to ensure activities run on specific workers.
6868
* [worker_versioning](worker_versioning) - Use the Worker Versioning feature to more easily version your workflows & other code.
6969

70+
7071
## Test
7172

7273
Running the tests requires `poe` to be installed.

hello/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,8 @@ Replace `hello_activity.py` in the command with any other example filename to ru
4141
* [hello_search_attributes](hello_search_attributes.py) - Start workflow with search attributes then change while
4242
running.
4343
* [hello_signal](hello_signal.py) - Send signals to a workflow.
44+
* [hello_update](hello_update.py) - Send a request to and a response from a client to a workflow execution.
45+
46+
Note: To enable the workflow update, set the `frontend.enableUpdateWorkflowExecution` dynamic config value to true.
47+
48+
temporal server start-dev --dynamic-config-value frontend.enableUpdateWorkflowExecution=true

hello/hello_update.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import asyncio
2+
3+
from temporalio import workflow
4+
from temporalio.client import Client
5+
from temporalio.worker import Worker
6+
7+
8+
@workflow.defn
9+
class GreetingWorkflow:
10+
is_complete = False
11+
12+
@workflow.run
13+
async def run(self) -> str:
14+
await workflow.wait_condition(lambda: self.is_complete)
15+
return "Hello, World!"
16+
17+
@workflow.update
18+
async def update_workflow_status(self) -> str:
19+
self.is_complete = True
20+
return "Workflow status updated"
21+
22+
23+
async def main():
24+
client = await Client.connect("localhost:7233")
25+
26+
# Run a worker for the workflow
27+
async with Worker(
28+
client,
29+
task_queue="update-workflow-task-queue",
30+
workflows=[GreetingWorkflow],
31+
):
32+
# While the worker is running, use the client to start the workflow.
33+
# Note, in many production setups, the client would be in a completely
34+
# separate process from the worker.
35+
handle = await client.start_workflow(
36+
GreetingWorkflow.run,
37+
id="hello-update-workflow-id",
38+
task_queue="update-workflow-task-queue",
39+
)
40+
41+
# Perform the update for GreetingWorkflow
42+
update_result = await handle.execute_update(
43+
GreetingWorkflow.update_workflow_status
44+
)
45+
print(f"Update Result: {update_result}")
46+
47+
# Get the result for GreetingWorkflow
48+
result = await handle.result()
49+
print(f"Workflow Result: {result}")
50+
51+
52+
if __name__ == "__main__":
53+
asyncio.run(main())

0 commit comments

Comments
 (0)