mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
Production log (v13.13): "scheduledRules: error running trigger ... User ID is required in cards insertAsync" on every run. Scheduled rules, auto-archive and attachment retention acted as board.createdBy, but the Boards schema has no createdBy, so it was undefined on every board: a scheduled rule ran as nobody and its "create card" action always failed. models/lib/boardActor.js names the board's first active admin, else its first active member. The rule's createCard action also passes its acting user as the card's author, since a scheduled run has no method user for the schema's autoValue. tests/boardActor.test.cjs scans the tree so that nothing reads board.createdBy again. Thanks to xet7 !
73 lines
2.5 KiB
JavaScript
73 lines
2.5 KiB
JavaScript
// Nextcloud Deck-style auto-archive (models/lib/autoArchive.js): every hour,
|
|
// archive the cards of boards with "Archive cards after N days without
|
|
// activity" set that nobody has touched for that long. Same quave:synced-cron
|
|
// infrastructure as server/checklistResetSchedule.js.
|
|
import { Meteor } from 'meteor/meteor';
|
|
import { DDP } from 'meteor/ddp';
|
|
import Boards from '/models/boards';
|
|
import Cards from '/models/cards';
|
|
import { SyncedCron } from '/server/cron/syncedCron';
|
|
|
|
const {
|
|
AUTO_ARCHIVE_BATCH, AUTO_ARCHIVE_BOARD_SELECTOR, normalizeAutoArchiveDays, autoArchiveCardSelector,
|
|
} = require('/models/lib/autoArchive');
|
|
|
|
// The archive is recorded as the board creator's, the same attribution the
|
|
// scheduled rules (server/scheduledRules.js) use for what the board itself
|
|
// does on a timer.
|
|
async function asUser(userId, fn) {
|
|
if (userId && typeof DDP._CurrentMethodInvocation?.withValue === 'function') {
|
|
return DDP._CurrentMethodInvocation.withValue({ userId }, fn);
|
|
}
|
|
return fn();
|
|
}
|
|
|
|
export async function autoArchiveBoard(board, now = new Date()) {
|
|
const days = normalizeAutoArchiveDays(board.autoArchiveInactiveDays);
|
|
if (!days) return 0;
|
|
const cards = await Cards.find(autoArchiveCardSelector(board._id, days, now), {
|
|
sort: { dateLastActivity: 1 }, limit: AUTO_ARCHIVE_BATCH,
|
|
}).fetchAsync();
|
|
let archived = 0;
|
|
for (const card of cards) {
|
|
try {
|
|
// card.archive() also archives its subtasks and records archivedCard.
|
|
// eslint-disable-next-line no-await-in-loop
|
|
await asUser(require('/models/lib/boardActor').boardActorId(board), () => card.archive());
|
|
archived += 1;
|
|
} catch (e) {
|
|
// One card that cannot be archived must not stop the rest.
|
|
// eslint-disable-next-line no-console
|
|
console.error('autoArchiveCards: could not archive card', card._id, e.message);
|
|
}
|
|
}
|
|
return archived;
|
|
}
|
|
|
|
export async function scanAutoArchive(now = new Date()) {
|
|
const boards = await Boards.find(AUTO_ARCHIVE_BOARD_SELECTOR, {
|
|
fields: { _id: 1, autoArchiveInactiveDays: 1, createdBy: 1 },
|
|
}).fetchAsync();
|
|
for (const board of boards) {
|
|
// eslint-disable-next-line no-await-in-loop
|
|
await autoArchiveBoard(board, now);
|
|
}
|
|
}
|
|
|
|
Meteor.startup(() => {
|
|
try {
|
|
SyncedCron.add({
|
|
name: 'wekan-card-auto-archive',
|
|
schedule(parser) {
|
|
return parser.text('every 1 hour');
|
|
},
|
|
job() {
|
|
return scanAutoArchive(new Date());
|
|
},
|
|
});
|
|
} catch (e) {
|
|
// eslint-disable-next-line no-console
|
|
console.error('autoArchiveCards: failed to register cron job', e);
|
|
}
|
|
});
|