- 新增 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 个用例),覆盖边界定位、保留天数校验、配置开关与防重入等场景
196 lines
6.8 KiB
JavaScript
196 lines
6.8 KiB
JavaScript
'use strict';
|
||
|
||
const assert = require('node:assert/strict');
|
||
const fs = require('node:fs');
|
||
const path = require('node:path');
|
||
const scheduleFactory = require('../../../app/schedule/system_log_cleanup');
|
||
|
||
/**
|
||
* 创建定时任务测试上下文(纯 mock,不连接任何数据库)。
|
||
*
|
||
* @param {Object} [options={}] 测试配置。
|
||
* @param {Object} [options.config] app.config(默认 { systemLogCleanup: {} })。
|
||
* @param {boolean} [options.running] 初始防重入标记。
|
||
* @param {boolean} [options.serviceThrows] 清理服务是否抛错。
|
||
* @return {Object} { app, ctx, logs, getCleanupCallCount, getCleanupCallArgs }
|
||
*/
|
||
function createScheduleCtx(options = {}) {
|
||
const config = options.config !== undefined ? options.config : { systemLogCleanup: {} };
|
||
const logs = { info: [], warn: [], error: [] };
|
||
let cleanupCallCount = 0;
|
||
let cleanupCallArgs = null;
|
||
|
||
const app = {
|
||
_systemLogCleanupRunning: Boolean(options.running),
|
||
config,
|
||
};
|
||
const ctx = {
|
||
app,
|
||
logger: {
|
||
info(message) { logs.info.push(message); },
|
||
warn(message) { logs.warn.push(message); },
|
||
error(message) { logs.error.push(message); },
|
||
},
|
||
service: {
|
||
systems: {
|
||
systemLog: {
|
||
async cleanupExpiredSystemLogs(args) {
|
||
cleanupCallCount += 1;
|
||
cleanupCallArgs = args;
|
||
if (options.serviceThrows) {
|
||
throw new Error('模拟清理失败');
|
||
}
|
||
return {
|
||
skipped: false,
|
||
boundary_id: 100,
|
||
batch_count: 2,
|
||
deleted_rows: 4000,
|
||
duration_ms: 1,
|
||
};
|
||
},
|
||
},
|
||
},
|
||
},
|
||
};
|
||
|
||
return {
|
||
app,
|
||
ctx,
|
||
logs,
|
||
getCleanupCallCount() { return cleanupCallCount; },
|
||
getCleanupCallArgs() { return cleanupCallArgs; },
|
||
};
|
||
}
|
||
|
||
describe('test/app/schedule/system_log_cleanup.test.js', () => {
|
||
describe('任务注册', () => {
|
||
it('每日凌晨 03:00 单 worker 执行,默认启用', () => {
|
||
const schedule = scheduleFactory({ config: { systemLogCleanup: {} } });
|
||
|
||
assert.equal(schedule.schedule.cron, '0 0 3 * * *');
|
||
assert.equal(schedule.schedule.type, 'worker');
|
||
assert.equal(schedule.schedule.immediate, false);
|
||
assert.equal(schedule.schedule.disable, false);
|
||
});
|
||
|
||
it('config.systemLogCleanup.enable=false 时任务禁用', () => {
|
||
const schedule = scheduleFactory({ config: { systemLogCleanup: { enable: false } } });
|
||
assert.equal(schedule.schedule.disable, true);
|
||
});
|
||
|
||
it('配置整体缺失时默认启用', () => {
|
||
const schedule = scheduleFactory({ config: {} });
|
||
assert.equal(schedule.schedule.disable, false);
|
||
});
|
||
|
||
it('cron 从 config.systemLogCleanup.cron 读取,缺失或非法时回退每日 03:00', () => {
|
||
const custom = scheduleFactory({ config: { systemLogCleanup: { cron: '0 */10 * * * *' } } });
|
||
assert.equal(custom.schedule.cron, '0 */10 * * * *');
|
||
|
||
const fallback = scheduleFactory({ config: { systemLogCleanup: { cron: 0 } } });
|
||
assert.equal(fallback.schedule.cron, '0 0 3 * * *');
|
||
});
|
||
});
|
||
|
||
describe('task 执行', () => {
|
||
it('正常执行:仅透传 batchSize/sleepMinMs/sleepMaxMs(不再解析 retainDays)、完成日志输出、finally 释放防重入标记', async () => {
|
||
const harness = createScheduleCtx({
|
||
config: {
|
||
systemLogCleanup: {
|
||
retainDays: 90,
|
||
batchSize: 500,
|
||
sleepMinMs: 10,
|
||
sleepMaxMs: 20,
|
||
},
|
||
},
|
||
});
|
||
const schedule = scheduleFactory({ config: harness.app.config });
|
||
|
||
await schedule.task(harness.ctx);
|
||
|
||
assert.equal(harness.getCleanupCallCount(), 1);
|
||
assert.deepEqual(harness.getCleanupCallArgs(), {
|
||
batchSize: 500,
|
||
sleepMinMs: 10,
|
||
sleepMaxMs: 20,
|
||
});
|
||
assert.equal(harness.app._systemLogCleanupRunning, false, '任务结束后应释放防重入标记');
|
||
assert.equal(harness.logs.info.some(message => message.includes('本轮完成')), true);
|
||
});
|
||
|
||
it('防重入:上一轮未结束时跳过本轮且不调用服务', async () => {
|
||
const harness = createScheduleCtx({ running: true });
|
||
const schedule = scheduleFactory({ config: harness.app.config });
|
||
|
||
await schedule.task(harness.ctx);
|
||
|
||
assert.equal(harness.getCleanupCallCount(), 0);
|
||
assert.equal(harness.app._systemLogCleanupRunning, true, '跳过时不应改动防重入标记');
|
||
assert.equal(harness.logs.info.some(message => message.includes('跳过')), true);
|
||
});
|
||
|
||
it('服务异常时仅记录错误日志、不向上抛出、finally 复位', async () => {
|
||
const harness = createScheduleCtx({ serviceThrows: true });
|
||
const schedule = scheduleFactory({ config: harness.app.config });
|
||
|
||
await assert.doesNotReject(() => schedule.task(harness.ctx));
|
||
|
||
assert.equal(harness.getCleanupCallCount(), 1);
|
||
assert.equal(harness.app._systemLogCleanupRunning, false);
|
||
assert.equal(harness.logs.error.length, 1);
|
||
assert.match(harness.logs.error[0], /执行失败/);
|
||
});
|
||
|
||
it('配置非法时回退默认值:batchSize=2000、sleepMinMs=100、sleepMaxMs=500', async () => {
|
||
const harness = createScheduleCtx({
|
||
config: {
|
||
systemLogCleanup: {
|
||
batchSize: -1,
|
||
sleepMinMs: -5,
|
||
sleepMaxMs: -5,
|
||
},
|
||
},
|
||
});
|
||
const schedule = scheduleFactory({ config: harness.app.config });
|
||
|
||
await schedule.task(harness.ctx);
|
||
|
||
assert.deepEqual(harness.getCleanupCallArgs(), {
|
||
batchSize: 2000,
|
||
sleepMinMs: 100,
|
||
sleepMaxMs: 500,
|
||
});
|
||
});
|
||
|
||
it('配置整体缺失时任务按默认参数执行', async () => {
|
||
const harness = createScheduleCtx({ config: {} });
|
||
const schedule = scheduleFactory({ config: harness.app.config });
|
||
|
||
await schedule.task(harness.ctx);
|
||
|
||
assert.equal(harness.getCleanupCallCount(), 1);
|
||
assert.deepEqual(harness.getCleanupCallArgs(), {
|
||
batchSize: 2000,
|
||
sleepMinMs: 100,
|
||
sleepMaxMs: 500,
|
||
});
|
||
});
|
||
});
|
||
|
||
describe('配置文件静态回归(定时恢复每日 03:00 扫描、保留 60 天)', () => {
|
||
const DEFAULT_CONFIG_PATH = path.join(__dirname, '../../../config/config.default.js');
|
||
const LOCAL_CONFIG_PATH = path.join(__dirname, '../../../config/config.local.js');
|
||
|
||
it('config.default.js 提供 systemLogCleanup 默认 cron 03:00 与 retainDays 60', () => {
|
||
const source = fs.readFileSync(DEFAULT_CONFIG_PATH, 'utf8');
|
||
assert.match(source, /cron: '0 0 3 \* \* \*'/);
|
||
assert.match(source, /retainDays: 60/);
|
||
});
|
||
|
||
it('config.local.js 不得再覆盖 systemLogCleanup(每分钟 cron 覆盖已移除)', () => {
|
||
const source = fs.readFileSync(LOCAL_CONFIG_PATH, 'utf8');
|
||
assert.doesNotMatch(source, /systemLogCleanup/);
|
||
});
|
||
});
|
||
});
|