11"""Run a long-lived Temporal worker on a Google Cloud Run worker pool.
22
3- The worker derives its identity and a PINNED Worker Deployment version from
4- Cloud Run instance metadata using
5- ``temporalio.contrib.gcp.cloud_run.get_google_cloud_run_metadata`` and runs
6- until Cloud Run sends SIGTERM (for example, on scale-down).
3+ The worker registers ``temporalio.contrib.gcp.cloud_run.CloudRunPlugin`` on the
4+ client. The plugin reads Cloud Run instance metadata and automatically sets the
5+ client identity and a PINNED Worker Deployment version, then propagates to the
6+ worker. The worker runs until Cloud Run sends SIGTERM (for example, on
7+ scale-down).
78"""
89
910from __future__ import annotations
1415from activities import compose_greeting
1516from settings import load_settings
1617from temporalio .client import Client
17- from temporalio .contrib .gcp .cloud_run import get_google_cloud_run_metadata
18+ from temporalio .contrib .gcp .cloud_run import CloudRunPlugin
1819from temporalio .worker import Worker
1920from workflows import GreetingWorkflow
2021
2122
2223async def main () -> None :
2324 settings = load_settings ()
2425
25- # Reads CLOUD_RUN_WORKER_POOL/CLOUD_RUN_REVISION (worker pools) or
26- # K_SERVICE/K_REVISION (services) and fetches this instance's unique id from
27- # the Cloud Run metadata server. Raises if not running on Cloud Run.
28- metadata = get_google_cloud_run_metadata ()
29-
26+ # The plugin reads CLOUD_RUN_WORKER_POOL/CLOUD_RUN_REVISION (worker pools) or
27+ # K_SERVICE/K_REVISION (services), fetches this instance's unique id from the
28+ # Cloud Run metadata server at connect time, and raises if not running on
29+ # Cloud Run. It sets the client identity to <instance_id>@<revision> (so each
30+ # running container is identifiable) and configures the worker with a
31+ # deployment config that enables Worker Versioning -- deployment name =
32+ # worker-pool name, build id = Cloud Run revision -- with a PINNED default
33+ # versioning behavior. Client plugins propagate to workers automatically.
3034 client = await Client .connect (
3135 settings .address ,
3236 namespace = settings .namespace ,
33- # <instance_id>@<revision>, so each running container is identifiable.
34- identity = metadata .worker_identity ,
37+ plugins = [CloudRunPlugin ()],
3538 api_key = settings .api_key ,
3639 tls = settings .tls ,
3740 )
@@ -41,9 +44,6 @@ async def main() -> None:
4144 task_queue = settings .task_queue ,
4245 workflows = [GreetingWorkflow ],
4346 activities = [compose_greeting ],
44- # Enables Worker Versioning: deployment name = worker-pool name, build
45- # id = Cloud Run revision, with a PINNED default versioning behavior.
46- deployment_config = metadata .worker_deployment_config ,
4747 )
4848
4949 loop = asyncio .get_running_loop ()
@@ -60,12 +60,10 @@ def request_shutdown() -> None:
6060 for signum in (signal .SIGTERM , signal .SIGINT ):
6161 loop .add_signal_handler (signum , request_shutdown )
6262
63- version = metadata .worker_deployment_version
63+ # identity was set by CloudRunPlugin from Cloud Run instance metadata.
6464 print (
6565 "Worker starting "
66- f"identity={ metadata .worker_identity } "
67- f"deployment={ version .deployment_name } "
68- f"build_id={ version .build_id } "
66+ f"identity={ client .identity } "
6967 f"task_queue={ settings .task_queue } " ,
7068 flush = True ,
7169 )
0 commit comments