首页 > 解决方案 > 尝试获取长期运行的 GCP 云功能进程的状态

问题描述

我正在使用每晚运行的云功能将 GCP Firestore 数据库备份到 GCP Cloud Storage。此备份可能需要几分钟才能完成,一旦完成,我想开始另一个过程。为此,我尝试使用 googleapiclient 发现库定期检查备份作业的状态,并使用此处显示的示例。

但是,当我拨打电话时出现错误:

 <HttpError 400 when requesting https://cloudresourcemanager.googleapis.com/v1/operations/ASAyMjE0ODY2MjUyChp0bHVhZmVkBxJsYXJ0bmVjc3Utc2Jvai1uaW1kYRQKLRI?alt=json returned "field [name] has issue [invalid operation name]". Details: "[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'name', 'description': 'invalid operation name'}]}]">

这似乎表明我下面的 op_id 参数格式不正确。虽然我不清楚以何种方式。

我的代码如下所示:

import google.auth
import google.auth.transport.requests
from googleapiclient import discovery

...snipping my backup call logic ...

# get the job-id from from the backup call's response. Format of content.name is:
# projects/my-project-name/databases/*/operations/ASAyMjE0ODY2MjUyChp0bHVhZmVkBxJsYXJ0bmVjc3Utc2Jvai1uaW1kYRQKLRI
op_id = content['name'].split('/')[-1] # should be 'operations/[job-id]'

still_running = True

# check status every 10s
while still_running:
    # get status of the backup job
    service = discovery.build('cloudresourcemanager', 'v1', cache_discovery=False, credentials=creds)
    service_name = 'operations/{}'.format(op_id)
    service_request = service.operations().get(name=service_name)
    service_response = service_request.execute() #error when called 
    
    if service_response.contains('completed'):
        still_running = False
    else:
        sleep(10)
    ...

任何建议或见解表示赞赏。谢谢

标签: pythongoogle-cloud-platformgoogle-cloud-functions

解决方案


原来我应该更仔细地阅读文档。获取 Firestore 作业状态的 HTTP GET 调用如下所示:

https://firestore.googleapis.com/v1/projects/my-project-name/databases/*/operations/ASAwNjM2MjQ5MzUyChp0bHVhZmVkBxJsYXJ0bmVjc3Utc2Jvai1uaW1kYRQKLRI"

我尝试使用我的问题中的 googleapiclient 发现模式,但没有成功。最后我使用了一个简单的 Python 请求 GET 调用。问题解决了。


推荐阅读