Skip to content

Commit

Permalink
feat: better timeout logic for failed data layer push
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Mar 6, 2022
1 parent 6199b2c commit 8d9aa40
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const pushChangeListToDataLayer = async (storeId, changelist) => {

return false;
} catch (error) {
console.log(error);
console.log('There was an error pushing your changes to the datalayer');
}
};

Expand Down Expand Up @@ -189,6 +189,7 @@ export const dataLayerAvailable = async () => {
return false;
} catch (error) {
console.error(error);

return false;
}
};
Expand Down
35 changes: 28 additions & 7 deletions src/datalayer/writeService.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import _ from 'lodash';

import * as dataLayer from './persistance';
import wallet from './wallet';
import * as simulator from './simulator';
Expand Down Expand Up @@ -27,14 +29,33 @@ const syncDataLayer = async (storeId, data) => {
await pushChangesWhenStoreIsAvailable(storeId, changeList);
};

const retry = (storeId, changeList) => {
const retry = (storeId, changeList, failedCallback, retryAttempts) => {
console.log('RETRYING...', retryAttempts);
if (retryAttempts >= 10) {
console.log(
'Could not push changelist to datalayer after retrying 10 times',
);
failedCallback();
return;
}

setTimeout(async () => {
console.log('Retrying...', storeId);
await pushChangesWhenStoreIsAvailable(storeId, changeList);
await pushChangesWhenStoreIsAvailable(
storeId,
changeList,
failedCallback,
retryAttempts + 1,
);
}, 30000);
};

const pushChangesWhenStoreIsAvailable = async (storeId, changeList) => {
const pushChangesWhenStoreIsAvailable = async (
storeId,
changeList,
failedCallback = _.noop,
retryAttempts = 0,
) => {
if (process.env.USE_SIMULATOR === 'true') {
return simulator.pushChangeListToDataLayer(storeId, changeList);
} else {
Expand All @@ -50,16 +71,16 @@ const pushChangesWhenStoreIsAvailable = async (storeId, changeList) => {
);

if (!success) {
retry(storeId, changeList);
retry(storeId, changeList, failedCallback, retryAttempts);
}
} else {
retry(storeId, changeList);
retry(storeId, changeList, failedCallback, retryAttempts);
}
}
};

const pushDataLayerChangeList = (storeId, changeList) => {
pushChangesWhenStoreIsAvailable(storeId, changeList);
const pushDataLayerChangeList = (storeId, changeList, failedCallback) => {
pushChangesWhenStoreIsAvailable(storeId, changeList, failedCallback);
};

const dataLayerAvailable = async () => {
Expand Down
7 changes: 7 additions & 0 deletions src/models/staging/staging.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ class Staging extends Model {
await datalayer.pushDataLayerChangeList(
myOrganization.registryId,
finalChangeList,
async () => {
// The push failed so revert the commited staging records.
await Staging.update(
{ commited: false },
{ where: { commited: true } },
);
},
);
}
}
Expand Down

0 comments on commit 8d9aa40

Please sign in to comment.