Skip to content
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

Adds Workflow Update to Samples #99

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Some examples require extra dependencies. See each sample's directory for specif
* [sentry](sentry) - Report errors to Sentry.
* [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.
* [workflow_update](workflow_update) - Send a request to and a response from a client to a workflow execution.
rachfop marked this conversation as resolved.
Show resolved Hide resolved

## Test

Expand Down
21 changes: 21 additions & 0 deletions workflow_update/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Workflow Update Sample

This sample shows you how you can end a request to and a response from a client to a workflow execution.

To run, first see [README.md](../README.md) for prerequisites. Then, run the following from this directory to start the
worker:

poetry run python worker.py

This will start the worker. Then, in another terminal, run the following to execute a workflow:

poetry run python starter.py

Which should produce some output like:

Update Result: Workflow status updated
Workflow Result: Hello, World!

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
Empty file added workflow_update/__init__.py
Empty file.
39 changes: 39 additions & 0 deletions workflow_update/starter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import asyncio

from temporalio.client import Client, WorkflowHandle

from workflow_update import HelloWorldWorkflow


class WorkflowRunner:
rachfop marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, client: Client):
self.client = client

async def start_workflow(self) -> WorkflowHandle:
return await self.client.start_workflow(
HelloWorldWorkflow.execute_workflow,
id="hello-world-update-workflow",
task_queue="workflow-update-task-queue",
)


async def run_workflow():
client = await Client.connect("localhost:7233")
runner = WorkflowRunner(client)

# Start the workflow
handle = await runner.start_workflow()

# Perform the update
update_result = await handle.execute_update(
HelloWorldWorkflow.update_workflow_status
)
print(f"Update Result: {update_result}")

# Get the workflow result
result = await handle.result()
print(f"Workflow Result: {result}")


if __name__ == "__main__":
asyncio.run(run_workflow())
26 changes: 26 additions & 0 deletions workflow_update/worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import asyncio

from temporalio.client import Client
from temporalio.worker import Worker

from workflow_update import HelloWorldWorkflow

interrupt_event = asyncio.Event()


async def main():
client = await Client.connect("localhost:7233")

worker = Worker(
client, task_queue="workflow-update-task-queue", workflows=[HelloWorldWorkflow]
)
await worker.run()


if __name__ == "__main__":
loop = asyncio.new_event_loop()
try:
loop.run_until_complete(main())
except KeyboardInterrupt:
interrupt_event.set()
loop.run_until_complete(loop.shutdown_asyncgens())
16 changes: 16 additions & 0 deletions workflow_update/workflow_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from temporalio import workflow


@workflow.defn
class HelloWorldWorkflow:
is_complete = False

@workflow.run
async def execute_workflow(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"
Loading