簡體   English   中英

如何最好地使用雲功能處理來自谷歌長期運行操作的異步響應

[英]How best to handle async response from google long running operation with cloud functions

我正在使用 Google Cloud Functions (python) 通過在此處調用 exportAssets() 方法從 GCP 啟動資產清單導出。 該方法返回此處定義的操作 object,可用於輪詢操作,直到操作完成。 當然,因為這是一個雲 function 我被限制在 540 秒,所以不能永遠這樣做。 google api python 客戶端提供了 add_done_callback() 方法,人們可以在其中等待異步響應,但據我所知,它要求我在雲中保持線程處於活動狀態 ZC1C425268E68385D1AB5074F 有沒有辦法告訴資產清單 API 執行操作以將 aync 響應(成功或失敗)發送到我可以正確處理響應的 pubsub 主題? 試圖避免使用 basic_scaling 啟動 appengine 實例以支持 24 小時超時。

    from google.cloud import asset_v1
    # .....
    # Setup request to asset inventory API
    parent = "organizations/{}".format(GCP_ORGANIZATION)
    requested_type = 'RESOURCE'
    dataset = 'projects/{}/datasets/gcp_assets_{}'.format(GCP_PROJECT, requested_type)
    partition_spec = asset_v1.PartitionSpec
    partition_key = asset_v1.PartitionSpec.PartitionKey.REQUEST_TIME
    partition_spec.partition_key = asset_v1.PartitionSpec.PartitionKey.REQUEST_TIME

    output_config = asset_v1.OutputConfig()
    output_config.bigquery_destination.dataset = dataset
    output_config.bigquery_destination.table = 'assets'
    output_config.bigquery_destination.separate_tables_per_asset_type = True
    output_config.bigquery_destination.partition_spec.partition_key = partition_key

    # Make API request to asset inventory API
    print("Creating job to load 'asset types: {}' to {}".format(
        requested_type,
        dataset
    ))
    response = ASSET_CLIENT.export_assets(
        request={
            "parent": parent,
            "content_type": content_type,
            "output_config": output_config,
        }
    )
    print(response.result())  # This waits for the job to complete

Cloud Asset 清單導出不會在導出結束時提供 PubSub 通知。 但是,在我之前的公司中,導出 100k+ 資產大約需要 5 分鍾; 這還不錯,如果你有更多的資產。 我確定您可以聯系 Google Cloud(使用您的客戶工程師)在路線圖中添加此通知。


無論如何,如果你想構建一個解決方法,你可以使用workflows

  • 使用 Cloud Function 觸發您的工作流程
  • 在您的工作流程中,
    • 調用 Cloud Asset API 將數據導出到 BigQuery
    • 獲取響應並執行循環(測試導出作業狀態,如果不正常,睡眠 X 秒並再次測試)
    • 作業結束后,調用 PubSub API(或直接調用 Cloud Function)提交作業狀態並進行處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM