auto clean sync plans

This commit is contained in:
fyears 2022-04-06 00:35:21 +08:00
parent b3c107ce58
commit bb21714b45
2 changed files with 64 additions and 0 deletions

View File

@ -552,6 +552,48 @@ export const readAllSyncPlanRecordTextsByVault = async (
} }
}; };
/**
* We remove records that are older than 7 days or 10000 records.
* It's a heavy operation, so we shall not place it in the start up.
* @param db
*/
export const clearExpiredSyncPlanRecords = async (db: InternalDBs) => {
const MILLISECONDS_OLD = 1000 * 60 * 60 * 24 * 7; // 7 days
const COUNT_TO_MANY = 10000;
const currTs = Date.now();
const expiredTs = currTs - MILLISECONDS_OLD;
let records = (await db.syncPlansTbl.keys()).map((key) => {
const ts = parseInt(key.split("\t")[1]);
const expired = ts <= expiredTs;
return {
ts: ts,
key: key,
expired: expired,
};
});
const keysToRemove = new Set(
records.filter((x) => x.expired).map((x) => x.key)
);
if (records.length - keysToRemove.size > COUNT_TO_MANY) {
// we need to find out records beyond 10000 records
records = records.filter((x) => !x.expired); // shrink the array
records.sort((a, b) => -(a.ts - b.ts)); // descending
records.slice(COUNT_TO_MANY).forEach((element) => {
keysToRemove.add(element.key);
});
}
const ps = [] as Promise<void>[];
keysToRemove.forEach((element) => {
ps.push(db.syncPlansTbl.removeItem(element));
});
await Promise.all(ps);
};
export const readAllLogRecordTextsByVault = async ( export const readAllLogRecordTextsByVault = async (
db: InternalDBs, db: InternalDBs,
vaultRandomID: string vaultRandomID: string

View File

@ -26,6 +26,7 @@ import {
InternalDBs, InternalDBs,
insertLoggerOutputByVault, insertLoggerOutputByVault,
clearExpiredLoggerOutputRecords, clearExpiredLoggerOutputRecords,
clearExpiredSyncPlanRecords,
} from "./localdb"; } from "./localdb";
import { RemoteClient } from "./remote"; import { RemoteClient } from "./remote";
import { import {
@ -419,6 +420,9 @@ export default class RemotelySavePlugin extends Plugin {
this.addOutputToDBIfSet(); this.addOutputToDBIfSet();
this.enableAutoClearOutputToDBHistIfSet(); this.enableAutoClearOutputToDBHistIfSet();
// must AFTER preparing DB
this.enableAutoClearSyncPlanHist();
this.syncStatus = "idle"; this.syncStatus = "idle";
this.registerEvent( this.registerEvent(
@ -948,4 +952,22 @@ export default class RemotelySavePlugin extends Plugin {
this.registerInterval(intervalID); this.registerInterval(intervalID);
}); });
} }
enableAutoClearSyncPlanHist() {
const initClearSyncPlanHistAfterMilliseconds = 1000 * 45;
const autoClearSyncPlanHistAfterMilliseconds = 1000 * 60 * 5;
this.app.workspace.onLayoutReady(() => {
// init run
window.setTimeout(() => {
clearExpiredSyncPlanRecords(this.db);
}, initClearSyncPlanHistAfterMilliseconds);
// scheduled run
const intervalID = window.setInterval(() => {
clearExpiredSyncPlanRecords(this.db);
}, autoClearSyncPlanHistAfterMilliseconds);
this.registerInterval(intervalID);
});
}
} }