'use strict'; /** * 定时清理 OpenAPI 幂等记录:每日凌晨 2 点执行,删除超过保留天数(默认 7 天)的记录。 * * 幂等只需要覆盖调用方的重试窗口,过期记录可安全清理,避免表无限增长。 */ const KEEP_DAYS = Number(process.env.OPENAPI_IDEMPOTENCY_KEEP_DAYS) || 7; module.exports = { schedule: { cron: '0 0 2 * * *', type: 'worker', disable: false, }, /** * 执行清理 * @param {Egg.Context} ctx egg context(定时任务上下文) * @return {Promise} 删除的记录数 */ async task(ctx) { const Op = ctx.app.Sequelize.Op; const threshold = new Date(Date.now() - KEEP_DAYS * 24 * 60 * 60 * 1000); const count = await ctx.model.OpenapiRequestIdempotency.destroy({ where: { created_at: { [Op.lt]: threshold } }, }); ctx.logger.info(`[openapi-idempotency] 已清理 ${KEEP_DAYS} 天前的幂等记录,共删除 ${count} 条`); return count; }, };