本教學課程將說明如何建立並執行父項工作流程,以便並行執行多個子項工作流程。
在下圖中,系統會叫用四個子工作流程的平行執行作業。這樣一來,父項工作流程就能在平行分支中處理資料,並縮短整體執行時間。父項工作流程會等待所有子項工作流程執行作業完成,然後傳回執行成功和執行失敗的摘要,簡化任何錯誤偵測作業。
目標
教學課程內容:
- 建立及部署子工作流程,以便接收來自父項工作流程的資料。
- 建立及部署父項工作流程,以便使用並行
for
迴圈執行多個子項工作流程。 - 執行會叫用子工作流程平行執行作業的父項工作流程。
- 所有成功和失敗的子工作流程執行作業結果都會儲存在對應項目中,並以對應項目傳回。
您可以在 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.
事前準備
貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 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.
-
Make sure 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.
-
Make sure 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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Workflow Executions and Workflows APIs:
gcloud services enable workflowexecutions.googleapis.com<wbr> workflows.googleapis.com
-
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account. -
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 accountPROJECT_ID
: the project ID where you created the service account
-
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Workflow Executions and Workflows APIs:
gcloud services enable workflowexecutions.googleapis.com<wbr> workflows.googleapis.com
-
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account. -
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 accountPROJECT_ID
: the project ID where you created the service account
-
建立及部署子工作流程
子工作流程可接收並處理來自父項工作流程的資料。子工作流程會透過以下方式示範這項功能:
- 接收整數做為引數
- 休眠 10 秒,模擬處理程序
根據整數是奇數或偶數,傳回指標,模擬工作流程執行的成功或失敗
控制台
前往 Google Cloud 控制台的「Workflows」頁面。
按一下「建立」。
輸入新工作流程的名稱
workflow-child
。在「Region」(區域) 清單中選取「us-central1」。
選取先前建立的「Service account」(服務帳戶)。
點按「Next」。
在工作流程編輯器中,輸入工作流程的定義:
按一下 [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
引數。父項工作流程會等��並儲存每個子項工作流程的執行結果。詳情請參閱「Workflows Executions API 連接器」和「執行階段引數」。系統會傳回執行結果。詳情請參閱「完成工作流程的執行作業」。
部署工作流程:
控制台
前往 Google Cloud 控制台的「Workflows」頁面:
按一下「建立」。
輸入新工作流程的名稱
workflow-parent
。在「Region」(區域) 清單中選取「us-central1」。
選取先前建立的「Service account」(服務帳戶)。
點按「Next」。
在工作流程編輯器中,貼上父項工作流程的定義。
按一下 [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」工作流程,前往其詳細資料頁面。
在「Workflow details」(工作流程詳細資料)頁面中,按一下 play_arrow「Execute」(執行)。
再次按一下「執行」。
在「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
您已成功建立及部署工作流程,該工作流程會叫用子項工作流程,在平行分支中執行四次子項工作流程的迭代,並針對每個子項工作流程執行作業傳回成功或失敗的指標。
清除所用資源
如果您是為了這個教學課程建立新專案,請刪除專案。如果您使用現有的專案,且希望保留該專案而不採用本教學課程中新增的變更,請刪除為教學課程建立的資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
如要刪除專案:
- 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 連接器,請參閱「瞭解連接器」。