轮询长时间运行的操作

本页面介绍如何以编程方式轮询由 Service Infrastructure API 中的方法(例如 service.rollouts.create)返回的长时间运行的操作资源,这些操作资源将需要花费大量时间才能完成。操作完成后,done 字段将设置为 true。

如要轮询操作,请以建议的 10 秒退避时间重复调用 operations.get 方法,直到操作完成:

# This is an example of getting a pending `Operation` resource.
gcurl https://servicemanagement.googleapis.com/v1/operations/rollouts.endpointsapis.appspot.com:2016-07-16r2
{
  "name": "operations/rollouts.endpointsapis.appspot.com:2016-07-16r2",
  "metadata": {
    "@type": "type.googleapis.com/google.api.servicemanagement.v1.OperationMetadata",
    "resourceNames": [
      "services/endpointsapis.appspot.com/rollouts/2016-07-16r2"
    ],
    "steps": [
      {
        "description": "update Service Controller",
        "status": "In_PROGRESS"
      }
    ],
    "progressPercentage": 0,
    "startTime": "2016-07-16T00:56:55.737Z",
  }
  "response": {
  "@type": "type.googleapis.com/google.api.servicemanagement.v1.Rollout",
  "rolloutId": "2016-07-16r2",
  "createTime": "2016-07-16T00:56:55.724Z",
  "serviceSelectionStrategy": {
    "serviceConfigIds": [
      "2016-07-16r1"
    ]
  },
  "serviceName": "endpointsapis.appspot.com"
}

# This is an example of getting a done `Operation` resource.
gcurl https://servicemanagement.googleapis.com/v1/operations/rollouts.endpointsapis.appspot.com:2016-07-16r2
{
  "name": "operations/rollouts.endpointsapis.appspot.com:2016-07-16r2",
  "metadata": {
    "@type": "type.googleapis.com/google.api.servicemanagement.v1.OperationMetadata",
    "resourceNames": [
      "services/endpointsapis.appspot.com/rollouts/2016-07-16r2"
    ],
    "steps": [
      {
        "description": "update Service Controller",
        "status": "Done"
      }
    ],
    "progressPercentage": 100,
    "startTime": "2016-07-16T00:56:55.737Z",
  }
  "done": true,
  "response": {
  "@type": "type.googleapis.com/google.api.servicemanagement.v1.Rollout",
  "rolloutId": "2016-07-16r2",
  "createTime": "2016-07-16T00:56:55.724Z",
  "serviceSelectionStrategy": {
    "serviceConfigIds": [
      "2016-07-16r1"
    ]
  },
  "serviceName": "endpointsapis.appspot.com"
}