From 7da1c6ed1c3e35a131ce40cf02b522ec350eb683 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Fri, 12 Jan 2024 14:08:56 -0500 Subject: [PATCH] fix: optimize retry logic of subscribe --- src/datalayer/syncService.js | 45 ++----------------- .../organizations/organizations.model.js | 23 +++++----- 2 files changed, 15 insertions(+), 53 deletions(-) diff --git a/src/datalayer/syncService.js b/src/datalayer/syncService.js index a0312943..2db1e52e 100644 --- a/src/datalayer/syncService.js +++ b/src/datalayer/syncService.js @@ -26,15 +26,7 @@ const subscribeToStoreOnDataLayer = async (storeId) => { } }; -const getSubscribedStoreData = async (storeId, retry = 0) => { - if (retry >= 60) { - throw new Error( - `Max retrys exceeded while trying to subscribe to ${storeId}, Can not subscribe to organization`, - ); - } - - const timeoutInterval = 30000; - +const getSubscribedStoreData = async (storeId) => { const subscriptions = await dataLayer.getSubscriptions(storeId); const alreadySubscribed = subscriptions.includes(storeId); @@ -43,20 +35,7 @@ const getSubscribedStoreData = async (storeId, retry = 0) => { const response = await subscribeToStoreOnDataLayer(storeId); if (!response || !response.success) { - if (!response) { - logger.info( - `Response from subscribe RPC came back undefined, is your datalayer running?`, - ); - } - logger.info( - `Retrying subscribe to ${storeId}, subscribe failed`, - retry + 1, - ); - logger.info('...'); - await new Promise((resolve) => - setTimeout(() => resolve(), timeoutInterval), - ); - return getSubscribedStoreData(storeId, retry + 1); + throw new Error(`Failed to subscribe to ${storeId}`); } } @@ -67,15 +46,7 @@ const getSubscribedStoreData = async (storeId, retry = 0) => { const storeExistAndIsConfirmed = await dataLayer.getRoot(storeId, true); logger.info(`Store found in DataLayer: ${storeId}.`); if (!storeExistAndIsConfirmed) { - logger.info( - `Retrying subscribe to ${storeId}, store not yet confirmed.`, - retry + 1, - ); - logger.info('...'); - await new Promise((resolve) => - setTimeout(() => resolve(), timeoutInterval), - ); - return getSubscribedStoreData(storeId, retry + 1); + throw new Error(`Store not found in DataLayer: ${storeId}.`); } else { logger.debug(`Store is confirmed, proceeding to get data ${storeId}`); } @@ -89,15 +60,7 @@ const getSubscribedStoreData = async (storeId, retry = 0) => { } if (_.isEmpty(encodedData?.keys_values)) { - logger.info( - `Retrying subscribe to ${storeId}, No data detected in store.`, - retry + 1, - ); - logger.info('...'); - await new Promise((resolve) => - setTimeout(() => resolve(), timeoutInterval), - ); - return getSubscribedStoreData(storeId, retry + 1); + throw new Error(`No data found for store ${storeId}`); } const decodedData = decodeDataLayerResponse(encodedData); diff --git a/src/models/organizations/organizations.model.js b/src/models/organizations/organizations.model.js index 3da9d064..e8207528 100644 --- a/src/models/organizations/organizations.model.js +++ b/src/models/organizations/organizations.model.js @@ -273,7 +273,7 @@ class Organization extends Model { const orgData = await datalayer.getSubscribedStoreData(orgUid); if (!orgData.registryId) { - throw new Error( + logger.error( 'Currupted organization, no registryId on the datalayer, can not import', ); } @@ -419,17 +419,16 @@ class Organization extends Model { ); } - await Promise.all( - defaultOrgs.map(async (org) => { - const exists = await Organization.findOne({ - where: { orgUid: org.orgUid }, - }); - - if (!exists) { - Organization.importOrganization(org.orgUid); - } - }), - ); + for (let i = 0; i < defaultOrgs.length; i++) { + const org = defaultOrgs[i]; + const exists = await Organization.findOne({ + where: { orgUid: org.orgUid }, + }); + + if (!exists) { + await Organization.importOrganization(org.orgUid); + } + } } catch (error) { logger.info(error); }