perf(onebot): reduce message history migration scans

Speed up unfinished history migration while preserving all messages and
existing processing limits.

Keep migration diagnostics under the configured logging policy and surface
preparation failures.

Fixes #462
This commit is contained in:
DrSmoothl committed 2026-10-04 14:56:02 +08:00
1 parent 99bcfca15c
commit a6dfd04924
6 files changed
+286 -39

No files matched your search

@@ -1,5 +1,6 @@
import { EventEmitter } from 'node:events';
import { describe, expect, it, vi } from 'vitest';
import { subscribeLogs } from '@snowluma/common/logger';
import type { DatabaseMigrationCallbacks } from '../src/manager';
import {
createMessageStoreMigrationTask,
@@ -22,6 +23,32 @@ function callbacks(): DatabaseMigrationCallbacks {
}
describe('message-store migration task lifecycle', () => {
it('records worker preparation through the parent logger with account context', () => {
const worker = fakeWorker();
const handlers = callbacks();
const entries: Array<{ scope: string; uin?: number; message: string }> = [];
const unsubscribe = subscribeLogs(entry => entries.push(entry));
const task = createMessageStoreMigrationTask('10001', () => worker);
try {
task.start(handlers);
worker.emit('message', { kind: 'preparation', progress: { stage: 'started' } });
worker.emit('message', { kind: 'preparation', progress: { stage: 'indexed', elapsedMs: 12.5 } });
worker.emit('message', { kind: 'preparation', progress: { stage: 'counted', total: 3, elapsedMs: 2.1 } });
expect(entries.map(entry => ({ scope: entry.scope, uin: entry.uin, message: entry.message }))).toEqual([
{ scope: 'OneBot.MessageMigration', uin: 10001, message: 'preparing message history migration' },
{ scope: 'OneBot.MessageMigration', uin: 10001, message: 'message history migration preparation finished in 13ms' },
{ scope: 'OneBot.MessageMigration', uin: 10001, message: 'message history migration: pending=3 checked in 2ms' },
]);
expect(handlers.onProgress).not.toHaveBeenCalled();
expect(handlers.onFailed).not.toHaveBeenCalled();
task.cancel();
worker.emit('message', { kind: 'preparation', progress: { stage: 'started' } });
expect(entries).toHaveLength(3);
} finally {
unsubscribe();
}
});
it('reports an exit before completion even when its code is zero', () => {
const worker = fakeWorker();
const handlers = callbacks();