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


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

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

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

目標

教學課程內容:

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

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

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用資格。

事前準備

貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 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. Verify 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. Verify 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. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Verify 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 roles/workflows.invoker role to the service account.

      To grant the role, find the Select a role list, then select roles/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. Verify 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 roles/workflows.invoker role to the service account.

      To grant the role, find the Select a role list, then select roles/workflows.invoker.

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

建立及部署子工作流程

子工作流程可以接收及處理來自上層工作流程的資料。子工作流程會執行下列動作來示範這點:

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

控制台

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

    前往「Workflows」頁面

  2. 按一下「建立」

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

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

  5. 選取先前建立的「服務帳戶」

  6. 點選「下一步」

  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 引數。父項工作流程會等待並儲存每個子項工作流程的執行結果。詳情請參閱「工作流程執行 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. 選取先前建立的「服務帳戶」

    6. 點選「下一步」

    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. 在「工作流程詳細資料」頁面中,按一下 「執行」

  4. 再次點按「執行」

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

    結果應類似於以下內容,指出第 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

後續步驟