Files
wekan/server/lib/systemStatus.js
T
Lauri Ojansivu 5a1c9a61e9 Admin Panel / Problems "Status" overview + snap run wekan.problems: show migrations/repairs in progress and login-page "Must be logged in" causes; run repairs server-side.
Motivated by the 10.16-10.21 reports where, after an upgrade, the login page
showed "Must be logged in" or sat on the logo + "Loading, please wait." spinner,
pages took 5+ minutes, All Boards counts stayed 0 and i18n showed raw keys — the
FerretDB v1 (SQLite) process pegged ~2 cores for a long time under WeKan's poll
load, i.e. a migration/repair or a saturated backend, with no way to see it
without the Admin Panel.

Server-side, independent of any client / progress page:
 - server/startup/repairBoardsOnStartup.js runs the shared board data-repairs once
   (version-gated, background, 30s after boot) so corrupted data is fixed even if
   no user opens a board and nobody watches a progress page.
 - The MongoDB <-> SQLite text migration and the startup repair now PERSIST their
   progress to the text_migration_status collection (models/textMigrationStatus.js),
   so a separate process can see "a migration/repair is in progress" (an in-memory
   object could not).

Status overview (server/lib/systemStatus.js), shown in Admin Panel / Problems ->
Summary and via the method systemStatusReport (admin-gated, server/methods/
systemStatus.js):
 - everything in progress (text migration, board repair, recovery, cron migration,
   attachment migration) aggregated from the persisted status collections;
 - detected problems (broken cards; failing login-page checks);
 - login-page "Must be logged in" / "Loading, please wait" cause checklist
   (migration in progress, ROOT_URL, LDAP, Sandstorm) — models/lib/loginProblems.js;
 - a CPU detail area that calls out a pegged ferretdb process and the mitigation.

Snap: `snap run wekan.problems` (snap-src/bin/wekan-problems[.mjs], snapcraft app
`problems`) prints the same overview as text — problems, migrations/repairs in
progress, login checks — so an admin with only server access can see it WITHOUT
Admin Panel access. Sub-commands: migrations | login | broken-cards | cpu | help.

Pure helpers models/lib/loginProblems.js and models/lib/problemsOverview.js are
unit-tested; tests/problemsStatus.test.cjs (14 checks) also source-guards the
server hub, method, startup pass, migration persistence and snap command.

Thanks to xet7 !
2026-07-22 16:22:39 +03:00

137 lines
4.9 KiB
JavaScript

import RecoveryStatus from '/models/recoveryStatus';
import CronJobStatus from '/models/cronJobStatus';
import AttachmentMigrationStatus from '/models/attachmentMigrationStatus';
import TextMigrationStatus from '/models/textMigrationStatus';
import Cards from '/models/cards';
import { loginProblemChecks } from '/models/lib/loginProblems';
import { buildProblemsOverview } from '/models/lib/problemsOverview';
// Server hub for the Admin Panel / Problems "Status" overview and the
// `snap run wekan.problems` command. It PERSISTS the text-migration and
// board-repair progress (so a separate process can see "a migration/repair is in
// progress") and AGGREGATES every in-progress migration/repair plus the detected
// problems into one overview.
const TEXT_MIGRATION_ID = 'text-migration';
const BOARD_REPAIR_ID = 'board-repair';
const REPAIR_MARKER_ID = 'startup-repair-version';
async function upsertStatusDoc(id, fields) {
try {
await TextMigrationStatus.upsertAsync(
{ _id: id },
{ $set: { ...fields, _id: id, updatedAt: new Date() } },
);
} catch (error) {
console.error('[systemStatus] persist', id, error);
}
}
// Persist the MongoDB <-> SQLite text-migration progress snapshot.
export async function setTextMigrationStatus(fields) {
await upsertStatusDoc(TEXT_MIGRATION_ID, fields);
}
// Persist the server-side board data-repair progress snapshot.
export async function setBoardRepairStatus(fields) {
await upsertStatusDoc(BOARD_REPAIR_ID, fields);
}
// Version marker so the startup board-repair pass runs once per version, not on
// every boot (a full scan is expensive). Board open still repairs per board.
export async function getRepairMarkerVersion() {
const doc = await TextMigrationStatus.findOneAsync({ _id: REPAIR_MARKER_ID });
return doc && Number.isFinite(doc.version) ? doc.version : 0;
}
export async function setRepairMarkerVersion(version, summary) {
await upsertStatusDoc(REPAIR_MARKER_ID, { version, summary: summary || null });
}
function describeTextMigration(tm) {
let where = '';
if (tm.phase === 'migrating') {
where = `: ${tm.collection || ''} (${tm.collectionsDone || 0}/${tm.collectionsTotal || 0} collections)`;
} else if (tm.phase === 'repairing') {
where = `: repairing ${tm.boardsDone || 0}/${tm.boardsTotal || 0} boards`;
}
return `Database migration (${tm.direction || '?'}) — ${tm.phase || 'running'}${where}`;
}
// Everything currently in progress, as [{ kind, active, message }].
export async function getInProgress() {
const items = [];
const tm = await TextMigrationStatus.findOneAsync({ _id: TEXT_MIGRATION_ID });
if (tm && tm.running) {
items.push({ kind: 'database-migration', active: true, message: describeTextMigration(tm) });
}
const br = await TextMigrationStatus.findOneAsync({ _id: BOARD_REPAIR_ID });
if (br && br.running) {
items.push({
kind: 'board-repair',
active: true,
message: `Board data-repair — ${br.boardsDone || 0}/${br.boardsTotal || 0} boards`,
});
}
try {
const rec = await RecoveryStatus.findOneAsync({});
if (rec && rec.active) {
items.push({ kind: 'recovery', active: true, message: rec.message || 'Data recovery / maintenance in progress' });
}
} catch (_) {}
try {
const jobs = await CronJobStatus.find({ status: 'running' }).fetchAsync();
for (const j of jobs) {
items.push({ kind: 'cron-migration', active: true, message: `Migration job ${j.jobId || j.jobType || j._id} running` });
}
} catch (_) {}
try {
const att = await AttachmentMigrationStatus.findOneAsync({ $or: [{ running: true }, { status: 'running' }] });
if (att) {
items.push({ kind: 'attachment-migration', active: true, message: att.message || 'Attachment migration / repair in progress' });
}
} catch (_) {}
return items;
}
// The login-page "Must be logged in" cause checklist (detected values).
export async function getLoginProblems() {
const inProgress = await getInProgress();
return loginProblemChecks({
migrationActive: inProgress.length > 0,
migrationMessage: inProgress.map(x => x.message).join('; '),
rootUrl: process.env.ROOT_URL || '',
ldapEnabled: process.env.LDAP_ENABLE === 'true' || process.env.LDAP_ENABLE === true,
sandstorm: !!(process.env.SANDSTORM || process.env.SANDSTORM_SMTP_SEND),
});
}
async function countBrokenCards() {
try {
return await Cards.find({
$or: [
{ boardId: { $in: [null, ''] } },
{ swimlaneId: { $in: [null, ''] } },
{ listId: { $in: [null, ''] } },
],
}).countAsync();
} catch (_) {
return 0;
}
}
// The Problems "Status" overview: everything in progress + detected problems.
export async function getProblemsOverview() {
const [inProgress, loginProblems, brokenCards] = await Promise.all([
getInProgress(),
getLoginProblems(),
countBrokenCards(),
]);
return buildProblemsOverview({ inProgress, loginProblems, brokenCards });
}