Skip to content

Commit

Permalink
feat: recover for fail org creation
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Mar 8, 2022
1 parent e1bd45c commit 40282ec
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
30 changes: 27 additions & 3 deletions src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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);
}

Expand All @@ -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,
Expand All @@ -241,5 +264,6 @@ export default {
getSubscribedStoreData,
getRootHistory,
getRootDiff,
getStoreData,
POLLING_INTERVAL,
};
23 changes: 21 additions & 2 deletions src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down Expand Up @@ -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 } });
};

Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions src/utils/data-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down

0 comments on commit 40282ec

Please sign in to comment.