oms/backend/app/schedule/clear_request_logs.js
liufumingren 6facd9c6f3 perf(log): 日志表按天分区,只保留 10 天
callback_logs / api_outbound_logs / request_logs 只增不减,DELETE 只标记删除、
磁盘不还。改为按 TO_DAYS(created_at) 做天分区,用 DROP PARTITION 物理删除。

- 迁移 20261107080000:三张日志表主键改为 (id, created_at)(MySQL 要求分区键
  包含在主键里),分区布局 p_old + 最近 10 天 + 未来 2 天 + pmax
- 新增 logRetention service:补未来分区(REORGANIZE pmax)→ DROP 过期分区;
  表若未分区则退化为分批 DELETE + OPTIMIZE 兜底
- 新增每日任务 purgeApiLogs(03:30):clean_request_logs 改为 disable,
  避免两条清理逻辑抢同一张表
- 保留天数默认 10 天,可用 LOG_RETENTION_DAYS 覆盖
- 文档补分区口径、为什么要 DROP PARTITION、上线注意

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-07 08:43:16 +08:00

37 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 【已停用】请求日志清理:改由 `schedule/purgeApiLogs.js` 按天分区回收。
*
* 原来这里用 `DELETE WHERE created_at < 阈值`,只标记删除、磁盘不还;
* 现在 request_logs 已按天分区,`DROP PARTITION` 才是真正的物理删除(立刻释放空间),
* 且保留天数统一收敛到 `LOG_RETENTION_DAYS`(默认 10 天)。
*/
module.exports = {
schedule: {
cron: '0 0 1 * * *',
type: 'worker',
disable: true,
},
/**
* 执行清理
* @param {Egg.Context} ctx egg context(定时任务上下文)
* @return {Promise<Number>} 删除的记录数
*/
async task(ctx) {
const { keepDays } = ctx.app.config.requestLog;
const Op = ctx.app.Sequelize.Op;
const threshold = new Date(Date.now() - keepDays * 24 * 60 * 60 * 1000);
const count = await ctx.model.RequestLog.destroy({
where: {
created_at: { [Op.lt]: threshold },
},
});
ctx.logger.info(`[request-log] 已清理 ${keepDays} 天前的请求日志,共删除 ${count} 条`);
return count;
},
};