From 80986abe52b205c16b1fa63b0bdf48f581c3f002 Mon Sep 17 00:00:00 2001 From: William Wills Date: Tue, 29 Oct 2024 11:31:51 -0400 Subject: [PATCH] feat: mirror check task automatically adds missing governance mirrors --- src/models/governance/governance.model.js | 4 +- src/tasks/mirror-check.js | 53 ++++++++++++++++++----- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/models/governance/governance.model.js b/src/models/governance/governance.model.js index 333452d2..fe9ad6a8 100644 --- a/src/models/governance/governance.model.js +++ b/src/models/governance/governance.model.js @@ -30,7 +30,7 @@ class Governance extends Model { const governanceVersionId = await datalayer.createDataLayerStore(); const revertOrganizationIfFailed = async () => { - logger.info('Reverting Failed Governance Body Creation'); + logger.warn('Reverting Failed Governance Body Creation'); await Meta.destroy({ where: { metaKey: 'governanceBodyId' } }); }; @@ -44,7 +44,6 @@ class Governance extends Model { ); const onConfirm = async () => { - logger.info('Organization confirmed, you are ready to go'); await Meta.upsert({ metaKey: 'governanceBodyId', metaValue: governanceVersionId, @@ -53,6 +52,7 @@ class Governance extends Model { metaKey: 'mainGoveranceBodyId', metaValue: governanceBodyId, }); + logger.info('Governance body confirmed, you are ready to go'); }; if (!USE_SIMULATOR) { diff --git a/src/tasks/mirror-check.js b/src/tasks/mirror-check.js index 123cf549..b4178e83 100644 --- a/src/tasks/mirror-check.js +++ b/src/tasks/mirror-check.js @@ -1,5 +1,5 @@ import { SimpleIntervalJob, Task } from 'toad-scheduler'; -import { Organization } from '../models'; +import { Meta, Organization } from '../models'; import { assertDataLayerAvailable, assertWalletIsSynced, @@ -23,7 +23,7 @@ const task = new Task('mirror-check', async () => { const shouldMirror = CONFIG?.AUTO_MIRROR_EXTERNAL_STORES ?? true; if (!CONFIG.USE_SIMULATOR && shouldMirror) { - runMirrorCheck(); + await runMirrorCheck(); } } catch (error) { logger.error( @@ -43,20 +43,49 @@ const job = new SimpleIntervalJob( ); const runMirrorCheck = async () => { - const organizations = await Organization.getOrgsMap(); - const orgs = Object.keys(organizations); - for (const org of orgs) { - const orgData = organizations[org]; - const mirrorUrl = await getMirrorUrl(); - if (mirrorUrl) { + const mirrorUrl = await getMirrorUrl(); + + if (mirrorUrl) { + const governanceOrgUidResult = await Meta.findOne({ + where: { metaKey: 'governanceBodyId' }, + attributes: ['metaValue'], + raw: true, + }); + const governanceRegistryIdResult = await Meta.findOne({ + where: { metaKey: 'mainGoveranceBodyId' }, + attributes: ['metaValue'], + raw: true, + }); + + if ( + governanceOrgUidResult?.metaValue && + governanceRegistryIdResult?.metaValue + ) { + // There is logic within the addMirror function to check if the mirror already exists + await Organization.addMirror( + governanceOrgUidResult?.metaValue, + mirrorUrl, + true, + ); + await Organization.addMirror( + governanceRegistryIdResult?.metaValue, + mirrorUrl, + true, + ); + } + + const organizations = await Organization.getOrgsMap(); + const orgs = Object.keys(organizations); + for (const org of orgs) { + const orgData = organizations[org]; // There is logic within the addMirror function to check if the mirror already exists await Organization.addMirror(orgData.orgUid, mirrorUrl, true); await Organization.addMirror(orgData.registryId, mirrorUrl, true); - } else { - logger.info( - 'DATALAYER_FILE_SERVER_URL not set, skipping mirror announcement', - ); } + } else { + logger.info( + 'DATALAYER_FILE_SERVER_URL not set, skipping mirror announcements', + ); } };