Files
wekan/models/lib/positionHistoryCleanup.js
T
Lauri Ojansivu d481bffdfe History audit and position-history cleanup: once per installation.
Both were Meteor.setInterval jobs, so every WeKan process behind one
database ran them. On a 24-pod installation (54,234 History rows, 148,660
board memberships, 1,760 position-history entries):

- the change history integrity audit read the whole History 24 times an
  hour, and
- UserPositionHistory.cleanup ran 24 times a day, each run one query per
  board membership of every user, each fetching up to 1000 whole
  documents, to trim nothing - outside the CPU governor.

Both are SyncedCron jobs now, the way the other periodic jobs already
are: synced-cron's { intendedAt, name } lock runs each scheduled time on
one pod only, and its running-job check (indexed in #6769) skips a run
while the previous one is still going. No new collection is needed for
it. The audit stays hourly; the cleanup runs at 4:15 am.

The cleanup asks the database which user/board pairs hold more than 1000
non-checkpoint entries (aggregate) and trims only those: the 1000th
newest entry's date with one indexed query, everything older in one
remove. The result is the same as before - the newest 1000 per user and
board are kept, checkpoints never - also for pairs whose user has since
left the board. It waits for a quiet CPU and pauses between pairs.

models/lib/positionHistoryCleanup.js holds the plan with the database
calls passed in; tests/positionHistoryCleanup.test.cjs runs it on an
in-memory history against the old algorithm's result, counts the queries,
and fails if anything in server/ or models/ drives either job from a
per-process timer again. From the reporter's patch, with the claim moved
onto the existing SyncedCron.

Thanks to Raymond-Voogt and xet7 !

Fixes #6821.
2026-10-27 21:18:08 +02:00

41 lines
1.7 KiB
JavaScript

'use strict';
// #6821: keep the newest KEEP non-checkpoint position-history entries per user
// and board. The daily cleanup used to ask every user's every board - one query
// per board membership, each fetching up to 1000 whole documents - which on a
// 24-pod installation was 148,660 queries per run to trim nothing.
//
// The plan here asks the database which user/board pairs are over the limit,
// and visits only those: the KEEPth newest entry's date with one indexed query,
// everything older in one remove. The result is the same as before, also for a
// pair whose user has since left the board (which the old loop skipped).
//
// The database calls are passed in, so this runs in plain Node
// (tests/positionHistoryCleanup.test.cjs).
const KEEP_PER_USER_BOARD = 1000;
function overLimitPipeline(keep = KEEP_PER_USER_BOARD) {
return [
{ $match: { isCheckpoint: { $ne: true } } },
{ $group: { _id: { userId: '$userId', boardId: '$boardId' }, n: { $sum: 1 } } },
{ $match: { n: { $gt: keep } } },
];
}
// db: { overLimit(pipeline) -> [{ _id: { userId, boardId } }],
// oldestKept({ userId, boardId }, keep) -> Date | null,
// remove({ userId, boardId, before }) -> number }
async function trimPositionHistory(db, { keep = KEEP_PER_USER_BOARD, pause = async () => {} } = {}) {
const over = await db.overLimit(overLimitPipeline(keep));
let removed = 0;
for (const { _id: { userId, boardId } } of over) {
const before = await db.oldestKept({ userId, boardId }, keep);
if (before) removed += (await db.remove({ userId, boardId, before })) || 0;
await pause();
}
return { pairs: over.length, removed };
}
module.exports = { KEEP_PER_USER_BOARD, overLimitPipeline, trimPositionHistory };