Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry logic added in KeyVault task #14135

Merged
merged 3 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions Tasks/AzureKeyVaultV1/operations/azure-arm-keyvault.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import msRestAzure = require('azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-common');
import azureServiceClient = require("azure-pipelines-tasks-azure-arm-rest-v2/AzureServiceClient");
import azureServiceClientBase = require("azure-pipelines-tasks-azure-arm-rest-v2/AzureServiceClientBase");
import util = require("util");
import tl = require('azure-pipelines-task-lib/task');
import webClient = require("azure-pipelines-tasks-azure-arm-rest-v2/webClient");

Expand Down Expand Up @@ -28,21 +29,45 @@ export class KeyVaultClient extends azureServiceClient.ServiceClient {
}

public async invokeRequest(request: webClient.WebRequest): Promise<webClient.WebResponse> {
try {
var response = await this.beginRequest(request);
if (response.statusCode == 401) {
var vaultResourceId = this.getValidVaultResourceId(response);
if(!!vaultResourceId) {
console.log(tl.loc("RetryingWithVaultResourceIdFromResponse", vaultResourceId));

this.getCredentials().activeDirectoryResourceId = vaultResourceId; // update vault resource Id
this.getCredentials().getToken(true); // Refresh authorization token in cache
var response = await this.beginRequest(request);
const maxRetryCount: number = 5;
tauhid621 marked this conversation as resolved.
Show resolved Hide resolved
const retryIntervalInSeconds: number = 2;
const retriableErrorCodes = ["ETIMEDOUT", "ECONNRESET", "ENOTFOUND", "ESOCKETTIMEDOUT", "ECONNREFUSED", "EHOSTUNREACH", "EPIPE", "EA_AGAIN", "EAI_AGAIN"];
const retriableStatusCodes = [408, 409, 500, 502, 503, 504];
let timeToWait: number = retryIntervalInSeconds;
let retryCount: number = 0;

while(true) {
tauhid621 marked this conversation as resolved.
Show resolved Hide resolved
try {
var response = await this.beginRequest(request);
if (response.statusCode == 401) {
var vaultResourceId = this.getValidVaultResourceId(response);
if(!!vaultResourceId) {
console.log(tl.loc("RetryingWithVaultResourceIdFromResponse", vaultResourceId));

this.getCredentials().activeDirectoryResourceId = vaultResourceId; // update vault resource Id
this.getCredentials().getToken(true); // Refresh authorization token in cache
var response = await this.beginRequest(request);
}
}

if (retriableStatusCodes.indexOf(response.statusCode) != -1 && ++retryCount < maxRetryCount) {
tl.debug(util.format("Encountered a retriable status code: %s. Message: '%s'.", response.statusCode, response.statusMessage));
await webClient.sleepFor(timeToWait);
timeToWait = timeToWait * retryIntervalInSeconds + retryIntervalInSeconds;
continue;
}

return response;
} catch(error) {
if (retriableErrorCodes.indexOf(error.code) != -1 && ++retryCount < maxRetryCount) {
tl.debug(util.format("Encountered an error. Will retry. Error:%s. Message: %s.", error.code, error.message));
await webClient.sleepFor(timeToWait);
timeToWait = timeToWait * retryIntervalInSeconds + retryIntervalInSeconds;
}
else {
throw error;
}
}
return response;
} catch(exception) {
throw exception;
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzureKeyVaultV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 1,
"Minor": 181,
"Minor": 182,
"Patch": 0
},
"demands": [],
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzureKeyVaultV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 1,
"Minor": 181,
"Minor": 182,
"Patch": 0
},
"demands": [],
Expand Down