Set task timeout (jobs)

By default, each task runs for a maximum of 10 minutes: you can change this to a shorter time or a longer time up to 24 hours.

You set task timeout as described in this page. There is no explicit timeout on a job execution: when all tasks are done, the job execution is done.

In the case of retries, the timeout setting applies to each attempt of a task. If the task attempt does not complete within this time, it will be stopped.

The units specify a duration. You can specify the timeout duration as an integer value in seconds, minutes, or hours. For example, to set the timeout duration as 10 minutes and 5 seconds, specify the value as 605 seconds.

Observing and handling maintenance events

Jobs might periodically undergo maintenance events. During a maintenance event, any in-progress tasks are migrated from the current machine to a different machine. This migration process preserves the entire state of the task. However, there is a brief pause in processing while the task is migrated.

Maintenance events are transparent; you don't have to make any changes to your container to handle maintenance events. Note that every time a task starts and finishes migrating, Cloud Run prints out a log message.

However, if you want to monitor or handle maintenance events in a specific way, you can catch the SIGTSTP signal, which is sent 10 seconds before a task is migrated. After migration, the task receives a SIGCONT signal immediately after the task is restarted.

The following Go sample is a function that catches these signals and prints out a log entry:

func testSignals() {
    sigs := make(chan os.Signal, 1)
    signal.Notify(sigs, syscall.SIGTSTP, syscall.SIGCONT)
    go func() {
        for  {
            sig := <-sigs
            log.Printf("Got Signal: %v", sig)
        }
    }()
 }
 

Required roles

To get the permissions that you need to configure Cloud Run jobs, ask your administrator to grant you the following IAM roles:

For a list of IAM roles and permissions that are associated with Cloud Run, see Cloud Run IAM roles and Cloud Run IAM permissions. If your Cloud Run job interfaces with Google Cloud APIs, such as Cloud Client Libraries, see the service identity configuration guide. For more information about granting roles, see deployment permissions and manage access.

Set task timeout

To specify task timeout for a Cloud Run job:

Console

  1. In the Google Cloud console, go to the Cloud Run jobs page:

    Go to Cloud Run

  2. Click Deploy container and select Job to fill out the initial job settings page. If you are configuring an existing job, select the job, then click Edit.

  3. Click Container, variables and secrets, connections, security to expand the job properties page.

  4. Click the General tab.

    image

    • In the Task timeout field, specify the maximum duration for the job tasks in the current job, and select a Time unit. You can only specify the timeout duration as an integer value in second, minute, or hour. For example, to set a duration of 10 minutes and 5 seconds, in the Task timeout field, specify 605, and select the Time unit as second.
  5. Click Create or Update.

gcloud

  1. For a job you are creating:

    gcloud run jobs create JOB_NAME --image IMAGE_URL --task-timeout TIMEOUT

    Replace

    • JOB_NAME with the name of your job.
    • IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/job:latest.
    • TIMEOUT with the maximum duration for the job tasks, specifying the amount of time and the units: for example, 10m5s is ten minutes and five seconds.
  2. For a job you are updating:

    gcloud run jobs update JOB_NAME --task-timeout TIMEOUT

YAML

  1. If you are creating a new job, skip this step. If you are updating an existing job, download its YAML configuration:

    gcloud run jobs describe JOB_NAME --format export > job.yaml
  2. Update the timeoutSeconds: attribute:

    apiVersion: run.googleapis.com/v1
    kind: Job
    metadata:
      name: JOB
    spec:
      template:
        spec:
          template:
            spec:
              containers:
              - image: IMAGE
              timeoutSeconds: TIMEOUT

    Replace:

    • JOB_NAME with the name of your job.
    • IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/job:latest.
    • TIMEOUT with the maximum duration for the job tasks, specifying the amount of time and the units. You can only specify time as an integer value in seconds, minutes, or hours. For example, to set a duration of 10 minutes and 5 seconds, specify 605s.

    You can also specify more configuration details, such as environment variables or memory limits.

  3. Update the existing job configuration:

    gcloud run jobs replace job.yaml

Terraform

To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.

To specify task timeout for a Cloud Run job, use google_cloud_run_v2_job resource and apply the following snippet to your main.tf file:

resource "google_cloud_run_v2_job" "default" {
  name     = "cloud-run-job-timeout"
  location = "us-central1"

  deletion_protection = false # set to "true" in production

  template {
    template {
      timeout = "3.500s"

      containers {
        image = "us-docker.pkg.dev/cloudrun/container/job:latest"
      }
    }
  }
}

View task timeout settings

To view the current task timeout settings for your Cloud Run job:

Console

  1. In the Google Cloud console, go to the Cloud Run jobs page:

    Go to Cloud Run jobs

  2. Click the job you are interested in to open the Job details page.

  3. Click the Configuration tab.

  4. Locate the task timeout setting in the configuration details.

gcloud

  1. Use the following command:

    gcloud run jobs describe JOB_NAME
  2. Locate the task timeout setting in the returned configuration.