Skip to content

Commit

Permalink
feat: add logging to subsciption process
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Sep 1, 2022
1 parent 2200504 commit 04c88c4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
21 changes: 13 additions & 8 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,15 @@ const unsubscribeFromDataLayerStore = async (storeId) => {

const subscribeToStoreOnDataLayer = async (storeId) => {
if (!storeId) {
logger.info(`No storeId found to subscribe to: ${storeId}`);
return false;
}

const subscriptions = await getSubscriptions(storeId);
const subscriptions = await getSubscriptions();

if (subscriptions.includes(storeId)) {
return true;
logger.info(`Already subscribed to: ${storeId}`);
return { success: true };
}

const options = {
Expand Down Expand Up @@ -282,7 +284,7 @@ const subscribeToStoreOnDataLayer = async (storeId) => {
`http://${await publicIpv4()}:${chiaConfig.data_layer.host_port}`,
);

return true;
return data;
}

return false;
Expand Down Expand Up @@ -433,12 +435,14 @@ const removeMirror = async (storeId, coinId) => {
}
};

const getSubscriptions = async (storeId) => {
const getSubscriptions = async () => {
if (CONFIG.USE_SIMULATOR) {
return [];
}

const options = {
url: `${CONFIG.DATALAYER_URL}/subscriptions `,
body: JSON.stringify({
id: storeId,
}),
body: JSON.stringify({}),
};

try {
Expand All @@ -449,10 +453,11 @@ const getSubscriptions = async (storeId) => {
const data = JSON.parse(response);

if (data.success) {
console.log('Your Subscriptions:', data.store_ids);
return data.store_ids;
}

logger.error(`FAILED GETTING STORE IDS FOR ${storeId}`);
logger.error(`FAILED GETTING SUBSCRIPTIONS ON DATALAYER`);
return [];
} catch (error) {
return [];
Expand Down
41 changes: 21 additions & 20 deletions src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ const { USE_SIMULATOR } = getConfig().APP;
const POLLING_INTERVAL = 5000;
const frames = ['-', '\\', '|', '/'];

logger.debug('Start Datalayer Update Polling');
logger.info('Start Datalayer Update Polling');
const startDataLayerUpdatePolling = async () => {
const updateStoreInfo = await dataLayerWasUpdated();
if (updateStoreInfo.length) {
await Promise.all(
updateStoreInfo.map(async (store) => {
logger.debug(
logger.info(
`Updates found syncing storeId: ${store.storeId} ${
frames[Math.floor(Math.random() * 3)]
}`,
Expand Down Expand Up @@ -195,11 +195,7 @@ const subscribeToStoreOnDataLayer = async (storeId) => {
}
};

const getSubscribedStoreData = async (
storeId,
alreadySubscribed = false,
retry = 0,
) => {
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`,
Expand All @@ -208,43 +204,48 @@ const getSubscribedStoreData = async (

const timeoutInterval = 30000;

const subscriptions = await dataLayer.getSubscriptions(storeId);
const alreadySubscribed = subscriptions.includes(storeId);
console.log('%%%%%%%%%%%%', alreadySubscribed);

if (!alreadySubscribed) {
logger.info(`No Subscription Found for ${storeId}, Subscribing...`);
const response = await subscribeToStoreOnDataLayer(storeId);

if (!response || !response.success) {
if (!response) {
logger.debug(
logger.info(
`Response from subscribe RPC came back undefined, is your datalayer running?`,
);
}
logger.debug(
logger.info(
`Retrying subscribe to ${storeId}, subscribe failed`,
retry + 1,
);
logger.debug('...');
logger.info('...');
await new Promise((resolve) =>
setTimeout(() => resolve(), timeoutInterval),
);
return getSubscribedStoreData(storeId, false, retry + 1);
return getSubscribedStoreData(storeId, retry + 1);
}
}

logger.debug(`Subscription Successful for ${storeId}.`);
logger.info(`Subscription Found for ${storeId}.`);

if (!USE_SIMULATOR) {
logger.debug(`Getting confirmation for ${storeId}.`);
logger.info(`Getting confirmation for ${storeId}.`);
const storeExistAndIsConfirmed = await dataLayer.getRoot(storeId, true);
logger.debug(`Store exists and is found ${storeId}.`);
logger.info(`Store exists and is found ${storeId}.`);
if (!storeExistAndIsConfirmed) {
logger.debug(
logger.info(
`Retrying subscribe to ${storeId}, store not yet confirmed.`,
retry + 1,
);
logger.debug('...');
logger.info('...');
await new Promise((resolve) =>
setTimeout(() => resolve(), timeoutInterval),
);
return getSubscribedStoreData(storeId, true, retry + 1);
return getSubscribedStoreData(storeId, retry + 1);
} else {
logger.debug(
`Store Exists and is confirmed, proceededing to get data ${storeId}`,
Expand All @@ -260,15 +261,15 @@ const getSubscribedStoreData = async (
}

if (_.isEmpty(encodedData?.keys_values)) {
logger.debug(
logger.info(
`Retrying subscribe to ${storeId}, No data detected in store.`,
retry + 1,
);
logger.debug('...');
logger.info('...');
await new Promise((resolve) =>
setTimeout(() => resolve(), timeoutInterval),
);
return getSubscribedStoreData(storeId, true, retry + 1);
return getSubscribedStoreData(storeId, retry + 1);
}

const decodedData = decodeDataLayerResponse(encodedData);
Expand Down
2 changes: 1 addition & 1 deletion src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ app.use(function (req, res, next) {
});

app.use(function (req, res, next) {
logger.debug(
logger.info(
`Setting header x-api-verion to package.json version: ${packageJson.version}`,
);
const version = packageJson.version;
Expand Down
3 changes: 3 additions & 0 deletions src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,11 @@ class Organization extends Model {
// eslint-disable-next-line
static importOrganization = async (orgUid) => {
try {
console.log('Importing organization ' + orgUid);
const orgData = await datalayer.getSubscribedStoreData(orgUid);

console.log('!!!!!!!!!!!!', orgData);

if (!orgData.registryId) {
throw new Error(
'Currupted organization, no registryId on the datalayer, can not import',
Expand Down

0 comments on commit 04c88c4

Please sign in to comment.