From de4e1d2683f8f734e25994b235471f3b7543715c Mon Sep 17 00:00:00 2001 From: chaosBreaking Date: Fri, 25 Oct 2019 11:12:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B8=85=E7=90=86=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clean.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 clean.js diff --git a/clean.js b/clean.js new file mode 100644 index 0000000..6bf7049 --- /dev/null +++ b/clean.js @@ -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();