|
| 1 | +--- |
| 2 | +title: Status of asynchronous operations |
| 3 | +description: Describes how to track asynchronous operations in Azure. It shows the values you use to get the status of a long-running operation. |
| 4 | +services: event-grid |
| 5 | +author: spelluru |
| 6 | + |
| 7 | +ms.service: event-grid |
| 8 | +ms.topic: conceptual |
| 9 | +ms.date: 04/30/2020 |
| 10 | +ms.author: spelluru |
| 11 | +--- |
| 12 | +# Track asynchronous Azure operations |
| 13 | +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. |
| 14 | + |
| 15 | +## Status codes for asynchronous operations |
| 16 | +An asynchronous operation initially returns an HTTP status code of either: |
| 17 | + |
| 18 | +* 201 (Created) |
| 19 | +* 202 (Accepted) |
| 20 | + |
| 21 | +When the operation successfully completes, it returns either: |
| 22 | + |
| 23 | +* 200 (OK) |
| 24 | +* 204 (No Content) |
| 25 | + |
| 26 | +Refer to the [REST API documentation](/rest/api/) to see the responses for the operation you're executing. |
| 27 | + |
| 28 | +## Monitor status of operation |
| 29 | +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: |
| 30 | + |
| 31 | +* `Azure-AsyncOperation` - URL for checking the ongoing status of the operation. If your operation returns this value, always use it (instead of Location) to track the status of the operation. |
| 32 | +* `Location` - URL for determining when an operation has completed. Use this value only when Azure-AsyncOperation isn't returned. |
| 33 | +* `Retry-After` - The number of seconds to wait before checking the status of the asynchronous operation. |
| 34 | + |
| 35 | +However, not every asynchronous operation returns all these values. For example, you may need to evaluate the Azure-AsyncOperation header value for one operation, and the Location header value for another operation. |
| 36 | + |
| 37 | +You retrieve the header values as you would retrieve any header value for a request. For example, in C#, you retrieve the header value from an `HttpWebResponse` object named `response` with the following code: |
| 38 | + |
| 39 | +```cs |
| 40 | +response.Headers.GetValues("Azure-AsyncOperation").GetValue(0) |
| 41 | +``` |
| 42 | + |
| 43 | +## Azure-AsyncOperation request and response |
| 44 | + |
| 45 | +To get the status of the asynchronous operation, send a GET request to the URL in Azure-AsyncOperation header value. |
| 46 | + |
| 47 | +The body of the response from this operation contains information about the operation. The following example shows the possible values returned from the operation: |
| 48 | + |
| 49 | +```json |
| 50 | +{ |
| 51 | + "id": "{resource path from GET operation}", |
| 52 | + "name": "{operation-id}", |
| 53 | + "status" : "Succeeded | Failed | Canceled | {resource provider values}", |
| 54 | + "startTime": "2017-01-06T20:56:36.002812+00:00", |
| 55 | + "endTime": "2017-01-06T20:56:56.002812+00:00", |
| 56 | + "percentComplete": {double between 0 and 100 }, |
| 57 | + "properties": { |
| 58 | + /* Specific resource provider values for successful operations */ |
| 59 | + }, |
| 60 | + "error" : { |
| 61 | + "code": "{error code}", |
| 62 | + "message": "{error description}" |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +Only `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. |
| 68 | + |
| 69 | +## provisioningState values |
| 70 | + |
| 71 | +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: |
| 72 | + |
| 73 | +* Succeeded |
| 74 | +* Failed |
| 75 | +* Canceled |
| 76 | + |
| 77 | +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. |
| 78 | + |
| 79 | + |
| 80 | +## Example requests and responses |
| 81 | + |
| 82 | +### Start virtual machine (202 with Azure-AsyncOperation) |
| 83 | +This example shows how to determine the status of **start** operation for virtual machines. The initial request is in the following format: |
| 84 | + |
| 85 | +```HTTP |
| 86 | +POST |
| 87 | +https://management.azure.com/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Compute/virtualMachines/{vm-name}/start?api-version=2016-03-30 |
| 88 | +``` |
| 89 | + |
| 90 | +It returns status code 202. Among the header values, you see: |
| 91 | + |
| 92 | +```HTTP |
| 93 | +Azure-AsyncOperation : https://management.azure.com/subscriptions/{subscription-id}/providers/Microsoft.Compute/locations/{region}/operations/{operation-id}?api-version=2016-03-30 |
| 94 | +``` |
| 95 | + |
| 96 | +To check the status of the asynchronous operation, sending another request to that URL. |
| 97 | + |
| 98 | +```HTTP |
| 99 | +GET |
| 100 | +https://management.azure.com/subscriptions/{subscription-id}/providers/Microsoft.Compute/locations/{region}/operations/{operation-id}?api-version=2016-03-30 |
| 101 | +``` |
| 102 | + |
| 103 | +The response body contains the status of the operation: |
| 104 | + |
| 105 | +```json |
| 106 | +{ |
| 107 | + "startTime": "2017-01-06T18:58:24.7596323+00:00", |
| 108 | + "status": "InProgress", |
| 109 | + "name": "9a062a88-e463-4697-bef2-fe039df73a02" |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### Deploy resources (201 with Azure-AsyncOperation) |
| 114 | + |
| 115 | +This example shows how to determine the status of **deployments** operation for deploying resources to Azure. The initial request is in the following format: |
| 116 | + |
| 117 | +```HTTP |
| 118 | +PUT |
| 119 | +https://management.azure.com/subscriptions/{subscription-id}/resourcegroups/{resource-group}/providers/microsoft.resources/deployments/{deployment-name}?api-version=2016-09-01 |
| 120 | +``` |
| 121 | + |
| 122 | +It returns status code 201. The body of the response includes: |
| 123 | + |
| 124 | +```json |
| 125 | +"provisioningState":"Accepted", |
| 126 | +``` |
| 127 | + |
| 128 | +Among the header values, you see: |
| 129 | + |
| 130 | +```HTTP |
| 131 | +Azure-AsyncOperation: https://management.azure.com/subscriptions/{subscription-id}/resourcegroups/{resource-group}/providers/Microsoft.Resources/deployments/{deployment-name}/operationStatuses/{operation-id}?api-version=2016-09-01 |
| 132 | +``` |
| 133 | + |
| 134 | +To check the status of the asynchronous operation, sending another request to that URL. |
| 135 | + |
| 136 | +```HTTP |
| 137 | +GET |
| 138 | +https://management.azure.com/subscriptions/{subscription-id}/resourcegroups/{resource-group}/providers/Microsoft.Resources/deployments/{deployment-name}/operationStatuses/{operation-id}?api-version=2016-09-01 |
| 139 | +``` |
| 140 | + |
| 141 | +The response body contains the status of the operation: |
| 142 | + |
| 143 | +```json |
| 144 | +{"status":"Running"} |
| 145 | +``` |
| 146 | + |
| 147 | +When the deployment is finished, the response contains: |
| 148 | + |
| 149 | +```json |
| 150 | +{"status":"Succeeded"} |
| 151 | +``` |
| 152 | + |
| 153 | +### Create storage account (202 with Location and Retry-After) |
| 154 | + |
| 155 | +This example shows how to determine the status of the **create** operation for storage accounts. The initial request is in the following format: |
| 156 | + |
| 157 | +```HTTP |
| 158 | +PUT |
| 159 | +https://management.azure.com/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Storage/storageAccounts/{storage-name}?api-version=2016-01-01 |
| 160 | +``` |
| 161 | + |
| 162 | +And the request body contains properties for the storage account: |
| 163 | + |
| 164 | +```json |
| 165 | +{ "location": "South Central US", "properties": {}, "sku": { "name": "Standard_LRS" }, "kind": "Storage" } |
| 166 | +``` |
| 167 | + |
| 168 | +It returns status code 202. Among the header values, you see the following two values: |
| 169 | + |
| 170 | +```HTTP |
| 171 | +Location: https://management.azure.com/subscriptions/{subscription-id}/providers/Microsoft.Storage/operations/{operation-id}?monitor=true&api-version=2016-01-01 |
| 172 | +Retry-After: 17 |
| 173 | +``` |
| 174 | + |
| 175 | +After waiting for number of seconds specified in Retry-After, check the status of the asynchronous operation by sending another request to that URL. |
| 176 | + |
| 177 | +```HTTP |
| 178 | +GET |
| 179 | +https://management.azure.com/subscriptions/{subscription-id}/providers/Microsoft.Storage/operations/{operation-id}?monitor=true&api-version=2016-01-01 |
| 180 | +``` |
| 181 | + |
| 182 | +If the request is still running, you receive a status code 202. 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. |
| 183 | + |
| 184 | +## Next steps |
| 185 | + |
| 186 | +* For documentation about each REST operation, see [REST API documentation](/rest/api/). |
| 187 | +* For information about deploying templates through the Resource Manager REST API, see [Deploy resources with Resource Manager templates and Resource Manager REST API](../azure-resource-manager/templates/deploy-rest.md). |
0 commit comments