'use strict'; /** * 每日清理日志表(callback_logs / api_outbound_logs / request_logs)。 * * 保留天数默认 10 天(`LOG_RETENTION_DAYS` 可覆盖)。表按天分区, * 清理方式是 `DROP PARTITION`(瞬时物理删除、立刻释放磁盘),不是 DELETE: * DELETE 只标记删除,磁盘空间不会还给系统。 * * 顺带补建未来 2 天的分区,避免跨天后新数据落到 pmax 分区导致不能按天回收。 * 若某张表还没分区(例如老库未跑迁移),退化为「分批 DELETE + OPTIMIZE TABLE」。 */ let running = false; module.exports = { schedule: { // 每天 03:30 执行(低峰) cron: '0 30 3 * * *', type: 'worker', disable: false, }, /** * 执行清理 * @param {Egg.Context} ctx egg context * @return {Promise>} 各表清理结果 */ async task(ctx) { if (running) { ctx.logger.info('[log-retention] 上一轮仍在执行,跳过本次'); return { skipped: true }; } running = true; try { return await ctx.service.logRetention.runAll(); } finally { running = false; } }, };