Remove sanity scans
This commit is contained in:
@@ -15,29 +15,6 @@ const INCOMPATIBLE_PATTERNS = [
|
||||
/bad collection type/i
|
||||
];
|
||||
|
||||
const SANITY_SCAN_TIMEOUT_MS = parseInt(process.env.P2NS_DB_SANITY_SCAN_TIMEOUT_MS || '8000', 10);
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @param {Promise<T>} promise
|
||||
* @param {number} ms
|
||||
* @param {string} label
|
||||
* @returns {Promise<T>}
|
||||
*/
|
||||
async function withTimeout(promise, ms, label) {
|
||||
let timer;
|
||||
try {
|
||||
return await Promise.race([
|
||||
promise,
|
||||
new Promise((_, reject) => {
|
||||
timer = setTimeout(() => reject(new Error(`${label} timed out after ${ms}ms`)), ms);
|
||||
})
|
||||
]);
|
||||
} finally {
|
||||
if (timer) clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Error|string} err
|
||||
* @returns {boolean}
|
||||
@@ -94,49 +71,7 @@ async function resetPluginDatabaseStorage(pluginDomain, pluginDir, options = {})
|
||||
}
|
||||
|
||||
/**
|
||||
* Lightweight sanity scan — iterate one entry per collection to catch decode errors early.
|
||||
* @param {Object} db - HyperDB instance
|
||||
* @param {Object} def - Database definition
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function sanityScanDatabase(db, def) {
|
||||
if (!db || db.closed || !def || !Array.isArray(def.collections)) {
|
||||
return;
|
||||
}
|
||||
|
||||
await db.ready();
|
||||
if (typeof db.update === 'function') {
|
||||
db.update();
|
||||
}
|
||||
|
||||
for (const collection of def.collections) {
|
||||
const collectionName = collection && collection.name;
|
||||
if (!collectionName) continue;
|
||||
|
||||
try {
|
||||
await withTimeout(
|
||||
db.find(collectionName, {}, { limit: 1 }).toArray(),
|
||||
SANITY_SCAN_TIMEOUT_MS,
|
||||
`Sanity scan for ${collectionName}`
|
||||
);
|
||||
} catch (err) {
|
||||
if (isIncompatibleDbError(err)) {
|
||||
throw err;
|
||||
}
|
||||
if (err.message && err.message.includes('timed out after')) {
|
||||
logWarn(
|
||||
'DBRecovery',
|
||||
`${err.message} — continuing startup (set P2NS_DB_SANITY_SCAN_TIMEOUT_MS to adjust)`
|
||||
);
|
||||
continue;
|
||||
}
|
||||
logDebug('DBRecovery', `Sanity scan skipped collection ${collectionName}: ${err.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize DB with sanity scan; reset storage and retry once on incompatible data.
|
||||
* Open plugin database; reset local storage and retry once on incompatible data at open time.
|
||||
* @param {string} pluginDomain
|
||||
* @param {string} pluginDir
|
||||
* @param {string} specDbDir - HyperDB definition directory (index.js)
|
||||
@@ -148,17 +83,7 @@ async function getDatabaseWithRecovery(pluginDomain, pluginDir, specDbDir, getDa
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
const db = await getDatabase(pluginDomain, pluginDir, specDbDir);
|
||||
logDebug('DBRecovery', `Running sanity scan for ${pluginDomain}...`);
|
||||
|
||||
const defPath = path.join(specDbDir, 'index.js');
|
||||
delete require.cache[require.resolve(defPath)];
|
||||
const def = require(defPath);
|
||||
const definition = def && def.default ? def.default : def;
|
||||
|
||||
await sanityScanDatabase(db, definition);
|
||||
logDebug('DBRecovery', `Sanity scan complete for ${pluginDomain}`);
|
||||
return db;
|
||||
return await getDatabase(pluginDomain, pluginDir, specDbDir);
|
||||
} catch (err) {
|
||||
if (!retried && isIncompatibleDbError(err)) {
|
||||
retried = true;
|
||||
@@ -182,6 +107,5 @@ module.exports = {
|
||||
isIncompatibleDbError,
|
||||
getPluginDbStorageDir,
|
||||
resetPluginDatabaseStorage,
|
||||
sanityScanDatabase,
|
||||
getDatabaseWithRecovery
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user