Skip to content

Commit

Permalink
feat: resync every 24
Browse files Browse the repository at this point in the history
  • Loading branch information
frantzarty committed Mar 10, 2022
1 parent 9920b1d commit 91dfaa9
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 6 deletions.
20 changes: 18 additions & 2 deletions src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,20 +242,35 @@ const getRootDiff = (storeId, root1, root2) => {
};

const getStoreData = async (storeId, callback, onFail, retry = 0) => {
if (retry >= 10) {
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);
callback(decodeDataLayerResponse(encodedData));
}
} else {
onFail();
}
};

const getStoreIfUpdated = async (
storeId,
lastRootHash,
onUpdate,
callback,
onFail,
) => {
const rootResponse = await dataLayer.getRoot(storeId);
if (rootResponse.confirmed && rootResponse.hash !== lastRootHash) {
log(`Updating orgUid ${storeId} with hash ${rootResponse.hash}`);
onUpdate(rootResponse.hash);
await getStoreData(storeId, callback, onFail);
}
};

export default {
startDataLayerUpdatePolling,
syncDataLayerStoreToClimateWarehouse,
Expand All @@ -265,5 +280,6 @@ export default {
getRootHistory,
getRootDiff,
getStoreData,
getStoreIfUpdated,
POLLING_INTERVAL,
};
52 changes: 52 additions & 0 deletions src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class Organization extends Model {
};

if (process.env.USE_SIMULATOR !== 'true') {
log('Waiting for New Organization to be confirmed');
datalayer.getStoreData(
newRegistryId,
onConfirm,
Expand Down Expand Up @@ -189,6 +190,57 @@ class Organization extends Model {
await Organization.update({ subscribed: false }, { orgUid });
};

static syncOrganizationMeta = async () => {
try {
const allSubscribedOrganizations = await Organization.findAll({
subscribed: true,
});

await Promise.all(
allSubscribedOrganizations.map((organization) => {
const onResult = (data) => {
const updateData = data.reduce((update, current) => {
update[current.key] = current.value;
return update;
}, {});

Organization.update(
{ ...updateData },
{
where: { orgUid: organization.orgUid },
},
);
};

const onUpdate = (updateHash) => {
Organization.update(
{ orgHash: updateHash },
{
where: { orgUid: organization.orgUid },
},
);
};

const onFail = () => {
throw new Error(
`Unable to sync metadata from ${organization.orgUid}`,
);
};

datalayer.getStoreIfUpdated(
organization.orgUid,
organization.orgHash,
onUpdate,
onResult,
onFail,
);
}),
);
} catch (error) {
log(error);
}
};

static subscribeToDefaultOrganizations = async () => {
try {
const defaultOrgs = await getDefaultOrganizationList();
Expand Down
6 changes: 4 additions & 2 deletions src/tasks/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ToadScheduler } from 'toad-scheduler';

import syncDataLayer from './sync-datalayer';
import syncOrganizations from './sync-organizations';
import syncDefaultOrganizations from './sync-default-organizations';
import syncPickLists from './sync-picklists';
import syncAudit from './sync-audit-table';
import syncOrganizationMeta from './sync-organization-meta';

const scheduler = new ToadScheduler();

Expand All @@ -18,9 +19,10 @@ const start = () => {
// add default jobs
const defaultJobs = [
syncDataLayer,
syncOrganizations,
syncDefaultOrganizations,
syncPickLists,
syncAudit,
syncOrganizationMeta,
];
defaultJobs.forEach((defaultJob) => {
jobRegistry[defaultJob.id] = defaultJob;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Debug.enable('climate-warehouse:task:organizations');

const log = Debug('climate-warehouse:task:organizations');

const task = new Task('sync-organizations', () => {
const task = new Task('sync-default-organizations', () => {
log('Subscribing to default organizations');
if (process.env.USE_SIMULATOR === 'false') {
Organization.subscribeToDefaultOrganizations();
Expand All @@ -18,7 +18,7 @@ const task = new Task('sync-organizations', () => {
const job = new SimpleIntervalJob(
{ days: 1, runImmediately: true },
task,
'sync-organizations',
'sync-default-organizations',
);

export default job;
24 changes: 24 additions & 0 deletions src/tasks/sync-organization-meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { SimpleIntervalJob, Task } from 'toad-scheduler';
import { Organization } from '../models';
import dotenv from 'dotenv';
dotenv.config();

import Debug from 'debug';
Debug.enable('climate-warehouse:task:organizations');

const log = Debug('climate-warehouse:task:organizations');

const task = new Task('sync-organization-meta', () => {
log('Syncing subscribed organizations');
if (process.env.USE_SIMULATOR === 'false') {
Organization.syncOrganizationMeta();
}
});

const job = new SimpleIntervalJob(
{ days: 1, runImmediately: true },
task,
'sync-organization-meta',
);

export default job;

0 comments on commit 91dfaa9

Please sign in to comment.