Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mirror check task automatically adds missing governance mirrors #1211

Merged
merged 5 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/models/governance/governance.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' } });
};

Expand All @@ -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,
Expand All @@ -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) {
Expand Down
83 changes: 66 additions & 17 deletions src/tasks/mirror-check.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SimpleIntervalJob, Task } from 'toad-scheduler';
import { Organization } from '../models';
import { Meta, Organization } from '../models';
import {
assertDataLayerAvailable,
assertWalletIsSynced,
Expand All @@ -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
Expand All @@ -20,43 +22,90 @@ 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,
);
}
});

const job = new SimpleIntervalJob(
{
seconds: CONFIG?.TASKS?.MIRROR_CHECK_TASK_INTERVAL || 300,
seconds: APP_CONFIG?.TASKS?.MIRROR_CHECK_TASK_INTERVAL || 300,
runImmediately: true,
},
task,
{ id: 'mirror-check', preventOverrun: true },
);

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);
}
};

Expand Down
Loading