'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} 删除的记录数 */ 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; }, };