Skip to content

Commit

Permalink
fix: optimize retry logic of subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Jan 12, 2024
1 parent a19be01 commit 7da1c6e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 53 deletions.
45 changes: 4 additions & 41 deletions src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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}`);
}
}

Expand All @@ -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}`);
}
Expand All @@ -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);
Expand Down
23 changes: 11 additions & 12 deletions src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 7da1c6e

Please sign in to comment.