執行同時執行其他工作流程的工作流程


本教學課程將說明如何建立並執行父項工作流程,以便並行執行多個子項工作流程。

在下圖中,系統會叫用四個子工作流程的平行執行作業。這樣一來,父項工作流程就能在平行分支中處理資料,並縮短整體執行時間。父項工作流程會等待所有子項工作流程執行作業完成,然後傳回執行成功和執行失敗的摘要,簡化任何錯誤偵測作業。

父項工作流程叫用子項工作流程的平行疊代

目標

教學課程內容:

  1. 建立及部署子工作流程,以便接收來自父項工作流程的資料。
  2. 建立及部署父項工作流程,以便使用並行 for 迴圈執行多個子項工作流程。
  3. 執行會叫用子工作流程平行執行作業的父項工作流程。
  4. 所有成功和失敗的子工作流程執行作業結果都會儲存在對應項目中,並以對應項目傳回。

您可以在 Google Cloud 主控台中執行下列指令,也可以在終端機或 Cloud Shell 中使用 Google Cloud CLI。

費用

In this document, you use the following billable components of Google Cloud:

To generate a cost estimate based on your projected usage, use the pricing calculator. New Google Cloud users might be eligible for a free trial.

事前準備

貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 Google Cloud 環境中開發應用程式」。

主控台

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  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 Workflow Executions and Workflows APIs.

    Enable the APIs

  5. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Grant the Workflows > Workflows Invoker role to the service account.

      To grant the role, find the Select a role list, then select Workflows > Workflows Invoker.

    6. Click Continue.
    7. Click Done to finish creating the service account.

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

    Go to project selector

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

  8. Enable the Workflow Executions and Workflows APIs.

    Enable the APIs

  9. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Grant the Workflows > Workflows Invoker role to the service account.

      To grant the role, find the Select a role list, then select Workflows > Workflows Invoker.

    6. Click Continue.
    7. Click Done to finish creating the service account.

gcloud

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

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

  7. Enable the Workflow Executions and Workflows APIs:

    gcloud services enable workflowexecutions.googleapis.com<wbr>&nbsp;workflows.googleapis.com
  8. Set up authentication:

    1. Create the service account:

      gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

      Replace SERVICE_ACCOUNT_NAME with a name for the service account.

    2. Grant the roles/workflows.invoker IAM role to the service account:

      gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/workflows.invoker

      Replace the following:

      • SERVICE_ACCOUNT_NAME: the name of the service account
      • PROJECT_ID: the project ID where you created the service account
  9. Install the Google Cloud CLI.

  10. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  11. To initialize the gcloud CLI, run the following command:

    gcloud init
  12. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

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

  14. Enable the Workflow Executions and Workflows APIs:

    gcloud services enable workflowexecutions.googleapis.com<wbr>&nbsp;workflows.googleapis.com
  15. Set up authentication:

    1. Create the service account:

      gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

      Replace SERVICE_ACCOUNT_NAME with a name for the service account.

    2. Grant the roles/workflows.invoker IAM role to the service account:

      gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/workflows.invoker

      Replace the following:

      • SERVICE_ACCOUNT_NAME: the name of the service account
      • PROJECT_ID: the project ID where you created the service account

建立及部署子工作流程

子工作流程可接收並處理來自父項工作流程的資料。子工作流程會透過以下方式示範這項功能:

  • 接收整數做為引數
  • 休眠 10 秒,模擬處理程序
  • 根據整數是奇數或偶數,傳回指標,模擬工作流程執行的成功或失敗

控制台

  1. 前往 Google Cloud 控制台的「Workflows」頁面。

    前往「Workflows」頁面

  2. 按一下「建立」

  3. 輸入新工作流程的名稱 workflow-child

  4. 在「Region」(區域) 清單中選取「us-central1」

  5. 選取先前建立的「Service account」(服務帳戶)

  6. 點按「Next」

  7. 在工作流程編輯器中,輸入工作流程的定義:

    main:
      params: [args]
      steps:
        - init:
            assign:
              - iteration : ${args.iteration}
        - wait:
            call: sys.sleep
            args:
                seconds: 10
        - check_iteration_even_or_odd:
            switch:
              - condition: ${iteration % 2 == 0}
                next: raise_error
        - return_message:
            return: ${"Hello world"+iteration}
        - raise_error:
            raise: ${"Error with iteration "+iteration}
  8. 按一下 [Deploy] (部署)

gcloud

  1. 建立工作流程的原始碼檔案:

    touch workflow-child.yaml
  2. 在文字編輯器中開啟來源程式碼檔案,然後將下列工作流程複製到檔案中。

    main:
      params: [args]
      steps:
        - init:
            assign:
              - iteration : ${args.iteration}
        - wait:
            call: sys.sleep
            args:
                seconds: 10
        - check_iteration_even_or_odd:
            switch:
              - condition: ${iteration % 2 == 0}
                next: raise_error
        - return_message:
            return: ${"Hello world"+iteration}
        - raise_error:
            raise: ${"Error with iteration "+iteration}
  3. 部署工作流程:

    gcloud workflows deploy workflow-child \
        --source=workflow-child.yaml \
        --location=us-central1 \
        --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com

    SERVICE_ACCOUNT_NAME 改成您先前建立的服務帳戶名稱。

建立及部署父項工作流程

父項工作流程會使用平行 for 迴圈執行子項工作流程的多個分支。

  1. 複製工作流程定義的原始碼。其中包含下列部分:

    1. 系統會使用對應表儲存子工作流程執行作業的結果。詳情請參閱「地圖」。

      main:
        steps:
          - init:
              assign:
                - execution_results: {} # results from each execution
                - execution_results.success: {} # successful executions saved under 'success' key
                - execution_results.failure: {} # failed executions saved under 'failure' key
    2. 系統會並行執行 for 迴圈,以便叫用子項工作流程。詳情請參閱「平行步驟」和「迭代」。

      - execute_child_workflows:
          parallel:
            shared: [execution_results]
            for:
              value: iteration
              in: [1, 2, 3, 4]
              steps:
                  - iterate:
    3. 子工作流程會使用連接器叫用。子工作流程的每個疊代都會傳遞 iteration 引數。父項工作流程會等��並儲存每個子項工作流程的執行結果。詳情請參閱「Workflows Executions API 連接器」和「執行階段引數」。

      try:
        steps:
          - execute_child_workflow:
              call: googleapis.workflowexecutions.v1.projects.locations.workflows.executions.run
              args:
                workflow_id: workflow-child
                #location: ...
                #project_id: ...
                argument:
                  iteration: ${iteration}
              result: execution_result
          - save_successful_execution:
              assign:
                - execution_results.success[string(iteration)]: ${execution_result}
      except:
          as: e
          steps:
            - save_failed_execution:
                assign:
                  - execution_results.failure[string(iteration)]: ${e}
    4. 系統會傳回執行結果。詳情請參閱「完成工作流程的執行作業」。

      - return_execution_results:
          return: ${execution_results}
  2. 部署工作流程:

    控制台

    1. 前往 Google Cloud 控制台的「Workflows」頁面:

      前往「Workflows」頁面

    2. 按一下「建立」

    3. 輸入新工作流程的名稱 workflow-parent

    4. 在「Region」(區域) 清單中選取「us-central1」

    5. 選取先前建立的「Service account」(服務帳戶)

    6. 點按「Next」

    7. 在工作流程編輯器中,貼上父項工作流程的定義。

    8. 按一下 [Deploy] (部署)

    gcloud

    1. 建立工作流程的原始碼檔案:

      touch workflow-parent.yaml
    2. 在文字編輯器中開啟原始碼檔案,然後貼上父項工作流程的定義。

    3. 部署工作流程:

      gcloud workflows deploy workflow-parent \
          --source=workflow-parent.yaml \
          --location=us-central1 \
          --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com

      SERVICE_ACCOUNT_NAME 替換為您先前建立的服務帳戶名稱。

執行父項工作流程

執行父項工作流程,讓子項工作流程的叫用作業以平行方式執行。執行作業應會在 10 秒內完成。

控制台

  1. 前往 Google Cloud 控制台的「Workflows」頁面:

    前往「Workflows」頁面

  2. 在「Workflows」頁面中,按一下「workflow-parent」工作流程,前往其詳細資料頁面。

  3. 在「Workflow details」(工作流程詳細資料)頁面中,按一下 「Execute」(執行)

  4. 再次按一下「執行」

  5. 在「Output」窗格中查看工作流程的結果。

    結��應類似於以下內容,指出第 2 和第 4 次迴圈發生錯誤,第 1 和第 3 次迴圈則成功。

    "failure": {
      "2": {
        "message": "Execution failed or cancelled.",
        "operation": {
          "argument": "{\"iteration\":2}",
          "duration": "10.157992541s",
          "endTime": "2023-07-11T13:13:13.028424329Z",
          "error": {
            "context": "RuntimeError: \"Error with iteration 2\"\nin step \"raise_error\", routine \"main\", line: 18",
            "payload": "\"Error with iteration 2\"",
    ...
      "4": {
        "message": "Execution failed or cancelled.",
        "operation": {
          "argument": "{\"iteration\":4}",
          "duration": "10.157929734s",
          "endTime": "2023-07-11T13:13:13.061289142Z",
          "error": {
            "context": "RuntimeError: \"Error with iteration 4\"\nin step \"raise_error\", routine \"main\", line: 18",
            "payload": "\"Error with iteration 4\"",
    ...
    "success": {
      "1": "Hello world1",
      "3": "Hello world3"

gcloud

執行工作流程:

gcloud workflows run workflow-parent \
    --location=us-central1

結果應類似於以下內容,指出第 2 和第 4 次迴圈發生錯誤,第 1 和第 3 次迴圈則成功。

Waiting for execution [06c753e4-6947-4c62-ac0b-2a9d53fb1b8f] to complete...done.
argument: 'null'
duration: 14.065415004s
endTime: '2023-07-11T12:50:43.929023883Z'
name: projects/386837416586/locations/us-central1/workflows/workflow-parent/executions/06c753e4-6947-4c62-ac0b-2a9d53fb1b8f
result: '{"failure":{"2":{"message":"Execution failed or cancelled.","operation":{"argument":"{\"iteration\":2}","duration":"10.143718070s","endTime":"2023-07-11T12:50:40.673209821Z","error":{"context":"RuntimeError:
...
"Error with iteration 2\"\nin step \"raise_error\", routine \"main\", line: 18","payload":"\"Error
...
"Error with iteration 4\"\nin step \"raise_error\", routine \"main\", line: 18","payload":"\"Error
...
"success":{"1":"Hello world1","3":"Hello world3"}}'
startTime: '2023-07-11T12:50:29.863608879Z'
state: SUCCEEDED

您已成功建立及部署工作流程,該工作流程會叫用子項工作流程,在平行分支中執行四次子項工作流程的迭代,並針對每個子項工作流程執行作業傳回成功或失敗的指標。

清除所用資源

如果您是為了這個教學課程建立新專案,請刪除專案。如果您使用現有的專案,且希望保留該專案而不採用本教學課程中新增的變更,請刪除為教學課程建立的資源

刪除專案

如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。

如要刪除專案:

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

刪除教學課程資源

刪除在本教學課程中建立的工作流程:

gcloud workflows delete workflow-child
gcloud workflows delete workflow-parent

後續步驟