- 新增 app/schedule/system_log_cleanup.js 定时任务:cron 从 config.systemLogCleanup.cron 读取(缺省回退 '0 0 3 * * *'),受 enable 开关与防重入保护 - 新增 app/service/systems/system_log.js:按 id 边界定位 + 分批硬删除超期日志,保留天数默认 60(normalizeRetainDays 校验非法值回退),批间随机休眠降低数据库压力 - config.default.js 新增 config.systemLogCleanup 配置块:enable、cron、retainDays 60、batchSize 2000、sleepMinMs/sleepMaxMs - 新增 service 与 schedule 单测(39 个用例),覆盖边界定位、保留天数校验、配置开关与防重入等场景
384 lines
14 KiB
JavaScript
384 lines
14 KiB
JavaScript
'use strict';
|
||
|
||
const assert = require('node:assert/strict');
|
||
const fs = require('node:fs');
|
||
const path = require('node:path');
|
||
const SystemLogService = require('../../../../app/service/systems/system_log');
|
||
|
||
const SERVICE_SOURCE_PATH = path.join(__dirname, '../../../../app/service/systems/system_log.js');
|
||
|
||
/**
|
||
* 创建系统日志清理服务的测试上下文(纯 mock,不连接任何数据库)。
|
||
*
|
||
* @param {Function} [queryHandler] 自定义 query 处理器,接收 (sql, options, callIndex)。
|
||
* @return {Object} { service, ctx, captures }
|
||
*/
|
||
function createHarness(queryHandler) {
|
||
const captures = { queries: [] };
|
||
const ctx = {
|
||
app: { config: {} },
|
||
service: {},
|
||
model: {
|
||
QueryTypes: { SELECT: 'SELECT' },
|
||
async query(sql, queryOptions) {
|
||
captures.queries.push({ sql, options: queryOptions });
|
||
if (typeof queryHandler === 'function') {
|
||
return await queryHandler(sql, queryOptions, captures.queries.length);
|
||
}
|
||
return [];
|
||
},
|
||
},
|
||
logger: {
|
||
debug() {},
|
||
info() {},
|
||
warn() {},
|
||
error() {},
|
||
},
|
||
};
|
||
const service = new SystemLogService(ctx);
|
||
return { service, ctx, captures };
|
||
}
|
||
|
||
/**
|
||
* 按 SQL 类型返回预设结果:SELECT 返回边界行,DELETE 依次返回各批 affectedRows。
|
||
*
|
||
* @param {Array<Object>} selectRows SELECT 结果行。
|
||
* @param {Array<number>} deleteAffectedRows DELETE 各批 affectedRows 序列。
|
||
* @return {Function} query 处理器。
|
||
*/
|
||
function sequenceQueryHandler(selectRows, deleteAffectedRows) {
|
||
const pending = deleteAffectedRows.slice();
|
||
return sql => {
|
||
if (/^SELECT/.test(sql)) {
|
||
return selectRows;
|
||
}
|
||
return [{ affectedRows: pending.shift() }, undefined ];
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 临时替换 global.setTimeout:捕获休眠时长并立即回调,避免测试真实等待。
|
||
*
|
||
* @param {Function} handler 执行体,接收捕获到的休眠时长数组。
|
||
* @return {Promise<Object>} { result, sleepDelays }
|
||
*/
|
||
async function runWithStubbedTimers(handler) {
|
||
const realSetTimeout = global.setTimeout;
|
||
const sleepDelays = [];
|
||
|
||
global.setTimeout = (callback, delay) => {
|
||
if (typeof delay === 'number') {
|
||
sleepDelays.push(delay);
|
||
}
|
||
return realSetTimeout(callback, 0);
|
||
};
|
||
|
||
try {
|
||
const result = await handler(sleepDelays);
|
||
return { result, sleepDelays };
|
||
} finally {
|
||
global.setTimeout = realSetTimeout;
|
||
}
|
||
}
|
||
|
||
describe('test/app/service/systems/system_log.test.js', () => {
|
||
describe('normalizeRetainDays 保留天数归一化(经 findCleanupBoundaryId 的 SQL 断言)', () => {
|
||
it('正整数保留原值:30 生成 INTERVAL 30 DAY', async () => {
|
||
const { service, captures } = createHarness(() => []);
|
||
await service.findCleanupBoundaryId(30);
|
||
assert.match(captures.queries[0].sql, /INTERVAL 30 DAY/);
|
||
});
|
||
|
||
it('数字字符串归一化为整数:\'45\' 生成 INTERVAL 45 DAY', async () => {
|
||
const { service, captures } = createHarness(() => []);
|
||
await service.findCleanupBoundaryId('45');
|
||
assert.match(captures.queries[0].sql, /INTERVAL 45 DAY/);
|
||
});
|
||
|
||
it('负数回退默认 60:-10 生成 INTERVAL 60 DAY', async () => {
|
||
const { service, captures } = createHarness(() => []);
|
||
await service.findCleanupBoundaryId(-10);
|
||
assert.match(captures.queries[0].sql, /INTERVAL 60 DAY/);
|
||
});
|
||
|
||
it('0 回退默认 60:生成 INTERVAL 60 DAY', async () => {
|
||
const { service, captures } = createHarness(() => []);
|
||
await service.findCleanupBoundaryId(0);
|
||
assert.match(captures.queries[0].sql, /INTERVAL 60 DAY/);
|
||
});
|
||
|
||
it('小数回退默认 60:30.5 生成 INTERVAL 60 DAY', async () => {
|
||
const { service, captures } = createHarness(() => []);
|
||
await service.findCleanupBoundaryId(30.5);
|
||
assert.match(captures.queries[0].sql, /INTERVAL 60 DAY/);
|
||
});
|
||
|
||
it('非数字回退默认 60:\'abc\' 生成 INTERVAL 60 DAY', async () => {
|
||
const { service, captures } = createHarness(() => []);
|
||
await service.findCleanupBoundaryId('abc');
|
||
assert.match(captures.queries[0].sql, /INTERVAL 60 DAY/);
|
||
});
|
||
|
||
it('null/undefined 回退默认 60', async () => {
|
||
const { service: service1, captures: captures1 } = createHarness(() => []);
|
||
await service1.findCleanupBoundaryId(null);
|
||
assert.match(captures1.queries[0].sql, /INTERVAL 60 DAY/);
|
||
|
||
const { service: service2, captures: captures2 } = createHarness(() => []);
|
||
await service2.findCleanupBoundaryId(undefined);
|
||
assert.match(captures2.queries[0].sql, /INTERVAL 60 DAY/);
|
||
});
|
||
});
|
||
|
||
describe('findCleanupBoundaryId 边界定位', () => {
|
||
it('返回超期行的最大 id 且 SQL 按 id 倒序取 1 行(默认 60 天)', async () => {
|
||
const { service, captures } = createHarness(() => [{ id: '987654' }]);
|
||
|
||
const boundaryId = await service.findCleanupBoundaryId(60);
|
||
|
||
assert.equal(boundaryId, 987654);
|
||
assert.equal(captures.queries.length, 1);
|
||
assert.match(captures.queries[0].sql, /SELECT id FROM wms_system_log/);
|
||
assert.match(captures.queries[0].sql, /WHERE created_at < DATE_SUB\(NOW\(\), INTERVAL 60 DAY\)/);
|
||
assert.match(captures.queries[0].sql, /ORDER BY id DESC LIMIT 1$/);
|
||
assert.equal(captures.queries[0].options.type, 'SELECT');
|
||
});
|
||
|
||
it('空表(查询返回 [])时返回 null', async () => {
|
||
const { service } = createHarness(() => []);
|
||
const boundaryId = await service.findCleanupBoundaryId(60);
|
||
assert.equal(boundaryId, null);
|
||
});
|
||
|
||
it('查询返回非数组(null)时返回 null', async () => {
|
||
const { service } = createHarness(() => null);
|
||
const boundaryId = await service.findCleanupBoundaryId(60);
|
||
assert.equal(boundaryId, null);
|
||
});
|
||
|
||
it('返回行缺少 id 字段时返回 null', async () => {
|
||
const { service } = createHarness(() => [{ created_at: '2026-01-01' }]);
|
||
const boundaryId = await service.findCleanupBoundaryId(60);
|
||
assert.equal(boundaryId, null);
|
||
});
|
||
});
|
||
|
||
describe('deleteSystemLogsBatch 分批硬删除', () => {
|
||
it('RAW DELETE 取 [ResultSetHeader, metadata] 第一项的 affectedRows', async () => {
|
||
const { service, captures } = createHarness(() => [{ affectedRows: 2000 }, undefined ]);
|
||
|
||
const affectedRows = await service.deleteSystemLogsBatch(42, 2000);
|
||
|
||
assert.equal(affectedRows, 2000);
|
||
const call = captures.queries[0];
|
||
assert.match(call.sql, /^DELETE FROM wms_system_log WHERE id <= 42 ORDER BY id LIMIT 2000$/);
|
||
assert.equal(call.options, undefined, '硬删除应走 RAW 查询,不传 type');
|
||
});
|
||
|
||
it('数组第二项也有 affectedRows 时仍取第一项', async () => {
|
||
const { service } = createHarness(() => [{ affectedRows: 7 }, { affectedRows: 999 }]);
|
||
const affectedRows = await service.deleteSystemLogsBatch(10, 2000);
|
||
assert.equal(affectedRows, 7);
|
||
});
|
||
|
||
it('非数组单对象返回时取 header.affectedRows', async () => {
|
||
const { service } = createHarness(() => ({ affectedRows: 5 }));
|
||
const affectedRows = await service.deleteSystemLogsBatch(10, 2000);
|
||
assert.equal(affectedRows, 5);
|
||
});
|
||
|
||
it('affectedRows 为 0 或非法时返回 0', async () => {
|
||
const harness1 = createHarness(() => [{ affectedRows: 0 }, undefined ]);
|
||
assert.equal(await harness1.service.deleteSystemLogsBatch(10, 2000), 0);
|
||
|
||
const harness2 = createHarness(() => [{}, undefined ]);
|
||
assert.equal(await harness2.service.deleteSystemLogsBatch(10, 2000), 0);
|
||
});
|
||
|
||
it('boundaryId 无效时不执行任何 SQL', async () => {
|
||
const { service, captures } = createHarness(() => {
|
||
throw new Error('不应被调用');
|
||
});
|
||
|
||
assert.equal(await service.deleteSystemLogsBatch(0, 2000), 0);
|
||
assert.equal(await service.deleteSystemLogsBatch(-1, 2000), 0);
|
||
assert.equal(captures.queries.length, 0);
|
||
});
|
||
});
|
||
|
||
describe('cleanupExpiredSystemLogs 清理主流程', () => {
|
||
it('查不到边界时 skipped 且不执行任何删除', async () => {
|
||
const { service, captures } = createHarness(() => []);
|
||
|
||
const summary = await service.cleanupExpiredSystemLogs();
|
||
|
||
assert.equal(summary.skipped, true);
|
||
assert.equal(summary.boundary_id, null);
|
||
assert.equal(summary.batch_count, 0);
|
||
assert.equal(summary.deleted_rows, 0);
|
||
const deleteQueries = captures.queries.filter(query => /^DELETE/.test(query.sql));
|
||
assert.equal(deleteQueries.length, 0);
|
||
});
|
||
|
||
it('分批循环删除到 affectedRows=0 为止,批数与总删除行数统计正确', async () => {
|
||
const { service, captures } = createHarness(
|
||
sequenceQueryHandler([{ id: 5000 }], [ 2000, 2000, 1000, 0 ])
|
||
);
|
||
|
||
const { result: summary, sleepDelays } = await runWithStubbedTimers(
|
||
() => service.cleanupExpiredSystemLogs()
|
||
);
|
||
|
||
assert.equal(summary.skipped, false);
|
||
assert.equal(summary.boundary_id, 5000);
|
||
assert.equal(summary.batch_count, 3);
|
||
assert.equal(summary.deleted_rows, 5000);
|
||
|
||
const deleteQueries = captures.queries.filter(query => /^DELETE/.test(query.sql));
|
||
// 3 个有效批次 + 1 个终止探测批(affectedRows=0)共 4 次 DELETE
|
||
assert.equal(deleteQueries.length, 4);
|
||
for (const query of deleteQueries) {
|
||
assert.match(query.sql, /WHERE id <= 5000/);
|
||
assert.match(query.sql, /LIMIT 2000$/);
|
||
}
|
||
|
||
// 批间休眠存在:第 2 批起每批前休眠一次(共 3 次),且默认区间 100~500ms
|
||
assert.equal(sleepDelays.length, 3);
|
||
for (const delay of sleepDelays) {
|
||
assert.ok(delay >= 100 && delay <= 500, `休眠时长 ${delay} 应在 100~500ms 区间内`);
|
||
}
|
||
});
|
||
|
||
it('边界行本身应被删除:DELETE 使用 <= 语义', async () => {
|
||
const { service, captures } = createHarness(
|
||
sequenceQueryHandler([{ id: 42 }], [ 1, 0 ])
|
||
);
|
||
|
||
const summary = await service.cleanupExpiredSystemLogs({ sleepMinMs: 0, sleepMaxMs: 0 });
|
||
|
||
assert.equal(summary.boundary_id, 42);
|
||
assert.equal(summary.deleted_rows, 1);
|
||
const deleteQuery = captures.queries.find(query => /^DELETE/.test(query.sql));
|
||
assert.match(deleteQuery.sql, /WHERE id <= 42/);
|
||
});
|
||
|
||
it('边界存在但首批 affectedRows=0 时统计为 0 批 0 行', async () => {
|
||
const { service } = createHarness(sequenceQueryHandler([{ id: 99 }], [ 0 ]));
|
||
|
||
const summary = await service.cleanupExpiredSystemLogs();
|
||
|
||
assert.equal(summary.skipped, false);
|
||
assert.equal(summary.boundary_id, 99);
|
||
assert.equal(summary.batch_count, 0);
|
||
assert.equal(summary.deleted_rows, 0);
|
||
});
|
||
|
||
it('参数非法时回退默认值:retainDays=-1 → INTERVAL 60 DAY、batchSize=-5 → LIMIT 2000', async () => {
|
||
const { service, captures } = createHarness(
|
||
sequenceQueryHandler([{ id: 9 }], [ 1, 0 ])
|
||
);
|
||
|
||
await service.cleanupExpiredSystemLogs({
|
||
retainDays: -1,
|
||
batchSize: -5,
|
||
sleepMinMs: 0,
|
||
sleepMaxMs: 0,
|
||
});
|
||
|
||
const selectQuery = captures.queries.find(query => /^SELECT/.test(query.sql));
|
||
const deleteQuery = captures.queries.find(query => /^DELETE/.test(query.sql));
|
||
assert.match(selectQuery.sql, /INTERVAL 60 DAY/);
|
||
assert.match(deleteQuery.sql, /LIMIT 2000$/);
|
||
});
|
||
|
||
it('解析优先级:options.retainDays 优先于 config.systemLogCleanup.retainDays', async () => {
|
||
const { service, ctx, captures } = createHarness(
|
||
sequenceQueryHandler([{ id: 9 }], [ 1, 0 ])
|
||
);
|
||
ctx.app.config = { systemLogCleanup: { retainDays: 90 } };
|
||
|
||
await service.cleanupExpiredSystemLogs({
|
||
retainDays: 30,
|
||
sleepMinMs: 0,
|
||
sleepMaxMs: 0,
|
||
});
|
||
|
||
const selectQuery = captures.queries.find(query => /^SELECT/.test(query.sql));
|
||
assert.match(selectQuery.sql, /INTERVAL 30 DAY/);
|
||
});
|
||
|
||
it('解析优先级:未传 options.retainDays 时读取 config.systemLogCleanup.retainDays', async () => {
|
||
const { service, ctx, captures } = createHarness(
|
||
sequenceQueryHandler([{ id: 9 }], [ 1, 0 ])
|
||
);
|
||
ctx.app.config = { systemLogCleanup: { retainDays: 90 } };
|
||
|
||
await service.cleanupExpiredSystemLogs({ sleepMinMs: 0, sleepMaxMs: 0 });
|
||
|
||
const selectQuery = captures.queries.find(query => /^SELECT/.test(query.sql));
|
||
assert.match(selectQuery.sql, /INTERVAL 90 DAY/);
|
||
});
|
||
|
||
it('解析优先级:options 与 config 均未提供保留天数时回退默认 60', async () => {
|
||
const { service, captures } = createHarness(
|
||
sequenceQueryHandler([{ id: 9 }], [ 1, 0 ])
|
||
);
|
||
|
||
await service.cleanupExpiredSystemLogs({ sleepMinMs: 0, sleepMaxMs: 0 });
|
||
|
||
const selectQuery = captures.queries.find(query => /^SELECT/.test(query.sql));
|
||
assert.match(selectQuery.sql, /INTERVAL 60 DAY/);
|
||
});
|
||
|
||
it('解析优先级:options.retainDays 非法时回退默认 60(不读取 config 值)', async () => {
|
||
const { service, ctx, captures } = createHarness(
|
||
sequenceQueryHandler([{ id: 9 }], [ 1, 0 ])
|
||
);
|
||
ctx.app.config = { systemLogCleanup: { retainDays: 90 } };
|
||
|
||
await service.cleanupExpiredSystemLogs({
|
||
retainDays: 0,
|
||
sleepMinMs: 0,
|
||
sleepMaxMs: 0,
|
||
});
|
||
|
||
const selectQuery = captures.queries.find(query => /^SELECT/.test(query.sql));
|
||
assert.match(selectQuery.sql, /INTERVAL 60 DAY/);
|
||
});
|
||
|
||
it('旧参数名 retentionDays 不再生效:回退默认 60', async () => {
|
||
const { service, captures } = createHarness(
|
||
sequenceQueryHandler([{ id: 9 }], [ 1, 0 ])
|
||
);
|
||
|
||
await service.cleanupExpiredSystemLogs({
|
||
retentionDays: 30,
|
||
sleepMinMs: 0,
|
||
sleepMaxMs: 0,
|
||
});
|
||
|
||
const selectQuery = captures.queries.find(query => /^SELECT/.test(query.sql));
|
||
assert.match(selectQuery.sql, /INTERVAL 60 DAY/);
|
||
});
|
||
|
||
it('sleepMaxMs < sleepMinMs 时钳制为 sleepMinMs', async () => {
|
||
const { service } = createHarness(sequenceQueryHandler([{ id: 9 }], [ 1, 0 ]));
|
||
|
||
const { sleepDelays } = await runWithStubbedTimers(
|
||
() => service.cleanupExpiredSystemLogs({ sleepMinMs: 150, sleepMaxMs: 50 })
|
||
);
|
||
|
||
assert.equal(sleepDelays.length, 1);
|
||
assert.equal(sleepDelays[0], 150);
|
||
});
|
||
});
|
||
|
||
describe('硬删除语义(静态检查)', () => {
|
||
it('必须走 ctx.model.query 原生 DELETE,禁止 SystemLog.destroy 软删除', () => {
|
||
const source = fs.readFileSync(SERVICE_SOURCE_PATH, 'utf8');
|
||
assert.match(source, /DELETE FROM \$\{SYSTEM_LOG_TABLE\}/);
|
||
assert.doesNotMatch(source, /\.destroy\s*\(/);
|
||
});
|
||
});
|
||
});
|