Delay execution of a job

This page describes how to delay execution of a job. Delayed execution lets you defer non-urgent tasks by up to a maximum duration of 12 hours. When you use this feature your job is provisioned during a low utilization window, in exchange for a lower price. See the pricing page for details.

Specifically:

  • The provisioning of a delayed job execution is potentially up to 12 hours, and the total runtime duration of all tasks in a delayed job execution is 12 hours. In combination, these requirements ensure that a delayed execution will complete within 24 hours.
  • Given the above, the task timeout limit for a delayed job is 12 hours.
  • When a delayed job's execution has reached the maximum duration limit, the execution is cancelled by the system.

Regular jobs versus delayed jobs

Every Cloud Run job is created as either a regular job or a delayed job.

Regardless of how the job was created, you can execute it as the other type. For details see:

Pricing

To learn more about the pricing for Cloud Run delayed jobs, see Cloud Run Pricing.

Required roles

To get the permissions that you need to create 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.

Create a delayed job

By default, when you create and execute a new job, execution starts as soon as possible.

With the delayed jobs feature, you can create a job that has delayed execution built in from the point of creation:

Console

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

    Go to Cloud Run

  2. Select Jobs from the Cloud Run navigation menu, and click Deploy container to display the Create job form.

    1. Fill out the fields in the Create job form, as described in Create jobs.

    2. Select Delay execution to defer execution up to 12 hours.

gcloud

Create the job using the --delay-execution flag:

gcloud beta run jobs create JOB_NAME \
  --image IMAGE_URL \
  --delay-execution \
  --region=REGION

If you created your job as a delayed job, you don't need to include the --delay-execution flag to execute it:

gcloud beta run jobs execute JOB_NAME \
  --region=REGION

YAML

Update your job.yaml with the delayExecution attribute se to true:

apiVersion: run.googleapis.com/v1
kind: Job
metadata:
  name: JOB_NAME
spec:
  delayExecution: "true"

Execute a regular job as a delayed job

To execute a regular job as a delayed job:

Console

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

    Go to Cloud Run

  2. Click the name of the job you want to edit.

  3. Click View & edit job configuration.

  4. Select Delayed execution.

  5. Click Update.

gcloud

Run the following command:

gcloud beta run jobs execute JOB_NAME \
  --async \
  --delay-execution \
  --region=REGION

YAML

Update your job.yaml with the delayExecution attribute set to true:

apiVersion: run.googleapis.com/v1
kind: Job
metadata:
  name: JOB_NAME
spec:
  delayExecution: "true"

Execute a delayed job as a regular job

When you disable delayed execution, the job is updated to be a regular job. This means that the next time you execute this job, it executes as soon as possible.

To disable delayed execution:

Console

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

    Go to Cloud Run

  2. Click the name of the job you want to edit.

  3. Click View & edit job configuration.

  4. Unselect Delayed execution.

  5. Click Update.

gcloud

Use the --no-delay-execution flag. You can also use this flag with the update commands. If you use this flag for a regular job, it updates the job to disable delayed execution.

gcloud beta run jobs update JOB_NAME \
  --image IMAGE_URL \
  --no-delay-execution \
  --region=REGION

YAML

Update your job.yaml with the delayExecution attribute set to false:

apiVersion: run.googleapis.com/v1
kind: Job
metadata:
  name: JOB_NAME
spec:
  delayExecution: "false"

Execute a delayed job on creation or update

When you create a delayed job with the --execute-now flag, it will trigger the execution on creation and the job will run at some point in next 12 hours.

To execute a delayed job upon creation or update:

Console

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

    Go to Cloud Run jobs

  2. If you have an existing delayed job, click the job to display the job details page, then select View & edit job configuration. If you are creating a new delayed job, select Deploy container.

  3. Go to the bottom of the page and check the Execute job immediately box before creating or updating the job.

gcloud

Specify the --execute-now flag when creating or updating a delayed job:

  • Creating a job:

    gcloud run jobs create JOB_NAME \
      --image IMAGE_URL \
      --delay-execution \
      --execute-now \
      --region=REGION
  • Updating a job:

    gcloud run jobs update JOB_NAME \
      --execute-now \
      --region=REGION