Skip to content

Commit

Permalink
add retries to commit edits (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisRumyantsev authored Sep 26, 2023
1 parent 206e070 commit 44b6dda
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
73 changes: 67 additions & 6 deletions Tasks/GooglePlayReleaseV4/modules/googleutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ export async function addBundle(edits: pub3.Resource$Edits, packageName: string,

try {
tl.debug('Request Parameters: ' + JSON.stringify(requestParameters));
const res = await edits.bundles.upload(requestParameters, { onUploadProgress });
const res = await retryUploadError(edits.bundles, requestParameters, bundleFile) as googleapis.Common.GaxiosResponse<pub3.Schema$Bundle>;
tl.debug('Returned: ' + JSON.stringify(res));
return res.data;
} catch (e) {
tl.debug(`Failed to upload Bundle ${bundleFile}`);
tl.debug(`Failed to upload the bundle "${bundleFile}"`);
tl.debug(e);
throw new Error(tl.loc('CannotUploadBundle', bundleFile, e));
}
Expand All @@ -197,7 +197,7 @@ export async function addApk(edits: pub3.Resource$Edits, packageName: string, ap

try {
tl.debug('Request Parameters: ' + JSON.stringify(requestParameters));
const res = await edits.apks.upload(requestParameters, { onUploadProgress });
const res = await retryUploadError(edits.apks, requestParameters, apkFile) as googleapis.Common.GaxiosResponse<pub3.Schema$Apk>;
tl.debug('Returned: ' + JSON.stringify(res));
return res.data;
} catch (e) {
Expand Down Expand Up @@ -236,7 +236,7 @@ export async function addObb(

try {
tl.debug('Request Parameters: ' + JSON.stringify(requestParameters));
const res = await edits.expansionfiles.upload(requestParameters, { onUploadProgress });
const res = await retryUploadError(edits.expansionfiles, requestParameters, obbFile) as googleapis.Common.GaxiosResponse<pub3.Schema$ExpansionFilesUploadResponse>;
tl.debug('returned: ' + JSON.stringify(res));
return res.data;
} catch (e) {
Expand Down Expand Up @@ -273,7 +273,7 @@ export async function uploadDeobfuscation(

try {
tl.debug('Request Parameters: ' + JSON.stringify(requestParameters));
const res = await edits.deobfuscationfiles.upload(requestParameters, { onUploadProgress });
const res = await retryUploadError(edits.deobfuscationfiles, requestParameters, mappingFilePath) as googleapis.Common.GaxiosResponse<pub3.Schema$DeobfuscationFilesUploadResponse>;
tl.debug('returned: ' + JSON.stringify(res));
return res.data;
} catch (e) {
Expand Down Expand Up @@ -308,14 +308,75 @@ export async function uploadNativeDeobfuscation(

try {
tl.debug(`Request Parameters: ${JSON.stringify(requestParameters)}`);
const res = await edits.deobfuscationfiles.upload(requestParameters, { onUploadProgress });
const res = await retryUploadError(edits.deobfuscationfiles, requestParameters, mappingFilePath) as googleapis.Common.GaxiosResponse<pub3.Schema$DeobfuscationFilesUploadResponse>;
tl.debug(`Response: ${JSON.stringify(res)}`);
return res.data;
} catch (e) {
throw new Error(tl.loc('CannotUploadNativeDeobfuscationFile', mappingFilePath, e));
}
}

/**
* Retries uploading a file to Google Play in case of a known Google API error.
* @param edits - The Google Play edits resource.
* @param requestParameters - The parameters for the upload request.
* @param file - The path of the file to upload.
* @returns A promise that resolves with the response from the upload request.
* @throws An error if the upload fails after the maximum number of attempts.
*/
async function retryUploadError(
edits:
pub3.Resource$Edits$Bundles |
pub3.Resource$Edits$Apks |
pub3.Resource$Edits$Expansionfiles |
pub3.Resource$Edits$Deobfuscationfiles,
requestParameters: pub3.Params$Resource$Edits$Bundles$Upload,
file: string
): Promise<googleapis.Common.GaxiosResponse<
pub3.Schema$Bundle |
pub3.Schema$Apk |
pub3.Schema$ExpansionFilesUploadResponse |
pub3.Schema$DeobfuscationFilesUploadResponse
>> {
let attemptsLeft: number = 7;
let delayInSeconds: number = 2;

while (true) {
try {
// to avoid typescript errors
switch (edits.constructor.name) {
case 'Resource$Edits$Bundles':
return await (edits as pub3.Resource$Edits$Bundles).upload(requestParameters, { onUploadProgress });
case 'Resource$Edits$Apks':
return await (edits as pub3.Resource$Edits$Apks).upload(requestParameters, { onUploadProgress });
case 'Resource$Edits$Expansionfiles':
return await (edits as pub3.Resource$Edits$Expansionfiles).upload(requestParameters, { onUploadProgress });
case 'Resource$Edits$Deobfuscationfiles':
return await (edits as pub3.Resource$Edits$Deobfuscationfiles).upload(requestParameters, { onUploadProgress });
}
} catch (error) {
if (
!error.message.includes(
'Request is missing required authentication credential.'
)
) {
throw error;
}

attemptsLeft--;
tl.debug(`Failed to upload "${file}" due to a known Google API error. Attemps left: ${attemptsLeft}`);

if (attemptsLeft === 0) {
throw error;
}

tl.debug(`Waiting ${delayInSeconds} seconds before retrying.`);
await new Promise(resolve => setTimeout(resolve, delayInSeconds * 1000));
delayInSeconds *= 2;
}
}
}

/**
* Default logger for uploading files
* @param progress progress update from googleapis
Expand Down
2 changes: 1 addition & 1 deletion Tasks/GooglePlayReleaseV4/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"demands": [],
"version": {
"Major": "4",
"Minor": "226",
"Minor": "227",
"Patch": "0"
},
"minimumAgentVersion": "2.182.1",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/GooglePlayReleaseV4/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"demands": [],
"version": {
"Major": "4",
"Minor": "226",
"Minor": "227",
"Patch": "0"
},
"minimumAgentVersion": "2.182.1",
Expand Down
2 changes: 1 addition & 1 deletion vsts-extension-google-play.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifestVersion": 1.0,
"id": "google-play",
"name": "Google Play",
"version": "4.226.0",
"version": "4.227.0",
"publisher": "ms-vsclient",
"description": "Provides tasks for continuous delivery to the Google Play Store from TFS/Team Services build or release definitions",
"categories": [
Expand Down

0 comments on commit 44b6dda

Please sign in to comment.