'use strict'; /** * 定时清理请求日志:每日凌晨 1 点执行,删除超过保留天数(默认 60 天)的记录 */ module.exports = { schedule: { cron: '0 0 1 * * *', type: 'worker', disable: false, }, /** * 执行清理 * @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; }, };