2017-12-15 11:16:26 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const sql = require('./sql');
|
2018-04-03 09:25:20 +08:00
|
|
|
const sqlInit = require('./sql_init');
|
2017-12-15 11:16:26 +08:00
|
|
|
const log = require('./log');
|
2018-04-02 09:27:46 +08:00
|
|
|
const messagingService = require('./messaging');
|
|
|
|
const syncMutexService = require('./sync_mutex');
|
2019-01-19 02:32:59 +08:00
|
|
|
const repository = require('./repository');
|
2018-03-29 11:41:22 +08:00
|
|
|
const cls = require('./cls');
|
2019-01-19 02:32:59 +08:00
|
|
|
const Branch = require('../entities/branch');
|
2017-12-15 11:16:26 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
let outstandingConsistencyErrors = false;
|
2017-12-15 11:16:26 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
async function runCheck(recoverable, query, errorText) {
|
|
|
|
const results = await sql.getRows(query);
|
2017-12-24 02:16:18 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
if (results.length > 0) {
|
|
|
|
const resultText = results.map(row => "'" + row.value + "'").join(', ');
|
2017-12-15 11:16:26 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
log.error(errorText + ": " + resultText);
|
|
|
|
|
|
|
|
if (!recoverable) {
|
|
|
|
outstandingConsistencyErrors = true;
|
|
|
|
}
|
2017-12-15 11:16:26 +08:00
|
|
|
}
|
2019-01-19 02:32:59 +08:00
|
|
|
|
|
|
|
return results;
|
2017-12-15 11:16:26 +08:00
|
|
|
}
|
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
async function checkTreeCycles() {
|
2018-01-02 08:41:22 +08:00
|
|
|
const childToParents = {};
|
2018-03-25 09:39:15 +08:00
|
|
|
const rows = await sql.getRows("SELECT noteId, parentNoteId FROM branches WHERE isDeleted = 0");
|
2018-01-02 08:41:22 +08:00
|
|
|
|
|
|
|
for (const row of rows) {
|
2018-01-29 08:30:14 +08:00
|
|
|
const childNoteId = row.noteId;
|
|
|
|
const parentNoteId = row.parentNoteId;
|
2018-01-02 08:41:22 +08:00
|
|
|
|
2018-10-22 03:37:34 +08:00
|
|
|
childToParents[childNoteId] = childToParents[childNoteId] || [];
|
2018-01-02 08:41:22 +08:00
|
|
|
childToParents[childNoteId].push(parentNoteId);
|
|
|
|
}
|
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
function checkTreeCycle(noteId, path) {
|
2018-01-02 08:41:22 +08:00
|
|
|
if (noteId === 'root') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-22 04:42:20 +08:00
|
|
|
if (!childToParents[noteId] || childToParents[noteId].length === 0) {
|
|
|
|
errorList.push(`No parents found for noteId=${noteId}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-02 08:41:22 +08:00
|
|
|
for (const parentNoteId of childToParents[noteId]) {
|
|
|
|
if (path.includes(parentNoteId)) {
|
2019-01-19 02:32:59 +08:00
|
|
|
log.error(`Tree cycle detected at parent-child relationship: ${parentNoteId} - ${noteId}, whole path: ${path}`);
|
|
|
|
|
|
|
|
outstandingConsistencyErrors = true;
|
2018-01-02 08:41:22 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
const newPath = path.slice();
|
|
|
|
newPath.push(noteId);
|
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
checkTreeCycle(parentNoteId, newPath);
|
2018-01-02 08:41:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const noteIds = Object.keys(childToParents);
|
|
|
|
|
|
|
|
for (const noteId of noteIds) {
|
2019-01-19 02:32:59 +08:00
|
|
|
checkTreeCycle(noteId, []);
|
2018-01-02 08:41:22 +08:00
|
|
|
}
|
2018-10-22 03:37:34 +08:00
|
|
|
|
|
|
|
if (childToParents['root'].length !== 1 || childToParents['root'][0] !== 'none') {
|
2019-01-19 02:32:59 +08:00
|
|
|
log.error('Incorrect root parent: ' + JSON.stringify(childToParents['root']));
|
|
|
|
outstandingConsistencyErrors = true;
|
2018-10-22 03:37:34 +08:00
|
|
|
}
|
2018-01-02 08:41:22 +08:00
|
|
|
}
|
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
async function runSyncRowChecks(table, key) {
|
|
|
|
await runCheck(false, `
|
2017-12-24 00:26:21 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
${key} AS value
|
2017-12-24 00:26:21 +08:00
|
|
|
FROM
|
|
|
|
${table}
|
2018-01-29 08:30:14 +08:00
|
|
|
LEFT JOIN sync ON sync.entityName = '${table}' AND entityId = ${key}
|
2017-12-24 00:26:21 +08:00
|
|
|
WHERE
|
2018-08-18 00:11:03 +08:00
|
|
|
sync.id IS NULL AND ` + (table === 'options' ? 'isSynced = 1' : '1'),
|
2019-01-19 02:32:59 +08:00
|
|
|
`Missing sync records for ${key} in table ${table}`);
|
2017-12-16 11:16:28 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
await runCheck(false, `
|
2017-12-24 00:26:21 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
entityId AS value
|
2017-12-24 00:26:21 +08:00
|
|
|
FROM
|
|
|
|
sync
|
2018-01-29 08:30:14 +08:00
|
|
|
LEFT JOIN ${table} ON entityId = ${key}
|
2017-12-24 00:26:21 +08:00
|
|
|
WHERE
|
2018-01-29 08:30:14 +08:00
|
|
|
sync.entityName = '${table}'
|
2017-12-24 00:26:21 +08:00
|
|
|
AND ${key} IS NULL`,
|
2019-01-19 02:32:59 +08:00
|
|
|
`Missing ${table} records for existing sync rows`);
|
2017-12-15 11:16:26 +08:00
|
|
|
}
|
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
async function fixEmptyRelationTargets() {
|
2018-11-13 06:34:22 +08:00
|
|
|
const emptyRelations = await repository.getEntities("SELECT * FROM attributes WHERE isDeleted = 0 AND type = 'relation' AND value = ''");
|
|
|
|
|
|
|
|
for (const relation of emptyRelations) {
|
|
|
|
relation.isDeleted = true;
|
|
|
|
await relation.save();
|
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
log.error(`Relation ${relation.attributeId} of name "${relation.name} has empty target. Autofixed.`);
|
2018-11-13 06:34:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
async function checkMissingBranches() {
|
|
|
|
const notes = await runCheck(true, `
|
2017-12-24 00:26:21 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
noteId AS value
|
2017-12-24 00:26:21 +08:00
|
|
|
FROM
|
|
|
|
notes
|
2018-03-25 09:39:15 +08:00
|
|
|
LEFT JOIN branches USING(noteId)
|
2017-12-24 00:26:21 +08:00
|
|
|
WHERE
|
2018-01-29 08:30:14 +08:00
|
|
|
noteId != 'root'
|
2018-03-25 09:39:15 +08:00
|
|
|
AND branches.branchId IS NULL`,
|
2019-01-19 02:32:59 +08:00
|
|
|
"Missing branches for following note IDs");
|
|
|
|
|
|
|
|
for (const {value: noteId} of notes) {
|
|
|
|
const branch = await new Branch({
|
|
|
|
parentNoteId: 'root',
|
|
|
|
noteId: noteId,
|
|
|
|
prefix: 'recovered'
|
|
|
|
}).save();
|
2017-12-15 11:16:26 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
log.info(`Created missing branch ${branch.branchId} for note ${noteId}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkMissingNotes() {
|
|
|
|
const records = await runCheck(true, `
|
2017-12-24 00:26:21 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
branchId || ' > ' || branches.noteId AS value, branchId, branches.noteId
|
2017-12-24 00:26:21 +08:00
|
|
|
FROM
|
2018-03-25 09:39:15 +08:00
|
|
|
branches
|
2018-01-29 08:30:14 +08:00
|
|
|
LEFT JOIN notes USING(noteId)
|
2017-12-24 00:26:21 +08:00
|
|
|
WHERE
|
2018-01-29 08:30:14 +08:00
|
|
|
notes.noteId IS NULL`,
|
2019-01-19 02:32:59 +08:00
|
|
|
"Missing notes records for following branch ID > note ID");
|
|
|
|
|
|
|
|
for (const {branchId, noteId} of records) {
|
|
|
|
const branch = await repository.getBranch(branchId);
|
|
|
|
branch.isDeleted = true;
|
|
|
|
await branch.save();
|
|
|
|
|
|
|
|
log.info(`Removed ${branchId} because it pointed to the missing ${noteId}`);
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 11:16:26 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
async function checkAllDeletedNotesBranchesAreDeleted() {
|
|
|
|
const branches = await runCheck(true, `
|
2017-12-24 00:26:21 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
branchId AS value, branchId, noteId
|
2017-12-24 00:26:21 +08:00
|
|
|
FROM
|
2018-03-25 09:39:15 +08:00
|
|
|
branches
|
2018-01-29 08:30:14 +08:00
|
|
|
JOIN notes USING(noteId)
|
2017-12-24 00:26:21 +08:00
|
|
|
WHERE
|
2018-01-29 08:30:14 +08:00
|
|
|
notes.isDeleted = 1
|
2018-03-25 09:39:15 +08:00
|
|
|
AND branches.isDeleted = 0`,
|
2019-01-19 02:32:59 +08:00
|
|
|
"Branch is not deleted even though main note is deleted for following branch IDs");
|
2017-12-15 11:16:26 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
for (const {branchId, noteId} of branches) {
|
|
|
|
const branch = await repository.getBranch(branchId);
|
|
|
|
branch.isDeleted = true;
|
|
|
|
await branch.save();
|
2017-12-24 00:26:21 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
log.info(`Branch ${branchId} has been deleted since associated note ${noteId} is deleted.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkAllNotesShouldHaveUndeletedBranch() {
|
2018-04-17 04:26:47 +08:00
|
|
|
// we do extra JOIN to eliminate orphan notes without branches (which are reported separately)
|
2019-01-19 02:32:59 +08:00
|
|
|
const notes = await runCheck(true, `
|
2017-12-24 00:26:21 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
DISTINCT noteId AS value
|
2017-12-24 00:26:21 +08:00
|
|
|
FROM
|
|
|
|
notes
|
2018-03-25 09:39:15 +08:00
|
|
|
JOIN branches USING(noteId)
|
2017-12-24 00:26:21 +08:00
|
|
|
WHERE
|
2018-03-25 09:39:15 +08:00
|
|
|
(SELECT COUNT(*) FROM branches WHERE notes.noteId = branches.noteId AND branches.isDeleted = 0) = 0
|
2018-01-29 08:30:14 +08:00
|
|
|
AND notes.isDeleted = 0
|
2019-01-19 02:32:59 +08:00
|
|
|
`, 'No undeleted branches for note IDs');
|
|
|
|
|
|
|
|
for (const {value: noteId} of notes) {
|
|
|
|
const branch = await new Branch({
|
|
|
|
parentNoteId: 'root',
|
|
|
|
noteId: noteId,
|
|
|
|
prefix: 'recovered'
|
|
|
|
}).save();
|
|
|
|
|
|
|
|
log.info(`Created missing branch ${branch.branchId} for note ${noteId}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runAllChecks() {
|
|
|
|
outstandingConsistencyErrors = false;
|
|
|
|
|
|
|
|
await checkMissingBranches();
|
|
|
|
|
|
|
|
await checkMissingNotes();
|
|
|
|
|
|
|
|
await checkAllDeletedNotesBranchesAreDeleted();
|
|
|
|
|
|
|
|
// FIXME - does this make sense? Specifically branch - branch comparison seems strange
|
|
|
|
await runCheck(false, `
|
|
|
|
SELECT
|
|
|
|
child.branchId AS value
|
|
|
|
FROM
|
|
|
|
branches AS child
|
|
|
|
WHERE
|
|
|
|
child.isDeleted = 0
|
|
|
|
AND child.parentNoteId != 'none'
|
|
|
|
AND (SELECT COUNT(*) FROM branches AS parent WHERE parent.noteId = child.parentNoteId
|
|
|
|
AND parent.isDeleted = 0) = 0`,
|
|
|
|
"All parent branches are deleted but child branch is not for these child branch IDs");
|
|
|
|
|
|
|
|
await checkAllNotesShouldHaveUndeletedBranch();
|
2017-12-24 00:26:21 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
await runCheck(false, `
|
2017-12-24 00:26:21 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
child.parentNoteId || ' > ' || child.noteId AS value
|
2018-03-25 09:39:15 +08:00
|
|
|
FROM branches
|
2017-12-24 00:26:21 +08:00
|
|
|
AS child
|
2018-03-25 09:39:15 +08:00
|
|
|
LEFT JOIN branches AS parent ON parent.noteId = child.parentNoteId
|
2017-12-24 00:26:21 +08:00
|
|
|
WHERE
|
2018-01-29 08:30:14 +08:00
|
|
|
parent.noteId IS NULL
|
2018-04-17 04:26:47 +08:00
|
|
|
AND child.parentNoteId != 'none'`,
|
2019-01-19 02:32:59 +08:00
|
|
|
"Not existing parent in the following parent > child relations");
|
2017-12-15 11:16:26 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
await runCheck(false, `
|
2017-12-24 00:26:21 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
noteRevisionId || ' > ' || note_revisions.noteId AS value
|
2017-12-24 00:26:21 +08:00
|
|
|
FROM
|
2018-01-29 08:38:05 +08:00
|
|
|
note_revisions LEFT JOIN notes USING(noteId)
|
2017-12-24 00:26:21 +08:00
|
|
|
WHERE
|
2018-01-29 08:30:14 +08:00
|
|
|
notes.noteId IS NULL`,
|
2019-01-19 02:32:59 +08:00
|
|
|
"Missing notes records for following note revision ID > note ID");
|
2017-12-15 11:16:26 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
await runCheck(false, `
|
2018-01-04 10:33:19 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
branches.parentNoteId || ' > ' || branches.noteId AS value
|
2018-01-04 10:33:19 +08:00
|
|
|
FROM
|
2018-03-25 09:39:15 +08:00
|
|
|
branches
|
2018-01-04 10:33:19 +08:00
|
|
|
WHERE
|
2018-03-25 09:39:15 +08:00
|
|
|
branches.isDeleted = 0
|
2018-01-04 10:33:19 +08:00
|
|
|
GROUP BY
|
2018-03-25 09:39:15 +08:00
|
|
|
branches.parentNoteId,
|
|
|
|
branches.noteId
|
2018-01-04 10:33:19 +08:00
|
|
|
HAVING
|
|
|
|
COUNT(*) > 1`,
|
2019-01-19 02:32:59 +08:00
|
|
|
"Duplicate undeleted parent note <-> note relationship - parent note ID > note ID");
|
2018-01-04 10:33:19 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
await runCheck(false, `
|
2018-01-21 10:56:03 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
noteId AS value
|
2018-01-21 10:56:03 +08:00
|
|
|
FROM
|
|
|
|
notes
|
|
|
|
WHERE
|
2018-11-08 17:11:00 +08:00
|
|
|
type != 'text'
|
|
|
|
AND type != 'code'
|
|
|
|
AND type != 'render'
|
|
|
|
AND type != 'file'
|
|
|
|
AND type != 'image'
|
|
|
|
AND type != 'search'
|
|
|
|
AND type != 'relation-map'`,
|
2019-01-19 02:32:59 +08:00
|
|
|
"Note has invalid type");
|
2018-01-21 10:56:03 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
await runCheck(false, `
|
2018-11-20 06:11:36 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
noteId AS value
|
2018-11-20 06:11:36 +08:00
|
|
|
FROM
|
|
|
|
notes
|
|
|
|
WHERE
|
|
|
|
isDeleted = 0
|
|
|
|
AND content IS NULL`,
|
2019-01-19 02:32:59 +08:00
|
|
|
"Note content is null even though it is not deleted");
|
2018-11-20 06:11:36 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
await runCheck(false, `
|
2018-03-14 07:18:52 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
parentNoteId AS value
|
2018-03-14 07:18:52 +08:00
|
|
|
FROM
|
2018-03-25 09:39:15 +08:00
|
|
|
branches
|
|
|
|
JOIN notes ON notes.noteId = branches.parentNoteId
|
2018-03-14 07:18:52 +08:00
|
|
|
WHERE
|
|
|
|
type == 'search'`,
|
2019-01-19 02:32:59 +08:00
|
|
|
"Search note has children");
|
2018-03-14 07:18:52 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
await fixEmptyRelationTargets();
|
2018-11-15 20:58:14 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
await runCheck(false, `
|
2018-11-15 20:58:14 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
attributeId AS value
|
2018-11-15 20:58:14 +08:00
|
|
|
FROM
|
|
|
|
attributes
|
|
|
|
WHERE
|
|
|
|
type != 'label'
|
|
|
|
AND type != 'label-definition'
|
|
|
|
AND type != 'relation'
|
|
|
|
AND type != 'relation-definition'`,
|
2019-01-19 02:32:59 +08:00
|
|
|
"Attribute has invalid type");
|
2018-11-15 20:58:14 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
await runCheck(false,`
|
2018-11-15 20:58:14 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
attributeId AS value
|
2018-11-15 20:58:14 +08:00
|
|
|
FROM
|
|
|
|
attributes
|
|
|
|
LEFT JOIN notes ON attributes.noteId = notes.noteId AND notes.isDeleted = 0
|
|
|
|
WHERE
|
|
|
|
attributes.isDeleted = 0
|
|
|
|
AND notes.noteId IS NULL`,
|
2019-01-19 02:32:59 +08:00
|
|
|
"Attribute reference to the owning note is broken");
|
2018-11-15 20:58:14 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
await runCheck(false, `
|
2018-11-15 20:58:14 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
attributeId AS value
|
2018-11-15 20:58:14 +08:00
|
|
|
FROM
|
|
|
|
attributes
|
|
|
|
LEFT JOIN notes AS targetNote ON attributes.value = targetNote.noteId AND targetNote.isDeleted = 0
|
|
|
|
WHERE
|
|
|
|
attributes.type = 'relation'
|
|
|
|
AND attributes.isDeleted = 0
|
|
|
|
AND targetNote.noteId IS NULL`,
|
2019-01-19 02:32:59 +08:00
|
|
|
"Relation reference to the target note is broken");
|
2018-11-15 20:58:14 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
await runCheck(false, `
|
2018-11-15 20:58:14 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
linkId AS value
|
2018-11-15 20:58:14 +08:00
|
|
|
FROM
|
|
|
|
links
|
|
|
|
WHERE
|
|
|
|
type != 'image'
|
2018-11-17 06:27:57 +08:00
|
|
|
AND type != 'hyper'
|
|
|
|
AND type != 'relation-map'`,
|
2019-01-19 02:32:59 +08:00
|
|
|
"Link type is invalid");
|
2018-11-15 20:58:14 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
await runCheck(false, `
|
2018-11-15 20:58:14 +08:00
|
|
|
SELECT
|
2019-01-19 02:32:59 +08:00
|
|
|
linkId AS value
|
2018-11-15 20:58:14 +08:00
|
|
|
FROM
|
|
|
|
links
|
|
|
|
LEFT JOIN notes AS sourceNote ON sourceNote.noteId = links.noteId AND sourceNote.isDeleted = 0
|
|
|
|
LEFT JOIN notes AS targetNote ON targetNote.noteId = links.noteId AND targetNote.isDeleted = 0
|
|
|
|
WHERE
|
2018-11-18 17:20:06 +08:00
|
|
|
links.isDeleted = 0
|
|
|
|
AND (sourceNote.noteId IS NULL
|
|
|
|
OR targetNote.noteId IS NULL)`,
|
2019-01-19 02:32:59 +08:00
|
|
|
"Link to source/target note link is broken");
|
2018-11-15 20:58:14 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
await runSyncRowChecks("notes", "noteId");
|
|
|
|
await runSyncRowChecks("note_revisions", "noteRevisionId");
|
|
|
|
await runSyncRowChecks("branches", "branchId");
|
|
|
|
await runSyncRowChecks("recent_notes", "branchId");
|
|
|
|
await runSyncRowChecks("attributes", "attributeId");
|
|
|
|
await runSyncRowChecks("api_tokens", "apiTokenId");
|
|
|
|
await runSyncRowChecks("options", "name");
|
2017-12-15 12:21:03 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
if (outstandingConsistencyErrors) {
|
2018-01-02 08:41:22 +08:00
|
|
|
// we run this only if basic checks passed since this assumes basic data consistency
|
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
await checkTreeCycles();
|
2018-01-02 08:41:22 +08:00
|
|
|
}
|
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
return !outstandingConsistencyErrors;
|
2018-01-05 10:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function runChecks() {
|
|
|
|
let elapsedTimeMs;
|
2019-01-19 02:32:59 +08:00
|
|
|
let dbConsistent;
|
2018-01-05 10:37:36 +08:00
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
await syncMutexService.doExclusively(async () => {
|
2018-01-05 10:37:36 +08:00
|
|
|
const startTime = new Date();
|
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
dbConsistent = await runAllChecks();
|
2018-01-05 10:37:36 +08:00
|
|
|
|
|
|
|
elapsedTimeMs = new Date().getTime() - startTime.getTime();
|
2018-01-14 11:51:39 +08:00
|
|
|
});
|
2018-01-02 08:47:50 +08:00
|
|
|
|
2019-01-19 02:32:59 +08:00
|
|
|
if (!dbConsistent) {
|
|
|
|
log.info(`Consistency checks failed (took ${elapsedTimeMs}ms)`);
|
2018-01-02 08:41:22 +08:00
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
messagingService.sendMessageToAllClients({type: 'consistency-checks-failed'});
|
2017-12-15 12:21:03 +08:00
|
|
|
}
|
2017-12-15 12:30:38 +08:00
|
|
|
else {
|
2018-01-02 08:47:50 +08:00
|
|
|
log.info(`All consistency checks passed (took ${elapsedTimeMs}ms)`);
|
2017-12-15 12:30:38 +08:00
|
|
|
}
|
2017-12-15 11:16:26 +08:00
|
|
|
}
|
|
|
|
|
2018-04-03 09:25:20 +08:00
|
|
|
sqlInit.dbReady.then(() => {
|
2018-03-29 11:41:22 +08:00
|
|
|
setInterval(cls.wrap(runChecks), 60 * 60 * 1000);
|
2017-12-15 11:16:26 +08:00
|
|
|
|
|
|
|
// kickoff backup immediately
|
2018-03-29 11:41:22 +08:00
|
|
|
setTimeout(cls.wrap(runChecks), 10000);
|
2017-12-15 11:16:26 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
runChecks
|
|
|
|
};
|