python - 尝试获取长期运行的 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)
...
任何建议或见解表示赞赏。谢谢
解决方案
原来我应该更仔细地阅读文档。获取 Firestore 作业状态的 HTTP GET 调用如下所示:
https://firestore.googleapis.com/v1/projects/my-project-name/databases/*/operations/ASAwNjM2MjQ5MzUyChp0bHVhZmVkBxJsYXJ0bmVjc3Utc2Jvai1uaW1kYRQKLRI"
我尝试使用我的问题中的 googleapiclient 发现模式,但没有成功。最后我使用了一个简单的 Python 请求 GET 调用。问题解决了。
推荐阅读
- sql - 如何相互比较行并根据条件仅保留一行
- computer-vision - 关于通过appsrc将cv::Mat保存到mp4文件的问题
- c++ - 如何解决预编译头错误
- flutter - Flutter Firestore:如何做类似于 where('fieldName', arrayNotContains: 'something') 的事情?
- r - kable 表未正确显示在 github 上
- arrays - 在 AWK 中打印数组中的所有元素
- elasticsearch - 我如何管理弹性搜索中的布尔查询
- python - 来自JSON的电报python检查键值
- javascript - FullCalendar V3 滚动条上弹出更多事件不起作用
- java - 无法创建到 MySQL 数据库服务器的 JDBC 连接