使用 Eventarc 管理 Dataflow 工作

本文說明如何建立事件驅動型工作流程,在 Dataflow 工作狀態變更時觸發。

舉例來說,工作流程可能:

  • 如果重要工作失敗,請傳送快訊給待命工程師。
  • 在批次工作完成時通知使用者,或啟動其他 Dataflow 工作。
  • 清除作業使用的資源,例如 Cloud Storage bucket。

總覽

Eventarc 是一項 Google Cloud 服務,可監聽其他服務的事件,並將事件轉送至各種目的地。

執行 Dataflow 工作時,工作會經歷各種狀態,例如 JOB_STATE_QUEUEDJOB_STATE_RUNNINGJOB_STATE_DONE。Dataflow 與 Eventarc 整合後,您可以在工作狀態改變時觸發動作。

由於 Eventarc 是代管服務,您不必佈建或管理基礎架構。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Eventarc APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Eventarc APIs.

    Enable the APIs

  8. 如要使用 Eventarc API,專案必須有足夠的配額。此外,與 Eventarc 觸發程序相關聯的服務帳戶必須具備適當權限

    選擇活動目的地

    選擇事件目的地,接收事件。目的地會決定工作流程中的下一個步驟。

    例如:

    • 如要傳送簡訊快訊,您可以使用 Cloud Run 函式建立獨立的 HTTP 觸發條件。
    • 如果是較複雜的工作流程,您可能會使用「工作流程」。
    • 如果 Dataflow 管道是較大型解決方案的一部分,且該解決方案在 Google Kubernetes Engine 上執行,觸發程序可將事件傳送至叢集中執行的 GKE 服務。

    如要進一步瞭解這類架構,請參閱 Eventarc 說明文件中的「事件導向架構」。

    建立觸發條件

    如要為 Dataflow 工作狀態變更建立 Eventarc 觸發條件,請參閱下列其中一個文件:

    您可以選擇依 Dataflow 工作 ID 篩選事件。舉例來說,您可以選取符合規則運算式的工作 ID。詳情請參閱「瞭解路徑模式」。

    處理事件

    事件資料會說明觸發事件時的 Dataflow 工作。酬載與 Job 資源類型類似,但省略了 stepspipeline_descriptiontransform_name_mapping 欄位。此外,視工作狀態而定,部分欄位可能不會顯示。

    以下是酬載範例:

    {
      "id":"2023-04-13_16_28_37-12345678",
      "projectId":"my-project",
      "name":"job1",
      "currentState":"JOB_STATE_QUEUED",
      "currentStateTime":"2023-04-13T23:28:37.437622Z",
      "createTime":"2023-04-13T23:28:37.437622Z",
      "location":"us-central1",
      "startTime":"2023-04-13T23:28:37.437622Z"
    }
    

    如要進一步瞭解工作狀態,請參閱下列主題:

    後續步驟