Skip to main content

Serverless Workers on GCP Cloud Run - Go SDK

View Markdown

On a GCP Cloud Run worker pool, you run a standard long-lived Temporal Worker. Register Workflows and Activities the same way you would with any other Go Worker, and Temporal Cloud scales the pool up and down as work arrives and drains.

A Cloud Run Worker needs no Cloud Run-specific package. The one addition to a standard Worker is Worker Versioning, which is required for Serverless Workers.

For the end-to-end deployment guide covering the Worker Pool, IAM, and compute configuration, see Deploy a Serverless Worker on GCP Cloud Run.

Create a versioned Worker

Build the Worker as you would any long-running Go Worker, then set DeploymentOptions in worker.Options to declare the Worker Deployment Version and turn versioning on.

The following Worker reads its connection settings and Task Queue from the environment, so the same image can run against any Namespace:

package main

import (
"log"
"os"

"go.temporal.io/sdk/client"
"go.temporal.io/sdk/contrib/envconfig"
"go.temporal.io/sdk/worker"
"go.temporal.io/sdk/workflow"

"example.com/myapp"
)

func main() {
c, err := client.Dial(envconfig.MustLoadDefaultClientOptions())
if err != nil {
log.Fatalln("Unable to create client", err)
}
defer c.Close()

w := worker.New(c, os.Getenv("TEMPORAL_TASK_QUEUE"), worker.Options{
DeploymentOptions: worker.DeploymentOptions{
UseVersioning: true,
Version: worker.WorkerDeploymentVersion{
DeploymentName: "my-app",
BuildID: "build-1",
},
},
})

w.RegisterWorkflowWithOptions(myapp.MyWorkflow, workflow.RegisterOptions{
VersioningBehavior: workflow.VersioningBehaviorPinned,
})
w.RegisterActivity(myapp.MyActivity)

if err := w.Run(worker.InterruptCh()); err != nil {
log.Fatalln("Unable to start worker", err)
}
}

DeploymentName and BuildID together identify the Worker Deployment Version. Both values must match the version you create with temporal worker deployment create-version in the deployment guide, or the Worker polls under a version the WCI does not manage.

Every Workflow needs a versioning behavior, either VersioningBehaviorPinned or VersioningBehaviorAutoUpgrade. Set it per Workflow at registration as shown above, or set DefaultVersioningBehavior in DeploymentOptions to cover every Workflow on the Worker. If a Version is set and neither is specified, registration panics with workflow type does not have a versioning behavior.

For general Worker setup and options that are not specific to Cloud Run, see Run a Worker.

Configure the Temporal connection

The envconfig package loads Temporal Client configuration from environment variables and an optional TOML config file, so the Worker code carries no Namespace or credentials. Set the non-secret values as environment variables on the Worker Pool, and mount the Temporal Cloud API key or TLS material from Secret Manager. For the full list of supported variables, the config file format, and profiles, see Environment configuration.

MustLoadDefaultClientOptions panics if the configuration is invalid. To handle a bad configuration yourself, use envconfig.LoadDefaultClientOptions and check the returned error.

Keep Activities safe across scale-in

The WCI decides when to remove an instance from Task Queue activity, not from what an individual instance is doing. An instance running a long Activity can be stopped mid-execution.

Use Activity Heartbeats so a retry resumes from the last recorded progress instead of starting over:

func MyActivity(ctx context.Context, input MyInput) (string, error) {
for i := range input.Items {
activity.RecordHeartbeat(ctx, i)
// ... process input.Items[i]
}
return "done", nil
}

For how scale-in decisions are made, see Serverless Workers on GCP Cloud Run.

Add observability

A Cloud Run Worker emits the same traces and metrics as a Worker anywhere else. For how to configure metrics export and OpenTelemetry tracing interceptors, see Observability - Go SDK and the SDK metrics reference.