Commit dddab3dc authored by 徐光星's avatar 徐光星

feat: 增加定时任务删除历史记录表半年前的数据

parent ab0c0163
import { Subscription } from 'egg';
class clearInvalidPageHistoryRecords extends Subscription {
// 通过 schedule 属性来设置定时任务的执行间隔等配置
static get schedule() {
return {
cronOptions: {
tz: 'Asia/Shanghai'
},
immediate: false,
cron: '0 0 2 L * *', // 每月的最后一天凌晨2点整执行
type: 'all', // 指定所有的 worker 都需要执行
};
}
async subscribe() {
this.ctx.service.records.clearInvalidPageHistoryRecords();
}
}
module.exports = clearInvalidPageHistoryRecords;
\ No newline at end of file
...@@ -6,4 +6,22 @@ export default class RecordsService extends Service { ...@@ -6,4 +6,22 @@ export default class RecordsService extends Service {
super(ctx); super(ctx);
this.context = ctx; this.context = ctx;
} }
async clearInvalidPageHistoryRecords() {
// 操作记录表直接删除半年以上的操作记录
const limitDate = new Date(new Date().getTime() - 180 * 24 * 3600 * 1000).toLocaleDateString().replace(/\//g, '-')
try {
this.context.logger.info(`开始删除页面历史记录,删除范围为日期在${limitDate}之前的所有记录`);
await this.context.model.PageHistory.destroy({
where: {
updated_at: {
$lte: new Date(limitDate)
}
}
});
this.context.logger.info(`页面历史记录删除成功,删除范围为日期在${limitDate}之前的所有记录`);
} catch (err) {
this.context.logger.info(`页面历史记录删除失败,失败原因: ${JSON.stringify(err)}`);
}
return;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment