Skip to content

Commit

Permalink
Add retry mechanism for exceptions (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
onetocny authored Feb 14, 2024
1 parent 3d52ecd commit 3822b8a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
52 changes: 22 additions & 30 deletions common-npm-packages/azure-arm-rest/azure-arm-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,37 +195,29 @@ export class ApplicationTokenCredentials {
}
}

private static initOIDCToken(connection: WebApi, projectId: string, hub: string, planId: string, jobId: string, serviceConnectionId: string, retryCount: number, timeToWait: number): Q.Promise<string> {
var deferred = Q.defer<string>();
connection.getTaskApi().then(
(taskApi: ITaskApi) => {
taskApi.createOidcToken({}, projectId, hub, planId, jobId, serviceConnectionId).then(
(response: TaskAgentInterfaces.TaskHubOidcToken) => {
if (response != null && response.oidcToken != null) {
tl.debug('Got OIDC token');
deferred.resolve(response.oidcToken);
}
else {
if (retryCount < 3) {
let waitedTime = timeToWait;
retryCount += 1;
setTimeout(() => {
deferred.resolve(this.initOIDCToken(connection, projectId, hub, planId, jobId, serviceConnectionId, retryCount, waitedTime));
}, waitedTime);
}
else {
deferred.reject(tl.loc('CouldNotFetchAccessTokenforAAD'));
}
}
},
(error) => {
deferred.reject(tl.loc('CouldNotFetchAccessTokenforAAD') + " " + error);
}
);
private static async initOIDCToken(connection: WebApi, projectId: string, hub: string, planId: string, jobId: string, serviceConnectionId: string, retryCount: number, timeToWait: number): Promise<string> {
let error: any;
for (let i = retryCount > 0 ? retryCount : 3; i > 0; i--) {
try {
const api = await connection.getTaskApi();
const response = await api.createOidcToken({}, projectId, hub, planId, jobId, serviceConnectionId);
if (response && response.oidcToken) {
tl.debug('Got OIDC token');
return response.oidcToken;
}
} catch (e: any) {
error = e;
}
);
await new Promise(r => setTimeout(r, timeToWait));
tl.debug(`Retrying OIDC token fetch. Retries left: ${i}`);
}

return deferred.promise;
let message = tl.loc('CouldNotFetchAccessTokenforAAD');
if (error) {
message += " " + error;
}

return Promise.reject(message);
}

private static getSystemAccessToken() : string {
Expand Down Expand Up @@ -461,7 +453,7 @@ export class ApplicationTokenCredentials {
planId,
jobId,
this.connectedServiceName,
0,
3,
2000);

return oidc_token;
Expand Down
2 changes: 1 addition & 1 deletion common-npm-packages/azure-arm-rest/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common-npm-packages/azure-arm-rest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azure-pipelines-tasks-azure-arm-rest",
"version": "3.232.0",
"version": "3.235.0",
"description": "Common Lib for Azure ARM REST apis",
"repository": {
"type": "git",
Expand Down

0 comments on commit 3822b8a

Please sign in to comment.