From af27e583da8cf0f8aef294314efec3dcd3f84b0a Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Mon, 26 Jul 2021 20:54:14 +0530 Subject: [PATCH] fixed retry method --- src/services/exportService.ts | 4 ++-- src/services/uploadService.ts | 10 +++++----- src/utils/common/index.ts | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/services/exportService.ts b/src/services/exportService.ts index eadac5873..6f38f7134 100644 --- a/src/services/exportService.ts +++ b/src/services/exportService.ts @@ -1,4 +1,4 @@ -import { retryPromise, runningInBrowser } from 'utils/common'; +import { retryAsyncFunction, runningInBrowser } from 'utils/common'; import { getExportPendingFiles, getExportFailedFiles, getFilesUploadedAfterLastExport, getFileUID, dedupe, getGoogleLikeMetadataFile } from 'utils/export'; import { logError } from 'utils/sentry'; import { getData, LS_KEYS } from 'utils/storage/localStorage'; @@ -254,7 +254,7 @@ class ExportService { const uid = `${file.id}_${this.sanitizeName( file.metadata.title, )}`; - const fileStream = await retryPromise(downloadManager.downloadFile(file)); + const fileStream = await retryAsyncFunction(()=>downloadManager.downloadFile(file)); this.ElectronAPIs.saveStreamToDisk(`${collectionPath}/${uid}`, fileStream); this.ElectronAPIs.saveFileToDisk( `${collectionPath}/${MetadataFolderName}/${uid}.json`, diff --git a/src/services/uploadService.ts b/src/services/uploadService.ts index 1dcb49431..8ecd02cb4 100644 --- a/src/services/uploadService.ts +++ b/src/services/uploadService.ts @@ -4,7 +4,7 @@ import EXIF from 'exif-js'; import { File, fileAttribute } from './fileService'; import { Collection } from './collectionService'; import { FILE_TYPE, SetFiles } from 'pages/gallery'; -import { checkConnectivity, retryPromise, sleep } from 'utils/common'; +import { checkConnectivity, retryAsyncFunction, sleep } from 'utils/common'; import { handleError, parseError, @@ -570,7 +570,7 @@ class UploadService { if (!token) { return; } - const response = await retryPromise(HTTPService.post( + const response = await retryAsyncFunction(()=>HTTPService.post( `${ENDPOINT}/files`, uploadFile, null, @@ -896,7 +896,7 @@ class UploadService { filename: string, ): Promise { try { - await retryPromise( + await retryAsyncFunction(()=> HTTPService.put( fileUploadURL.url, file, @@ -940,7 +940,7 @@ class UploadService { } } const uploadChunk = Uint8Array.from(combinedChunks); - const response = await retryPromise( + const response = await retryAsyncFunction(()=> HTTPService.put( fileUploadURL, uploadChunk, @@ -959,7 +959,7 @@ class UploadService { { CompleteMultipartUpload: { Part: resParts } }, options, ); - await retryPromise( + await retryAsyncFunction(()=> HTTPService.post(multipartUploadURLs.completeURL, body, null, { 'content-type': 'text/xml', }), diff --git a/src/utils/common/index.ts b/src/utils/common/index.ts index 87441885c..dda85b719 100644 --- a/src/utils/common/index.ts +++ b/src/utils/common/index.ts @@ -32,14 +32,14 @@ export function reverseString(title: string) { .reduce((reversedString, currWord) => `${currWord} ${reversedString}`); } -export async function retryPromise(promise: Promise, retryCount: number = 3) { +export async function retryAsyncFunction(func: ()=>Promise, retryCount: number = 3) { try { - const resp = await promise; + const resp = await func(); return resp; } catch (e) { if (retryCount > 0) { await sleep(retrySleepTime[3 - retryCount]); - await retryPromise(promise, retryCount - 1); + await retryAsyncFunction(func, retryCount - 1); } else { throw e; }