如何最好地处理来自谷歌长期运行的云功能操作的异步响应



我正在使用谷歌云函数(python(,通过调用此处的exportAssets((方法,从GCP启动资产清单导出。该方法返回此处定义的Operations对象,该对象可用于轮询操作,直到操作完成。当然,由于这是一个云功能,我的时间限制在540秒,所以不能永远这样做。google api python客户端提供了add_done_callback((方法,在该方法中可以等待异步响应,但据我所知,它要求我在云函数中保持线程的活动状态。有没有一种方法可以告诉执行操作的资产清单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

云资产库存导出在导出结束时不提供PubSub通知。然而,在我以前的公司,出口10万以上的资产大约需要5分钟;还不错!如果你有更多的资产,我相信你可以联系谷歌云(使用你的客户工程师(在路线图中添加此通知。


无论如何,如果你想建立一个变通方法,你可以使用工作流。

  • 使用云功能触发工作流
  • 在您的工作流程中,
    • 调用云资产API将数据导出到BigQuery
    • 获取响应并执行循环(测试导出作业状态,如果不正常,则睡眠X秒,然后再次测试(
    • 作业结束后,调用PubSub API(或直接调用云函数(提交作业状态并进行处理

最新更新