diff --git a/README.md b/README.md index 63486fca..bc985ff6 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ Some examples require extra dependencies. See each sample's directory for specif * [worker_specific_task_queues](worker_specific_task_queues) - Use unique task queues to ensure activities run on specific workers. * [worker_versioning](worker_versioning) - Use the Worker Versioning feature to more easily version your workflows & other code. + ## Test Running the tests requires `poe` to be installed. diff --git a/hello/README.md b/hello/README.md index 191b4621..9e13f544 100644 --- a/hello/README.md +++ b/hello/README.md @@ -41,3 +41,8 @@ Replace `hello_activity.py` in the command with any other example filename to ru * [hello_search_attributes](hello_search_attributes.py) - Start workflow with search attributes then change while running. * [hello_signal](hello_signal.py) - Send signals to a workflow. +* [hello_update](hello_update.py) - Send a request to and a response from a client to a workflow execution. + +Note: To enable the workflow update, set the `frontend.enableUpdateWorkflowExecution` dynamic config value to true. + + temporal server start-dev --dynamic-config-value frontend.enableUpdateWorkflowExecution=true \ No newline at end of file diff --git a/hello/hello_update.py b/hello/hello_update.py new file mode 100644 index 00000000..1b1236d3 --- /dev/null +++ b/hello/hello_update.py @@ -0,0 +1,53 @@ +import asyncio + +from temporalio import workflow +from temporalio.client import Client +from temporalio.worker import Worker + + +@workflow.defn +class GreetingWorkflow: + is_complete = False + + @workflow.run + async def run(self) -> str: + await workflow.wait_condition(lambda: self.is_complete) + return "Hello, World!" + + @workflow.update + async def update_workflow_status(self) -> str: + self.is_complete = True + return "Workflow status updated" + + +async def main(): + client = await Client.connect("localhost:7233") + + # Run a worker for the workflow + async with Worker( + client, + task_queue="update-workflow-task-queue", + workflows=[GreetingWorkflow], + ): + # While the worker is running, use the client to start the workflow. + # Note, in many production setups, the client would be in a completely + # separate process from the worker. + handle = await client.start_workflow( + GreetingWorkflow.run, + id="hello-update-workflow-id", + task_queue="update-workflow-task-queue", + ) + + # Perform the update for GreetingWorkflow + update_result = await handle.execute_update( + GreetingWorkflow.update_workflow_status + ) + print(f"Update Result: {update_result}") + + # Get the result for GreetingWorkflow + result = await handle.result() + print(f"Workflow Result: {result}") + + +if __name__ == "__main__": + asyncio.run(main())