oms/backend/app/schedule/clear_request_logs.js

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