|
| 1 | +"""Main application for the worker versioning sample.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import logging |
| 5 | +import uuid |
| 6 | + |
| 7 | +from temporalio.client import Client |
| 8 | + |
| 9 | +from worker_versioning.constants import DEPLOYMENT_NAME, TASK_QUEUE |
| 10 | + |
| 11 | +logging.basicConfig(level=logging.INFO) |
| 12 | + |
| 13 | + |
| 14 | +async def main() -> None: |
| 15 | + client = await Client.connect("localhost:7233") |
| 16 | + |
| 17 | + # Wait for v1 worker and set as current version |
| 18 | + logging.info( |
| 19 | + "Waiting for v1 worker to appear. Run `python worker_versioning/workerv1.py` in another terminal" |
| 20 | + ) |
| 21 | + await wait_for_worker_and_make_current(client, "1.0") |
| 22 | + |
| 23 | + # Start auto-upgrading and pinned workflows |
| 24 | + auto_upgrade_workflow_id = "worker-versioning-versioning-autoupgrade_" + str( |
| 25 | + uuid.uuid4() |
| 26 | + ) |
| 27 | + auto_upgrade_execution = await client.start_workflow( |
| 28 | + "AutoUpgrading", |
| 29 | + id=auto_upgrade_workflow_id, |
| 30 | + task_queue=TASK_QUEUE, |
| 31 | + ) |
| 32 | + |
| 33 | + pinned_workflow_id = "worker-versioning-versioning-pinned_" + str(uuid.uuid4()) |
| 34 | + pinned_execution = await client.start_workflow( |
| 35 | + "Pinned", |
| 36 | + id=pinned_workflow_id, |
| 37 | + task_queue=TASK_QUEUE, |
| 38 | + ) |
| 39 | + |
| 40 | + logging.info("Started auto-upgrading workflow: %s", auto_upgrade_execution.id) |
| 41 | + logging.info("Started pinned workflow: %s", pinned_execution.id) |
| 42 | + |
| 43 | + # Signal both workflows a few times to drive them |
| 44 | + await advance_workflows(auto_upgrade_execution, pinned_execution) |
| 45 | + |
| 46 | + # Now wait for the v1.1 worker to appear and become current |
| 47 | + logging.info( |
| 48 | + "Waiting for v1.1 worker to appear. Run `python worker_versioning/workerv1_1.py` in another terminal" |
| 49 | + ) |
| 50 | + await wait_for_worker_and_make_current(client, "1.1") |
| 51 | + |
| 52 | + # Once it has, we will continue to advance the workflows. |
| 53 | + # The auto-upgrade workflow will now make progress on the new worker, while the pinned one will |
| 54 | + # keep progressing on the old worker. |
| 55 | + await advance_workflows(auto_upgrade_execution, pinned_execution) |
| 56 | + |
| 57 | + # Finally we'll start the v2 worker, and again it'll become the new current version |
| 58 | + logging.info( |
| 59 | + "Waiting for v2 worker to appear. Run `python worker_versioning/workerv2.py` in another terminal" |
| 60 | + ) |
| 61 | + await wait_for_worker_and_make_current(client, "2.0") |
| 62 | + |
| 63 | + # Once it has we'll start one more new workflow, another pinned one, to demonstrate that new |
| 64 | + # pinned workflows start on the current version. |
| 65 | + pinned_workflow_2_id = "worker-versioning-versioning-pinned-2_" + str(uuid.uuid4()) |
| 66 | + pinned_execution_2 = await client.start_workflow( |
| 67 | + "Pinned", |
| 68 | + id=pinned_workflow_2_id, |
| 69 | + task_queue=TASK_QUEUE, |
| 70 | + ) |
| 71 | + logging.info("Started pinned workflow v2: %s", pinned_execution_2.id) |
| 72 | + |
| 73 | + # Now we'll conclude all workflows. You should be able to see in your server UI that the pinned |
| 74 | + # workflow always stayed on 1.0, while the auto-upgrading workflow migrated. |
| 75 | + for handle in [auto_upgrade_execution, pinned_execution, pinned_execution_2]: |
| 76 | + await handle.signal("do_next_signal", "conclude") |
| 77 | + await handle.result() |
| 78 | + |
| 79 | + logging.info("All workflows completed") |
| 80 | + |
| 81 | + |
| 82 | +async def advance_workflows(auto_upgrade_execution, pinned_execution): |
| 83 | + """Signal both workflows a few times to drive them.""" |
| 84 | + for i in range(3): |
| 85 | + await auto_upgrade_execution.signal("do_next_signal", "do-activity") |
| 86 | + await pinned_execution.signal("do_next_signal", "some-signal") |
| 87 | + |
| 88 | + |
| 89 | +async def wait_for_worker_and_make_current(client: Client, build_id: str) -> None: |
| 90 | + import temporalio.api.workflowservice.v1 as wsv1 |
| 91 | + from temporalio.common import WorkerDeploymentVersion |
| 92 | + |
| 93 | + target_version = WorkerDeploymentVersion( |
| 94 | + deployment_name=DEPLOYMENT_NAME, build_id=build_id |
| 95 | + ) |
| 96 | + |
| 97 | + # Wait for the worker to appear |
| 98 | + while True: |
| 99 | + try: |
| 100 | + describe_request = wsv1.DescribeWorkerDeploymentRequest( |
| 101 | + namespace=client.namespace, |
| 102 | + deployment_name=DEPLOYMENT_NAME, |
| 103 | + ) |
| 104 | + response = await client.workflow_service.describe_worker_deployment( |
| 105 | + describe_request |
| 106 | + ) |
| 107 | + |
| 108 | + # Check if our version is present in the version summaries |
| 109 | + for version_summary in response.worker_deployment_info.version_summaries: |
| 110 | + if ( |
| 111 | + version_summary.deployment_version.deployment_name |
| 112 | + == target_version.deployment_name |
| 113 | + and version_summary.deployment_version.build_id |
| 114 | + == target_version.build_id |
| 115 | + ): |
| 116 | + break |
| 117 | + else: |
| 118 | + await asyncio.sleep(1) |
| 119 | + continue |
| 120 | + |
| 121 | + break |
| 122 | + |
| 123 | + except Exception: |
| 124 | + await asyncio.sleep(1) |
| 125 | + continue |
| 126 | + |
| 127 | + # Once the version is available, set it as current |
| 128 | + set_request = wsv1.SetWorkerDeploymentCurrentVersionRequest( |
| 129 | + namespace=client.namespace, |
| 130 | + deployment_name=DEPLOYMENT_NAME, |
| 131 | + build_id=target_version.build_id, |
| 132 | + ) |
| 133 | + await client.workflow_service.set_worker_deployment_current_version(set_request) |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + asyncio.run(main()) |
0 commit comments