跟踪异步 Azure 操作Track asynchronous Azure operations

某些 Azure REST 操作以异步方式运行,因为操作无法快速完成。Some Azure REST operations run asynchronously because the operation can't be completed quickly. 本文介绍如何通过响应中返回的值跟踪异步操作的状态。This article describes how to track the status of asynchronous operations through values returned in the response.

异步操作的状态代码Status codes for asynchronous operations

最初,异步操作返回下面的一个 HTTP 状态代码:An asynchronous operation initially returns an HTTP status code of either:

  • 201 (已创建)201 (Created)
  • 202 (已接受)202 (Accepted)

该操作在成功完成时返回下面的一个代码:When the operation successfully completes, it returns either:

  • 200 (正常)200 (OK)
  • 204 (无内容)204 (No Content)

请参阅 REST API 文档,了解要执行的操作的响应。Refer to the REST API documentation to see the responses for the operation you're executing.

获取 201 或 202 响应代码后,就可以监视操作状态了。After getting the 201 or 202 response code, you're ready to monitor the status of the operation.

用来监视状态的 URLURL to monitor status

可以通过两种不同的方式监视异步操作的状态。There are two different ways to monitor the status the asynchronous operation. 请通过检查从原始请求返回的标头值来确定正确的方法。You determine the correct approach by examining the header values that are returned from your original request. 首先,查找:First, look for:

  • Azure-AsyncOperation - URL,用于检查操作目前的状态。Azure-AsyncOperation - URL for checking the ongoing status of the operation. 如果操作返回此值,请使用它来跟踪操作的状态。If your operation returns this value, use it to track the status of the operation.
  • Retry-After - 在检查异步操作的状态之前等待的秒数。Retry-After - The number of seconds to wait before checking the status of the asynchronous operation.

如果 Azure-AsyncOperation 不是标头值之一,请查找:If Azure-AsyncOperation isn't one of the header values, then look for:

  • Location - URL,用于确定操作的完成时间。Location - URL for determining when an operation has completed. 只有在未返回 Azure-AsyncOperation 时,才使用此值。Only use this value only when Azure-AsyncOperation isn't returned.
  • Retry-After - 在检查异步操作的状态之前等待的秒数。Retry-After - The number of seconds to wait before checking the status of the asynchronous operation.

Azure-AsyncOperation 请求和响应Azure-AsyncOperation request and response

如果你有一个来自 Azure-AsyncOperation 标头值的 URL,请向该 URL 发送 GET 请求。If you have a URL from the Azure-AsyncOperation header value, send a GET request to that URL. 使用来自 Retry-After 的值计划以何频率检查状态。Use the value from Retry-After to schedule how often to check the status. 你将得到一个指示操作状态的响应对象。You'll get a response object that indicates the status of the operation. 使用 Location URL 检查操作状态时,将返回一个不同的响应。A different response is returned when checking the status of the operation with the Location URL. 若要详细了解来自某个位置 URL 的响应,请参阅创建存储帐户(202,位置和重试间隔)For more information about the response from a location URL, see Create storage account (202 with Location and Retry-After).

响应属性可能会变化,但始终包括异步操作的状态。The response properties can vary but always include the status of the asynchronous operation.

{
    "status": "{status-value}"
}

以下示例显示可能从操作返回的其他值:The following example shows other values that might be returned from the operation:

{
    "id": "{resource path from GET operation}",
    "name": "{operation-id}",
    "status" : "Succeeded | Failed | Canceled | {resource provider values}",
    "startTime": "2017-01-06T20:56:36.002812+00:00",
    "endTime": "2017-01-06T20:56:56.002812+00:00",
    "percentComplete": {double between 0 and 100 },
    "properties": {
        /* Specific resource provider values for successful operations */
    },
    "error" : {
        "code": "{error code}",  
        "message": "{error description}"
    }
}

当状态为“已失败”或“已取消”时,返回错误对象。The error object is returned when the status is Failed or Canceled. 所有其他值都是可选的。All other values are optional. 你收到的响应看起来可能不同于示例。The response you receive may look different than the example.

provisioningState 值provisioningState values

用于创建、更新或删除(PUT、PATCH、DELETE)资源的操作通常返回 provisioningState 值。Operations that create, update, or delete (PUT, PATCH, DELETE) a resource typically return a provisioningState value. 完成操作后,将返回以下三个值之一:When an operation has completed, one of following three values is returned:

  • 已成功Succeeded
  • 失败Failed
  • 已取消Canceled

所有其他值表示该操作仍在运行。All other values indicate the operation is still running. 资源提供程序可以返回自定义的值,用于指示其状态。The resource provider can return a customized value that indicates its state. 例如,当请求已收到且正在运行时,用户会收到“已接受” 。For example, you may receive Accepted when the request is received and running.

示例请求和响应Example requests and responses

启动虚拟机(Azure-AsyncOperation 标头出现 202 响应)Start virtual machine (202 with Azure-AsyncOperation)

此示例演示了如何确定虚拟机的启动操作的状态。This example shows how to determine the status of start operation for virtual machines. 初始请求采用以下格式:The initial request is in the following format:

POST 
https://management.chinacloudapi.cn/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Compute/virtualMachines/{vm-name}/start?api-version=2019-12-01

它返回状态代码 202。It returns status code 202. 在标头值中可以看到:Among the header values, you see:

Azure-AsyncOperation : https://management.chinacloudapi.cn/subscriptions/{subscription-id}/providers/Microsoft.Compute/locations/{region}/operations/{operation-id}?api-version=2019-12-01

若要检查异步操作的状态,请向该 URL 发送另一请求。To check the status of the asynchronous operation, sending another request to that URL.

GET 
https://management.chinacloudapi.cn/subscriptions/{subscription-id}/providers/Microsoft.Compute/locations/{region}/operations/{operation-id}?api-version=2019-12-01

响应正文包含操作的状态:The response body contains the status of the operation:

{
  "startTime": "2017-01-06T18:58:24.7596323+00:00",
  "status": "InProgress",
  "name": "9a062a88-e463-4697-bef2-fe039df73a02"
}

部署资源(Azure-AsyncOperation 标头出现 201 响应)Deploy resources (201 with Azure-AsyncOperation)

此示例演示了将资源部署到 Azure 时,如何确定部署操作的状态。This example shows how to determine the status of deployments operation for deploying resources to Azure. 初始请求采用以下格式:The initial request is in the following format:

PUT
https://management.chinacloudapi.cn/subscriptions/{subscription-id}/resourcegroups/{resource-group}/providers/microsoft.resources/deployments/{deployment-name}?api-version=2020-06-01

它返回状态代码 201。It returns status code 201. 响应的正文包括:The body of the response includes:

"provisioningState":"Accepted",

在标头值中可以看到:Among the header values, you see:

Azure-AsyncOperation: https://management.chinacloudapi.cn/subscriptions/{subscription-id}/resourcegroups/{resource-group}/providers/Microsoft.Resources/deployments/{deployment-name}/operationStatuses/{operation-id}?api-version=2020-06-01

若要检查异步操作的状态,请向该 URL 发送另一请求。To check the status of the asynchronous operation, sending another request to that URL.

GET 
https://management.chinacloudapi.cn/subscriptions/{subscription-id}/resourcegroups/{resource-group}/providers/Microsoft.Resources/deployments/{deployment-name}/operationStatuses/{operation-id}?api-version=2020-06-01

响应正文包含操作的状态:The response body contains the status of the operation:

{
    "status": "Running"
}

部署完成后,响应包含:When the deployment is finished, the response contains:

{
    "status": "Succeeded"
}

创建存储帐户(Location 和 Retry-After 标头出现 202 响应)Create storage account (202 with Location and Retry-After)

此示例演示了如何确定存储帐户的创建操作的状态。This example shows how to determine the status of the create operation for storage accounts. 初始请求采用以下格式:The initial request is in the following format:

PUT
https://management.chinacloudapi.cn/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Storage/storageAccounts/{storage-name}?api-version=2019-06-01

请求正文包含存储帐户的属性:And the request body contains properties for the storage account:

{
    "location": "China East",
    "properties": {},
    "sku": {
        "name": "Standard_LRS"
    },
    "kind": "Storage"
}

它返回状态代码 202。It returns status code 202. 在标头值中会出现以下两个值:Among the header values, you see the following two values:

Location: https://management.chinacloudapi.cn/subscriptions/{subscription-id}/providers/Microsoft.Storage/operations/{operation-id}?monitor=true&api-version=2019-06-01
Retry-After: 17

等待特定的秒数(在 Retry-After 中指定)后,查看异步操作的状态,方法是向该 URL 发送另一请求。After waiting for number of seconds specified in Retry-After, check the status of the asynchronous operation by sending another request to that URL.

GET 
https://management.chinacloudapi.cn/subscriptions/{subscription-id}/providers/Microsoft.Storage/operations/{operation-id}?monitor=true&api-version=2019-06-01

如果请求仍在运行,则会收到状态代码 202。If the request is still running, you receive a status code 202. 如果请求已完成,则会收到状态代码 200,且响应的正文包含已创建存储帐户的属性。If the request has completed, your receive a status code 200, and the body of the response contains the properties of the storage account that has been created.

后续步骤Next steps