Skip to content

Commit

Permalink
Adds Workflow Update to Samples (#99)
Browse files Browse the repository at this point in the history
* Adds Workflow Update to Samples

* Move Workflow Update to single file

* Update the name of the file and run format
  • Loading branch information
rachfop authored Dec 14, 2023
1 parent ecdfcd6 commit c6cf076
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions hello/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 53 additions & 0 deletions hello/hello_update.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit c6cf076

Please sign in to comment.