clear-task.js
1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const dayjs = require('dayjs');
const {logger} = require('../../libs/logger');
const {mysqlPool} = require('../../libs/mysql');
module.exports = async() => {
const deleteTables = [{
table: 'price_trend_day',
beforeTime: dayjs().subtract(3, 'day').unix()
}, {
table: 'price_trend_month',
beforeTime: dayjs().subtract(30, 'day').unix()
}, {
table: 'price_trend_sixty_day',
beforeTime: dayjs().subtract(60, 'day').unix()
}, {
table: 'price_trend_half_year',
beforeTime: dayjs().subtract(180, 'day').unix()
}];
try {
const results = await Promise.all(deleteTables.map(dt => {
return mysqlPool.delete(`DELETE FROM \`${dt.table}\` WHERE \`create_time\` < :beforeTime`, {
beforeTime: dt.beforeTime
});
}));
results.forEach((result, inx) => {
if (result <= 0) {
logger.error(`[price-line-clear-task]删除数据失败, ${deleteTables[inx].table}, endTime: ${deleteTables[inx].beforeTime}`);
} else {
logger.info(`[price-line-clear-task]清理成功, ${deleteTables[inx].table}, endTime: ${deleteTables[inx].beforeTime}, records: ${result}`);
}
});
} catch (error) {
logger.error(`[price-line-clear-task]删除数据错误, error: ${error.stack}`);
}
};