Files
wekan/models/lib/dueScanClaim.js
T
Lauri Ojansivu d42369e96a Full collection scans run once, not at every start of every server; due-date reminders are sent once.
The startup schema re-check claims a run in _wekan_migration so one server
re-checks while the others skip (a claim older than 2 hours is taken over),
and a run that leaves work unresolved is retried once a day instead of at
every start. The daily due-date reminder scan is claimed per day in
dueReminderRuns, so 24 replicas no longer send 24 reminders per card.
A partial index on lists serves the 15-minute list sync scan, and a sparse
index on boards.dueReminderDays serves the due-date lookup.

Thanks to Raymond-Voogt and xet7 !

Fixes #6855,
2026-11-02 11:28:44 +02:00

33 lines
1.3 KiB
JavaScript

'use strict';
// The due-date reminder scan (models/cards.js findDueCards) runs once a day,
// at NOTIFY_DUE_AT_HOUR_OF_DAY, on a timer in every WeKan server process. With
// several servers on one database (a Kubernetes Deployment of 24 replicas)
// every one of them scanned the due cards and inserted its own duenow /
// almostdue / pastdue activity for each card - one reminder per server.
// The first server to claim the day does the scan; the others skip.
//
// The claim is a document whose _id is the local date, inserted into
// COLLECTION. The unique _id makes the insert succeed for exactly one server;
// the others get a duplicate key error (11000). One small document per day.
const COLLECTION = 'dueReminderRuns';
function dayKey(date) {
const pad = n => String(n).padStart(2, '0');
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
}
// db: a mongodb driver Db. Resolves true when this server should scan today.
async function claimDueScanDay(db, now = new Date()) {
try {
await db.collection(COLLECTION).insertOne({ _id: dayKey(now), at: now });
return true;
} catch (error) {
if (error && error.code === 11000) return false;
throw error;
}
}
module.exports = { claimDueScanDay, dayKey, COLLECTION };