Skip to content

Commit

Permalink
Merge pull request #486 from Chia-Network/geature/checkDatalayerAndWa…
Browse files Browse the repository at this point in the history
…llet

feat: check for datalayer and wallet conditions before
  • Loading branch information
MichaelTaylor3D authored Apr 22, 2022
2 parents 46f75cd + 2c0d52e commit 1ffefcc
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 39 deletions.
2 changes: 0 additions & 2 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ export const dataLayerAvailable = async () => {
log(data);
return false;
} catch (error) {
log(error);

return false;
}
};
Expand Down
34 changes: 24 additions & 10 deletions src/tasks/sync-audit-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,38 @@ import { Organization, Audit } from '../models';
import datalayer from '../datalayer';
import { decodeHex } from '../utils/datalayer-utils';
import dotenv from 'dotenv';

import {
assertDataLayerAvailable,
assertWalletIsSynced,
} from '../utils/data-assertions';
import Debug from 'debug';
Debug.enable('climate-warehouse:task:audit');
const log = Debug('climate-warehouse:datalayer:persistance');
const log = Debug('climate-warehouse:datalayer:audit');
dotenv.config();
import { getConfig } from '../utils/config-loader';

const { USE_SIMULATOR } = getConfig().APP;

const task = new Task('sync-audit', async () => {
log('Syncing Audit Information');
if (!USE_SIMULATOR) {
const organizations = await Organization.findAll({
where: { subscribed: true },
raw: true,
});
await Promise.all(
organizations.map((organization) => syncOrganizationAudit(organization)),
);
try {
await assertDataLayerAvailable();
await assertWalletIsSynced();

log('Syncing Audit Information');
if (process.env.USE_SIMULATOR === 'false') {
const organizations = await Organization.findAll({
where: { subscribed: true },
raw: true,
});
await Promise.all(
organizations.map((organization) =>
syncOrganizationAudit(organization),
),
);
}
} catch (error) {
log(`${error.message} retrying in 30 seconds`);
}
});

Expand Down
24 changes: 20 additions & 4 deletions src/tasks/sync-datalayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,33 @@ import { SimpleIntervalJob, Task } from 'toad-scheduler';
import datalayer from '../datalayer';
import dotenv from 'dotenv';
import cliSpinner from 'cli-spinner';
import {
assertDataLayerAvailable,
assertWalletIsSynced,
} from '../utils/data-assertions';
const Spinner = cliSpinner.Spinner;
dotenv.config();

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

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

const spinner = new Spinner('Waiting for Updates %s');
spinner.setSpinnerString('|/-\\');
spinner.setSpinnerDelay(500);

const task = new Task('sync-datalayer', () => {
spinner.stop();
spinner.start();
datalayer.startDataLayerUpdatePolling();
const task = new Task('sync-datalayer', async () => {
try {
await assertDataLayerAvailable();
await assertWalletIsSynced();

spinner.stop();
spinner.start();
datalayer.startDataLayerUpdatePolling();
} catch (error) {
log(`${error.message} retrying in 60 seconds`);
}
});

let seconds = 5;
Expand Down
21 changes: 15 additions & 6 deletions src/tasks/sync-default-organizations.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import { SimpleIntervalJob, Task } from 'toad-scheduler';
import { Organization } from '../models';
import {
assertDataLayerAvailable,
assertWalletIsSynced,
} from '../utils/data-assertions';
import { getConfig } from '../utils/config-loader';
const { USE_SIMULATOR } = getConfig().APP;

import dotenv from 'dotenv';
dotenv.config();

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

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

const task = new Task('sync-default-organizations', () => {
log('Subscribing to default organizations');
if (!USE_SIMULATOR) {
Organization.subscribeToDefaultOrganizations();
const task = new Task('sync-default-organizations', async () => {
try {
await assertDataLayerAvailable();
await assertWalletIsSynced();
if (process.env.USE_SIMULATOR === 'false') {
Organization.subscribeToDefaultOrganizations();
}
} catch (error) {
log(`${error.message} retrying in 30 seconds`);
}
});

Expand Down
23 changes: 19 additions & 4 deletions src/tasks/sync-governance-body.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { SimpleIntervalJob, Task } from 'toad-scheduler';
import { Governance } from '../models';
import {
assertDataLayerAvailable,
assertWalletIsSynced,
} from '../utils/data-assertions';
import { getConfig } from '../utils/config-loader';
const { GOVERANCE_BODY_ID, GOVERNANCE_BODY_IP, GOVERNANCE_BODY_PORT } =
getConfig().GOVERNANCE;
Expand All @@ -12,10 +16,21 @@ Debug.enable('climate-warehouse:task:governance');

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

const task = new Task('sync-governance-meta', () => {
log('Syncing governance data');
if (GOVERANCE_BODY_ID && GOVERNANCE_BODY_IP && GOVERNANCE_BODY_PORT) {
Governance.sync();
const task = new Task('sync-governance-meta', async () => {
try {
await assertDataLayerAvailable();
await assertWalletIsSynced();

log('Syncing governance data');
if (
process.env.GOVERANCE_BODY_ID &&
process.env.GOVERNANCE_BODY_IP &&
process.env.GOVERNANCE_BODY_PORT
) {
Governance.sync();
}
} catch (error) {
log(`${error.message} retrying in 24 hours`);
}
});

Expand Down
23 changes: 16 additions & 7 deletions src/tasks/sync-organization-meta.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import { SimpleIntervalJob, Task } from 'toad-scheduler';
import { Organization } from '../models';
import { getConfig } from '../utils/config-loader';
import {
assertDataLayerAvailable,
assertWalletIsSynced,
} from '../utils/data-assertions';
const { USE_SIMULATOR } = getConfig().APP;

import dotenv from 'dotenv';
dotenv.config();

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

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

const task = new Task('sync-organization-meta', () => {
log('Syncing subscribed organizations');
if (!USE_SIMULATOR) {
Organization.syncOrganizationMeta();
const log = Debug('climate-warehouse:task:sync-organizations');
const task = new Task('sync-organization-meta', async () => {
try {
await assertDataLayerAvailable();
await assertWalletIsSynced();
log('Syncing subscribed organizations');
if (process.env.USE_SIMULATOR === 'false') {
Organization.syncOrganizationMeta();
}
} catch (error) {
log(`${error.message} retrying in 24 hours`);
}
});

Expand Down
23 changes: 17 additions & 6 deletions src/tasks/sync-picklists.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import { SimpleIntervalJob, Task } from 'toad-scheduler';
import { pullPickListValues } from '../utils/data-loaders';
import Debug from 'debug';
Debug.enable('climate-warehouse:task:picklists');
import {
assertDataLayerAvailable,
assertWalletIsSynced,
} from '../utils/data-assertions';
Debug.enable('climate-warehouse:task:sync-picklists');

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

const task = new Task('sync-picklist', () => {
log('Syncing Picklist Values');
pullPickListValues();
const retryInSeconds = 30;

const task = new Task('sync-picklist', async () => {
try {
await assertDataLayerAvailable();
await assertWalletIsSynced();
pullPickListValues();
} catch (error) {
log(`${error.message} retrying in ${retryInSeconds} seconds`);
}
});

const job = new SimpleIntervalJob(
{ seconds: 30, runImmediately: true },
{ seconds: retryInSeconds, runImmediately: true },
task,
'sync-picklist',
);
Expand Down

0 comments on commit 1ffefcc

Please sign in to comment.