From 2c0d52ee227c10e09dbd61fe59af3e4c7f1bf503 Mon Sep 17 00:00:00 2001 From: Frantz Arty Date: Fri, 22 Apr 2022 08:16:32 -0400 Subject: [PATCH] feat: check for datalayer and wallet conditions before running tasks --- src/datalayer/persistance.js | 2 -- src/tasks/sync-audit-table.js | 34 +++++++++++++++++-------- src/tasks/sync-datalayer.js | 24 ++++++++++++++--- src/tasks/sync-default-organizations.js | 21 ++++++++++----- src/tasks/sync-governance-body.js | 23 ++++++++++++++--- src/tasks/sync-organization-meta.js | 23 ++++++++++++----- src/tasks/sync-picklists.js | 23 ++++++++++++----- 7 files changed, 111 insertions(+), 39 deletions(-) diff --git a/src/datalayer/persistance.js b/src/datalayer/persistance.js index b5205e2a..96920c14 100644 --- a/src/datalayer/persistance.js +++ b/src/datalayer/persistance.js @@ -198,8 +198,6 @@ export const dataLayerAvailable = async () => { log(data); return false; } catch (error) { - log(error); - return false; } }; diff --git a/src/tasks/sync-audit-table.js b/src/tasks/sync-audit-table.js index 9dc5d80d..63607ac3 100644 --- a/src/tasks/sync-audit-table.js +++ b/src/tasks/sync-audit-table.js @@ -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`); } }); diff --git a/src/tasks/sync-datalayer.js b/src/tasks/sync-datalayer.js index 1f812981..54d99953 100644 --- a/src/tasks/sync-datalayer.js +++ b/src/tasks/sync-datalayer.js @@ -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; diff --git a/src/tasks/sync-default-organizations.js b/src/tasks/sync-default-organizations.js index 7e988a80..dc7835aa 100644 --- a/src/tasks/sync-default-organizations.js +++ b/src/tasks/sync-default-organizations.js @@ -1,5 +1,9 @@ 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; @@ -7,14 +11,19 @@ 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`); } }); diff --git a/src/tasks/sync-governance-body.js b/src/tasks/sync-governance-body.js index b9f08d74..85f74c30 100644 --- a/src/tasks/sync-governance-body.js +++ b/src/tasks/sync-governance-body.js @@ -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; @@ -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`); } }); diff --git a/src/tasks/sync-organization-meta.js b/src/tasks/sync-organization-meta.js index 3f9a554d..9be1a0b5 100644 --- a/src/tasks/sync-organization-meta.js +++ b/src/tasks/sync-organization-meta.js @@ -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`); } }); diff --git a/src/tasks/sync-picklists.js b/src/tasks/sync-picklists.js index ce6a00f8..e566f883 100644 --- a/src/tasks/sync-picklists.js +++ b/src/tasks/sync-picklists.js @@ -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', );