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..e66cd636 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, @@ -8,8 +8,10 @@ import { logger } from '../config/logger.js'; import { getConfig } from '../utils/config-loader'; import { getMirrorUrl } from '../utils/datalayer-utils'; import dotenv from 'dotenv'; +import datalayer from '../datalayer'; -const CONFIG = getConfig().APP; +const APP_CONFIG = getConfig().APP; +const GOVERNANCE_CONFIG = getConfig().GOVERNANCE; dotenv.config(); // This task checks if there are any mirrors that have not been properly mirrored and then mirrors them if not @@ -20,14 +22,14 @@ const task = new Task('mirror-check', async () => { await assertWalletIsSynced(); // Default AUTO_MIRROR_EXTERNAL_STORES to true if it is null or undefined - const shouldMirror = CONFIG?.AUTO_MIRROR_EXTERNAL_STORES ?? true; + const shouldMirror = APP_CONFIG?.AUTO_MIRROR_EXTERNAL_STORES ?? true; - if (!CONFIG.USE_SIMULATOR && shouldMirror) { - runMirrorCheck(); + if (!APP_CONFIG.USE_SIMULATOR && shouldMirror) { + await runMirrorCheck(); } } catch (error) { logger.error( - `Retrying in ${CONFIG?.TASKS?.MIRROR_CHECK_TASK_INTERVAL || 300} seconds`, + `Retrying in ${APP_CONFIG?.TASKS?.MIRROR_CHECK_TASK_INTERVAL || 300} seconds`, error, ); } @@ -35,7 +37,7 @@ const task = new Task('mirror-check', async () => { const job = new SimpleIntervalJob( { - seconds: CONFIG?.TASKS?.MIRROR_CHECK_TASK_INTERVAL || 300, + seconds: APP_CONFIG?.TASKS?.MIRROR_CHECK_TASK_INTERVAL || 300, runImmediately: true, }, task, @@ -43,20 +45,67 @@ const job = new SimpleIntervalJob( ); const runMirrorCheck = async () => { + const mirrorUrl = await getMirrorUrl(); + + if (!mirrorUrl) { + logger.info( + 'DATALAYER_FILE_SERVER_URL not set, skipping mirror announcements', + ); + return; + } + + // get governance info if governance node + 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 + ) { + // add governance mirrors if instance is governance + // 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, + ); + } else if (GOVERNANCE_CONFIG?.GOVERNANCE_BODY_ID) { + const governanceStoreValue = await datalayer.getSubscribedStoreData( + GOVERNANCE_CONFIG.GOVERNANCE_BODY_ID, + ); + + if (governanceStoreValue?.v1) { + // add governance mirrors if non-governance instance + await Organization.addMirror( + GOVERNANCE_CONFIG.GOVERNANCE_BODY_ID, + mirrorUrl, + true, + ); + await Organization.addMirror(governanceStoreValue.v1, mirrorUrl, true); + } else { + logger.warn('error adding governance mirrors'); + } + } + 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) { - // 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', - ); - } + await Organization.addMirror(orgData.orgUid, mirrorUrl, true); + await Organization.addMirror(orgData.registryId, mirrorUrl, true); } };