Create and run a job that sends Pub/Sub status notifications

This document explains how to create a Batch job that sends Pub/Sub notifications. You can use Pub/Sub to get notifications when job or task state changes, or when a job or task enters a specific state. For more information, see Monitor jobs using notifications.

Before you begin

Create and run a job that sends notifications

You can create a Batch job that sends Pub/Sub notifications by doing the following:

Use the Google Cloud CLI or REST API to create a job that includes the notifications field and one or more jobNotification objects in the main body of the JSON file:

  "notifications": [
    {
      "pubsubTopic": "projects/PROJECT_ID/topics/TOPIC_ID",
      "message": {
        ATTRIBUTES
      }
    },
  ]

Replace the following:

  • PROJECT_ID: the project ID of the project that contains the Pub/Sub topic.
  • TOPIC_ID: the Pub/Sub topic ID of the topic you created when you enabled Pub/Sub notifications.
  • ATTRIBUTES: the attributes you specify depends on whether you want to receive notifications about a job or all tasks in the job:

    • For notifications about all job state changes, specify the following:

      "type": "JOB_STATE_CHANGED"
      
    • For notifications about a specific job state change, specify the following:

      "type": "JOB_STATE_CHANGED",
      "newJobState": "JOB_STATE"
      

      Replace JOB_STATE with one of the following job states:

      • QUEUED
      • SCHEDULED
      • RUNNING
      • SUCCEEDED
      • FAILED

      For more information about job states, see Job lifecycle.

    • For notifications about all task state changes, specify the following:

      "type": "TASK_STATE_CHANGED"
      
    • For notifications about specific task state changes, specify the following:

      "type": "TASK_STATE_CHANGED",
      "newTaskState": "TASK_STATE"
      

      Replace TASK_STATE with one of the following task states:

      • PENDING
      • ASSIGNED
      • RUNNING
      • SUCCEEDED
      • FAILED

      For more information about task states, see Job lifecycle.

For example, suppose you want to receive notifications about all job state changes and any time a task fails. To do so, you would add the following to your job's configuration JSON file:

  "notifications": [
    {
      "pubsubTopic": "projects/example-projecet/topics/example-topic",
      "message": {
        "type": "JOB_STATE_CHANGED"
      }
    },
    {
      "pubsubTopic": "projects/example-project/topics/example-topic",
      "message": {
        "type": "TASK_STATE_CHANGED",
        "newTaskState": "FAILED"
      }
    }
  ]

What's next