跟踪异步 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.

监视操作状态Monitor status of operation

异步 REST 操作返回标头值,用于确定操作的状态。The asynchronous REST operations return header values, which you use to determine the status of the operation. 可能有三个需要检查的标头值:There are potentially three header values to examine:

  • Azure-AsyncOperation - URL,用于检查操作目前的状态。Azure-AsyncOperation - URL for checking the ongoing status of the operation. 如果操作返回此值,则应始终使用它(而不是 Location)来跟踪操作的状态。If your operation returns this value, always use it (instead of Location) to track the status of the operation.
  • Location - URL,用于确定操作的完成时间。Location - URL for determining when an operation has completed. 只有在未返回 Azure-AsyncOperation 时,才使用此值。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.

但是,并非每个异步操作都会返回所有这些值。However, not every asynchronous operation returns all these values. 例如,可能需要对一个操作计算 Azure-AsyncOperation 标头值,对另一个操作计算 Location 标头值。For example, you may need to evaluate the Azure-AsyncOperation header value for one operation, and the Location header value for another operation.

检索这些标头值与检索请求的任何标头值一样。You retrieve the header values as you would retrieve any header value for a request. 例如,在 C# 中,可以使用以下代码从名为 responseHttpWebResponse 对象检索标头值:For example, in C#, you retrieve the header value from an HttpWebResponse object named response with the following code:

response.Headers.GetValues("Azure-AsyncOperation").GetValue(0)

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

若要获取异步操作的状态,请向 Azure-AsyncOperation 标头值中的 URL 发送 GET 请求。To get the status of the asynchronous operation, send a GET request to the URL in Azure-AsyncOperation header value.

来自此操作的响应的正文包含有关操作的信息。The body of the response from this operation contains information about the operation. 以下示例显示从操作中返回的可能值:The following example shows the possible values 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}" 
    }
}

所有响应都只返回 statusOnly status is returned for all responses. 当状态为“已失败”或“已取消”时,返回错误对象。The error object is returned when the status is Failed or Canceled. 所有其他值都是可选的;因此,收到的响应看起来可能不同于示例。All other values are optional; therefore, 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=2016-03-30

它返回状态代码 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=2016-03-30

若要检查异步操作的状态,请向该 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=2016-03-30

响应正文包含操作的状态: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=2016-09-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=2016-09-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=2016-09-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=2016-01-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=2016-01-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=2016-01-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