In the last blog I showed you how to create a Logical SQL Server and create a SQL Database. These two operation ran asynchronously therefore the REST API call finished immediately. In this blog I will explain how we monitor asynchronous operations from the REST API.
Tracking Asynchronous Azure Operations
Some Azure REST operations run asynchronously because the operation cannot be completed quickly and we do not want to tie up request threads. An asynchronous operation initially returns an HTTP status code of either:
- 201 (Created)
- 202 (Accepted)
Within the Response header there are three header values we need to look at:
The first two values above will return URL’s to another REST API call to monitor the operation which is documented here. What is not clear is which one to use. In my testing both values included the same URL however; the documentation states that you first look for Azure-AsyncOperation and it is missing look for a Location value. My theory is that Location is there for backward compatibility reasons.
The Retry-After value is the number of seconds it is suggesting you wait before checking the status. It helps with automation to prevent flooding the servers with checks when the operation is likely to take a long time.
Tracking a Create Logical SQL Server
Using the API request in the previous blog, when the request was sent a status of 202 (Accepted) was returned immediately and the response was:
Let’s look at the Response header. Notice the URL’s are the same. The Retry-After is suggesting we check the response after 1 second.
Let’s run a GET request to the Azure-AsyncOperation URL and check the response. Note: we need to include a bearer token to view this or we will get an authorization failure.
The operation status shows InProgress.
In the header from this request it is now suggesting we wait 20 seconds before trying again.
Let’s try again after 20 seconds. The status now shows Succeeded.
I’ll try the same request and Ill generate a failure so we can see what is returned.