編寫元件來顯示 Google Cloud 控制台連結
執行元件時,您通常不僅想查看啟動的元件工作連結,也想查看基礎雲端資源的連結,例如 Vertex 批次預測工作或 Dataflow 工作。
gcp_resource
proto 是一種特殊參數,您可以在元件中使用,讓 Google Cloud 控制台在 Vertex AI Pipelines 控制台中提供資源記錄和狀態的自訂檢視畫面。
輸出 gcp_resource
參數
使用容器型元件
首先,您需要在元件中定義 gcp_resource
參數,如下列範例 component.py
檔案所示:
Python
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Python API 參考說明文件。
接著,在容器中安裝 Google Cloud Pipeline Components 套件:
pip install --upgrade google-cloud-pipeline-components
接著,在 Python 程式碼中,將資源定義為 gcp_resource
參數:
Python
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Python API 參考說明文件。
from google_cloud_pipeline_components.proto.gcp_resources_pb2 import GcpResources
from google.protobuf.json_format import MessageToJson
dataflow_resources = GcpResources()
dr = dataflow_resources.resources.add()
dr.resource_type='DataflowJob'
dr.resource_uri='https://dataflow.googleapis.com/v1b3/projects/[your-project]/locations/us-east1/jobs/[dataflow-job-id]'
with open(gcp_resources, 'w') as f:
f.write(MessageToJson(dataflow_resources))
使用 Python 元件
或者,您也可以像處理任何字串輸出參數一樣,傳回 gcp_resources
輸出參數:
@dsl.component(
base_image='python:3.9',
packages_to_install=['google-cloud-pipeline-components==2.19.0'],
)
def launch_dataflow_component(project: str, location:str) -> NamedTuple("Outputs", [("gcp_resources", str)]):
# Launch the dataflow job
dataflow_job_id = [dataflow-id]
dataflow_resources = GcpResources()
dr = dataflow_resources.resources.add()
dr.resource_type='DataflowJob'
dr.resource_uri=f'https://dataflow.googleapis.com/v1b3/projects/{project}/locations/{location}/jobs/{dataflow_job_id}'
gcp_resources=MessageToJson(dataflow_resources)
return gcp_resources
支援的 resource_type
值
您可以將 resource_type
設為任意字串,但只有下列類型會在 Google Cloud 控制台中顯示連結:
- BatchPredictionJob
- BigQueryJob
- CustomJob
- DataflowJob
- HyperparameterTuningJob
撰寫元件來取消基礎資源
取消管道工作時,預設行為是讓基礎 Google Cloud 資源繼續執行。系統不會自動取消。如要變更這項行為,請將 SIGTERM 處理常式附加至管道工作。如果工作可能長時間執行,建議您在輪詢迴圈之前執行這項操作。
取消作業已在多個 Google Cloud 管道元件中實作,包括:
- 批次預測工作
- BigQuery ML 工作
- 自訂工作
- 無伺服器型 Dataproc 批次工作
- 超參數微調工作
如要瞭解詳情 (包括如何附加 SIGTERM 處理常式的程式碼範例),請參閱下列 GitHub 連結:
- https://github.com/kubeflow/pipelines/blob/google-cloud-pipeline-components-2.19.0/components/google-cloud/google_cloud_pipeline_components/container/utils/execution_context.py
- https://github.com/kubeflow/pipelines/blob/google-cloud-pipeline-components-2.19.0/components/google-cloud/google_cloud_pipeline_components/container/v1/gcp_launcher/job_remote_runner.py#L124
實作 SIGTERM 處理常式時,請考量下列事項:
- 元件執行幾分鐘後,取消傳播功能才會生效。這是因為在呼叫 Python 信號處理常式之前,需要處理背景啟動工作。
- 部分 Google Cloud 資源可能未實作取消功能。舉例來說,建立或刪除 Vertex AI 端點或模型可能會建立長期執行的作業,該作業會透過 REST API 接受取消要求,但不會實作取消作業本身。