feat: 清理程序
This commit is contained in:
88
clean.js
Normal file
88
clean.js
Normal file
@@ -0,0 +1,88 @@
|
||||
'use strict';
|
||||
const mongoose = require('mongoose');
|
||||
const Fund = require('./src/modules/fund/fund.model');
|
||||
const Action = require('./src/modules/action/action.model');
|
||||
const PrettyStream = require('bunyan-pretty-colors');
|
||||
const bunyan = require('bunyan');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
const prettyStdOut = new PrettyStream();
|
||||
|
||||
if (!fs.existsSync('clean')) {
|
||||
fs.mkdirSync('clean');
|
||||
}
|
||||
const logger = function (option) {
|
||||
option = option || {};
|
||||
return bunyan.createLogger({
|
||||
name: "log",
|
||||
src: false,
|
||||
streams: [
|
||||
{
|
||||
level: 'info',
|
||||
stream: prettyStdOut
|
||||
},
|
||||
{
|
||||
level: 'info',
|
||||
type: 'rotating-file',
|
||||
path: path.join(option.root, option.file || 'info.log'),
|
||||
period: '1d', // daily rotation
|
||||
count: 365 // keep 30 days
|
||||
}
|
||||
]
|
||||
});
|
||||
};
|
||||
const filename = 'clean' + new Date().getFullYear() + new Date().getMonth() + new Date().getDate();
|
||||
const mylog = logger(({ root: 'clean', file: filename }));
|
||||
|
||||
const probe = {
|
||||
currentIndex: 0
|
||||
};
|
||||
async function clean () {
|
||||
let allFund = await Fund.find({ fundSum: { $gt: 0 } });
|
||||
// 批量更新,每次十个
|
||||
for (let currentIndex = 0; currentIndex < allFund.length; currentIndex++) {
|
||||
const userFund = allFund[currentIndex];
|
||||
const fundList = userFund.fundList;
|
||||
const boughtAmountSum = fundList.reduce((sum, fund) => sum + +fund.amount, 0);
|
||||
const releasedAmout = (await Action.find({ type: '1', userId: userFund.userId })).reduce((sum, action) => sum + +action.amount, 0);
|
||||
const rest = boughtAmountSum - releasedAmout;
|
||||
mylog.info('No. ', currentIndex, '用户' + userFund.userId, '剩余 ', rest);
|
||||
await Fund.findByIdAndUpdate(userFund.id, { fundList: [], 'option.restNeetToRelease': rest });
|
||||
// await Fund.findByIdAndUpdate(userFund.id, { fundList: [], $inc: {'asset.vic': userFund.option.restNeetToRelease } });
|
||||
probe.currentIndex = currentIndex;
|
||||
}
|
||||
process.exit();
|
||||
}
|
||||
|
||||
const start = async (job, done) => {
|
||||
const SysConfig = require('./configSec');
|
||||
mongoose.set('useCreateIndex', true);
|
||||
const { DB_USER_NAME, DB_PASSWD, DB_HOST, DB_PORT, DB_NAME } = SysConfig;
|
||||
const db = mongoose.connection;
|
||||
db.on('error', mylog.error);
|
||||
db.once('open', function() {
|
||||
mylog.info('数据库连接成功...');
|
||||
});
|
||||
mylog.info('开始连接数据库...');
|
||||
await mongoose.connect(`mongodb://${DB_USER_NAME}:${DB_PASSWD}@${DB_HOST}:${DB_PORT}/${DB_NAME}`, {
|
||||
useNewUrlParser: true,
|
||||
bufferMaxEntries: 0,
|
||||
autoReconnect: true,
|
||||
useFindAndModify: false,
|
||||
useUnifiedTopology: true
|
||||
});
|
||||
const beforeExit = async (msg) => {
|
||||
mylog.info('意外退出', msg);
|
||||
mylog.info('进行到第'+ probe.currentIndex + '个用户');
|
||||
process.exit(0);
|
||||
};
|
||||
process.on('SIGINT', beforeExit);
|
||||
process.on('uncaughtException', beforeExit);
|
||||
process.on('unhandledRejection', beforeExit);
|
||||
typeof process.send === 'function' && process.send('子进程开始执行mine');
|
||||
clean(job, done);
|
||||
typeof process.send === 'function' && process.send('子进程mine执行完毕');
|
||||
};
|
||||
|
||||
start();
|
||||
Reference in New Issue
Block a user