本教學課程說明如何建立及執行父項工作流程,並行執行多個子項工作流程。
在下圖中,系統會叫用四個並行執行的子工作流程。這樣一來,父項工作流程就能在平行分支中處理資料,縮短整體執行時間。父項工作流程會等待所有子項工作流程執行作業完成,然後傳回成功和失敗執行作業的摘要,簡化任何錯誤偵測作業。
目標
教學課程內容:
- 建立及部署子工作流程,接收來自父項工作流程的資料。
- 建立及部署上層工作流程,使用平行
for
迴圈執行多個下層工作流程。 - 執行父項工作流程,並同時叫用子項工作流程。
- 所有成功和失敗的子工作流程執行結果都會儲存並以對應項目形式傳回。
您可以在 Google Cloud 控制台中執行下列指令,也可以在終端機或 Cloud Shell 中使用 Google Cloud CLI 執行。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
如要根據預測用量估算費用,請使用 Pricing Calculator。
事前準備
貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 Google Cloud 環境中開發應用程式」。
主控台
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Workflow Executions and Workflows APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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
. - Click Create and continue.
-
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.
- Click Continue.
-
Click Done to finish creating the service account.
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Workflow Executions and Workflows APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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
. - Click Create and continue.
-
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.
- Click Continue.
-
Click Done to finish creating the service account.
-
gcloud
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Workflow Executions and Workflows APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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
. - Click Create and continue.
-
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.
- Click Continue.
-
Click Done to finish creating the service account.
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Workflow Executions and Workflows APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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
. - Click Create and continue.
-
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.
- Click Continue.
-
Click Done to finish creating the service account.
-
建立及部署子工作流程
子工作流程可以接收及處理來自上層工作流程的資料。子工作流程會執行下列動作來示範這點:
- 以整數做為引數
- 休眠 10 秒,模擬部分處理程序
傳回指標 (根據整數是奇數還是偶數),模擬工作流程執行成功或失敗
控制台
前往 Google Cloud 控制台的「Workflows」頁面。
按一下「建立」
。輸入新工作流程的名稱,例如
workflow-child
。在「Region」(區域) 清單中選取「us-central1」。
選取先前建立的「服務帳戶」。
點選「下一步」。
在工作流程編輯器中,輸入下列工作流程定義:
按一下 [Deploy] (部署)。
gcloud
為工作流程建立原始碼檔案:
touch workflow-child.yaml
在文字編輯器中開啟原始碼檔案,然後將下列工作流程複製到檔案中。
部署工作流程:
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
迴圈,執行子工作流程的多個分支。
複製工作流程定義的原始碼。其中包含下列部分:
地圖用於儲存子工作流程的執行結果。詳情請參閱「地圖」。
子工作流程是透過連接器叫用。子工作流程的每次疊代都會傳遞
iteration
引數。父項工作流程會等待並儲存每個子項工作流程的執行結果。詳情請參閱「工作流程執行 API 連接器」和「執行階段引數」。系統會傳回執行結果。詳情請參閱「完成工作流程的執行作業」。
部署工作流程:
控制台
前往 Google Cloud 控制台的「Workflows」頁面:
按一下「建立」
。輸入新工作流程的名稱,例如
workflow-parent
。在「Region」(區域) 清單中選取「us-central1」。
選取先前建立的「服務帳戶」。
點選「下一步」。
在工作流程編輯器中,貼上父項工作流程的定義。
按一下 [Deploy] (部署)。
gcloud
為工作流程建立原始碼檔案:
touch workflow-parent.yaml
在文字編輯器中開啟原始碼檔案,然後貼上父項工作流程的定義。
部署工作流程:
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 秒。
控制台
前往 Google Cloud 控制台的「Workflows」頁面:
在「Workflows」頁面中,按一下 workflow-parent 工作流程,前往詳細資料頁面。
在「工作流程詳細資料」頁面中,按一下 play_arrow「執行」。
再次點按「執行」。
在「輸出」窗格中查看工作流程結果。
結果應類似於以下內容,指出第 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
您已成功建立及部署工作流程,該工作流程會叫用子項工作流程、在平行分支中執行四次子項工作流程,並傳回每次子項工作流程執行作業的成功或失敗指標。
清除所用資源
如果您是為了這個教學課程建立新專案,請刪除專案。如果您使用現有專案,並想保留專案,但不要在本教學課程中新增的變更,請刪除為本教學課程建立的資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
如要刪除專案:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- 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
後續步驟
- 如要進一步瞭解 Workflows 語法,請參閱 Workflows 語法參考資料。
- 如要進一步瞭解 Workflows 連接器,請參閱「瞭解連接器」一文。