From 40282ecea2a069800986336b52edcaa393eabffe Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Tue, 8 Mar 2022 12:02:23 -0500 Subject: [PATCH] feat: recover for fail org creation --- src/datalayer/syncService.js | 30 +++++++++++++++++-- .../organizations/organizations.model.js | 23 ++++++++++++-- src/utils/data-assertions.js | 6 ++++ 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/datalayer/syncService.js b/src/datalayer/syncService.js index 88c9a1e6..e755180c 100644 --- a/src/datalayer/syncService.js +++ b/src/datalayer/syncService.js @@ -177,12 +177,16 @@ const getSubscribedStoreData = async ( throw new Error('Max retrys exceeded, Can not subscribe to organization'); } + const timeoutInterval = 60000; + if (!alreadySubscribed) { const response = await subscribeToStoreOnDataLayer(storeId, ip, port); if (!response.success) { log(`Retrying...`, retry + 1); log('...'); - await new Promise((resolve) => setTimeout(() => resolve(), 30000)); + await new Promise((resolve) => + setTimeout(() => resolve(), timeoutInterval), + ); return getSubscribedStoreData(storeId, ip, port, false, retry + 1); } } @@ -192,7 +196,9 @@ const getSubscribedStoreData = async ( if (!storeExistAndIsConfirmed) { log(`Retrying...`, retry + 1); log('...'); - await new Promise((resolve) => setTimeout(() => resolve(), 30000)); + await new Promise((resolve) => + setTimeout(() => resolve(), timeoutInterval), + ); return getSubscribedStoreData(storeId, ip, port, true, retry + 1); } } @@ -209,7 +215,9 @@ const getSubscribedStoreData = async ( if (_.isEmpty(encodedData?.keys_values)) { log(`Retrying...`, retry + 1); log('...'); - await new Promise((resolve) => setTimeout(() => resolve(), 30000)); + await new Promise((resolve) => + setTimeout(() => resolve(), timeoutInterval), + ); return getSubscribedStoreData(storeId, ip, port, true, retry + 1); } @@ -233,6 +241,21 @@ const getRootDiff = (storeId, root1, root2) => { } }; +const getStoreData = async (storeId, callback, onFail, retry = 0) => { + if (retry >= 10) { + log('Waiting for New Organization to be confirmed'); + const encodedData = await dataLayer.getStoreData(storeId); + if (_.isEmpty(encodedData?.keys_values)) { + await new Promise((resolve) => setTimeout(() => resolve(), 60000)); + return getStoreData(storeId, callback, retry + 1); + } else { + callback(encodedData.keys_values); + } + } else { + onFail(); + } +}; + export default { startDataLayerUpdatePolling, syncDataLayerStoreToClimateWarehouse, @@ -241,5 +264,6 @@ export default { getSubscribedStoreData, getRootHistory, getRootDiff, + getStoreData, POLLING_INTERVAL, }; diff --git a/src/models/organizations/organizations.model.js b/src/models/organizations/organizations.model.js index 10943b10..9c13562c 100644 --- a/src/models/organizations/organizations.model.js +++ b/src/models/organizations/organizations.model.js @@ -20,7 +20,7 @@ import ModelTypes from './organizations.modeltypes.cjs'; class Organization extends Model { static async getHomeOrg() { const myOrganization = await Organization.findOne({ - attributes: ['orgUid', 'name', 'icon'], + attributes: ['orgUid', 'name', 'icon', 'subscribed'], where: { isHome: true }, raw: true, }); @@ -59,6 +59,7 @@ class Organization extends Model { const registryVersionId = await datalayer.createDataLayerStore(); const revertOrganizationIfFailed = async () => { + console.log('Reverting Failed Organization'); await Organization.destroy({ where: { orgUid: newOrganizationId } }); }; @@ -86,11 +87,29 @@ class Organization extends Model { orgUid: newOrganizationId, registryId: registryVersionId, isHome: true, - subscribed: true, + subscribed: process.env.USE_SIMULATOR === 'true', name, icon, }); + if (process.env.USE_SIMULATOR !== 'true') { + const onConfirm = () => { + log('Organization confirmed, you are ready to go'); + Organization.update( + { + subscribed: true, + }, + { where: { orgUid: newOrganizationId } }, + ); + }; + + datalayer.getStoreData( + newRegistryId, + onConfirm, + revertOrganizationIfFailed, + ); + } + return newOrganizationId; } diff --git a/src/utils/data-assertions.js b/src/utils/data-assertions.js index e4406d57..135e3e00 100644 --- a/src/utils/data-assertions.js +++ b/src/utils/data-assertions.js @@ -79,6 +79,12 @@ export const assertHomeOrgExists = async () => { ); } + if (!homeOrg.subscribed) { + throw new Error( + `Your Home organization is still confirming, please wait a little longer for it to finished.`, + ); + } + return homeOrg; };