job touch
This commit is contained in:
111
src/jobs/jobs.js
111
src/jobs/jobs.js
@@ -15,115 +15,9 @@
|
|||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
const User = require('../modules/user/user.model');
|
|
||||||
const Fund = require('../modules/fund/fund.model');
|
const Fund = require('../modules/fund/fund.model');
|
||||||
const Action = require('../modules/action/action.model');
|
|
||||||
const system = require('../modules/system/system.model');
|
|
||||||
const { createAction } = require('../modules/action/action.handler');
|
|
||||||
const { createNewFund } = require('../modules/fund/fund.handler');
|
|
||||||
const exec = require('child_process').exec;
|
const exec = require('child_process').exec;
|
||||||
|
|
||||||
const tokenReleaseRate = 0.02;
|
|
||||||
async function awardMine(user, fund) {
|
|
||||||
if (!user || !fund || fund.fundList.length === 0) return 0;
|
|
||||||
let sum = 0;
|
|
||||||
for (let i = 0, len = fund.fundList.length; i < len; ++i) {
|
|
||||||
const released = fund.releasedAmout || 0;
|
|
||||||
if (released === fund.releasedAmout) {
|
|
||||||
await Fund.findByIdAndUpdate(fund.id, {
|
|
||||||
$set: {
|
|
||||||
[`fundList.${i}.status`]: 2 //标记失效
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
sum += +fund.fundList[i].amount * tokenReleaseRate;
|
|
||||||
}
|
|
||||||
return sum;
|
|
||||||
}
|
|
||||||
function awardInvite(user, fund, allInviteReward) {
|
|
||||||
const rewardInvite = allInviteReward - (fund.option && fund.option.usedGroupReward || 0); // 求增量: 总额 - 已领取总额(fundSum)
|
|
||||||
// 挖矿奖励 = 充值金额奖励 + 社区奖励
|
|
||||||
return rewardInvite;
|
|
||||||
}
|
|
||||||
// fund.option.usedGroupReward : 已领取的社群奖励总额
|
|
||||||
const doReward = async (user, fund) => {
|
|
||||||
// 1.对于充值的fundList的每一项,按照2%进行发放,累加到asset.vic,添加action,类型为1
|
|
||||||
// 2.对于社团成员进行人头数目,按照10vic/人进行奖励,累加到asset.vic,添加action,类型为2
|
|
||||||
if (!user || !fund) return 0;
|
|
||||||
const allInviteReward = (+user.group.length || 0) * 5;
|
|
||||||
const rewardMine = await awardMine(user, fund);
|
|
||||||
const rewardInvite = awardInvite(user, fund, allInviteReward);
|
|
||||||
const reward = rewardMine + rewardInvite;
|
|
||||||
|
|
||||||
reward && await Fund.findByIdAndUpdate(fund.id, {
|
|
||||||
$inc: {
|
|
||||||
'asset.vic': reward,
|
|
||||||
},
|
|
||||||
$set: {
|
|
||||||
'option.usedGroupReward': rewardInvite, // 本次的奖励值累加到已领取团队奖励总和内
|
|
||||||
'asset.usdt': allInviteReward, // 用来记录所有社群奖励总额
|
|
||||||
'lastMine': new Date().toISOString()
|
|
||||||
}
|
|
||||||
}).then(() => {
|
|
||||||
rewardMine > 0 && createAction(1, {
|
|
||||||
userId: user.id,
|
|
||||||
tokenType: 'vic',
|
|
||||||
amount: rewardMine,
|
|
||||||
remain: fund.asset
|
|
||||||
}).save();
|
|
||||||
rewardInvite > 0 && createAction(2, {
|
|
||||||
userId: user.id,
|
|
||||||
tokenType: 'vic',
|
|
||||||
amount: rewardInvite,
|
|
||||||
remain: fund.asset
|
|
||||||
}).save();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
const mock = {
|
|
||||||
touch: () => {console.log('touch');},
|
|
||||||
done: () => {console.log('done');},
|
|
||||||
};
|
|
||||||
async function mine (job = mock, done = mock) {
|
|
||||||
//todo: 程序内检查是否可以执行,保证不会启动时错误的自动执行一次
|
|
||||||
mylog.info('[Schedule Job] 挖矿程序 开始定时计算作业...');
|
|
||||||
let currentIndex = await system.findOne({
|
|
||||||
key: 'userIndex4job'
|
|
||||||
}).exec();
|
|
||||||
currentIndex = currentIndex.value;
|
|
||||||
if (typeof currentIndex !== 'number') {
|
|
||||||
currentIndex = 0;
|
|
||||||
await new system({
|
|
||||||
key: 'userIndex4job',
|
|
||||||
value: currentIndex
|
|
||||||
}).save();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 批量更新,每次十个
|
|
||||||
const userFundList = await Fund.find().sort('_id').limit(10).skip(currentIndex);
|
|
||||||
typeof job.touch === 'function' && job.touch();
|
|
||||||
mylog.info('touch');
|
|
||||||
if (!userFundList || !Array.isArray(userFundList)) return 0;
|
|
||||||
for (let i = 0, len = userFundList.length; i < len; ++i) {
|
|
||||||
const fund = userFundList[i]; // 一条数据库fund表记录
|
|
||||||
if (fund.lastMine && new Date(fund.lastMine).getDate() === new Date().getDate()) {
|
|
||||||
mylog.info('已经计算过...跳过');
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const user = await User.findById(fund.userId).exec();
|
|
||||||
mylog.info('开始更新用户: ', user.id);
|
|
||||||
await doReward(user, fund);
|
|
||||||
mylog.info('用户: ', user.id, '更新完毕');
|
|
||||||
typeof job.touch === 'function' && job.touch();
|
|
||||||
await system.updateOne({ key: 'userIndex4job' }, {
|
|
||||||
$set: {
|
|
||||||
value: currentIndex
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
mylog.info('收益计算分配完成');
|
|
||||||
typeof done === 'function' && done('收益计算分配完成');
|
|
||||||
}
|
|
||||||
async function confirm (job = mock, done = mock) {
|
async function confirm (job = mock, done = mock) {
|
||||||
mylog.info('[Schedule Job] 矿晶确认程序开始执行......');
|
mylog.info('[Schedule Job] 矿晶确认程序开始执行......');
|
||||||
const unconfirmed = await Fund.find({
|
const unconfirmed = await Fund.find({
|
||||||
@@ -160,7 +54,12 @@ async function mineJob (job = mock, done = mock) {
|
|||||||
mylog.info('child process exited with code ' + code); // 输出到控制台
|
mylog.info('child process exited with code ' + code); // 输出到控制台
|
||||||
mylog.info('挖矿程序执行完毕');
|
mylog.info('挖矿程序执行完毕');
|
||||||
});
|
});
|
||||||
|
rl.on('message', function () {
|
||||||
|
job.touch();
|
||||||
|
});
|
||||||
|
rl.on('exit', function () {
|
||||||
typeof done === 'function' && done('挖矿程序执行完毕');
|
typeof done === 'function' && done('挖矿程序执行完毕');
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const jobs = [
|
const jobs = [
|
||||||
|
|||||||
@@ -56,7 +56,14 @@ async function awardMine(user, fund) {
|
|||||||
});
|
});
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
sum += +fund.fundList[i].amount * tokenReleaseRate;
|
const profit = +fund.fundList[i].amount * tokenReleaseRate;
|
||||||
|
await Fund.findByIdAndUpdate(fund.id, {
|
||||||
|
$inc: {
|
||||||
|
[`fundList.${i}.mineCount`]: 1,
|
||||||
|
[`fundList.${i}.releasedAmout`]: profit,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
sum += profit;
|
||||||
}
|
}
|
||||||
return sum.toFixed(2);
|
return sum.toFixed(2);
|
||||||
}
|
}
|
||||||
@@ -156,14 +163,17 @@ async function mine (job = mock, done = mock) {
|
|||||||
value: -1
|
value: -1
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
typeof process.send === 'function' && process.send('tick');
|
||||||
}
|
}
|
||||||
currentIndex += 10;
|
currentIndex += 10;
|
||||||
probe.currentIndex = currentIndex;
|
probe.currentIndex = currentIndex;
|
||||||
}
|
}
|
||||||
mylog.info('收益计算分配完成');
|
mylog.info('收益计算分配完成');
|
||||||
|
typeof process.send === 'function' && process.send('tick');
|
||||||
await system.findOneAndUpdate({
|
await system.findOneAndUpdate({
|
||||||
key: 'userIndex4job'
|
key: 'userIndex4job'
|
||||||
}, { $set: { value: 0 } }).exec();
|
}, { $set: { value: 0 } }).exec();
|
||||||
|
typeof process.send === 'function' && process.send('tick');
|
||||||
await system.findOneAndUpdate({
|
await system.findOneAndUpdate({
|
||||||
key: 'mineCount',
|
key: 'mineCount',
|
||||||
}, {
|
}, {
|
||||||
@@ -171,12 +181,13 @@ async function mine (job = mock, done = mock) {
|
|||||||
value: 1
|
value: 1
|
||||||
}
|
}
|
||||||
}, { upsert: true });
|
}, { upsert: true });
|
||||||
|
typeof process.send === 'function' && process.send('tick');
|
||||||
await system.findOneAndUpdate({
|
await system.findOneAndUpdate({
|
||||||
key: 'lastMine',
|
key: 'lastMine',
|
||||||
}, {
|
}, {
|
||||||
value: new Date().toISOString()
|
value: new Date().toISOString()
|
||||||
}, { upsert: true });
|
}, { upsert: true });
|
||||||
|
typeof process.send === 'function' && process.send('tick');
|
||||||
typeof done === 'function' && done('收益计算分配完成');
|
typeof done === 'function' && done('收益计算分配完成');
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -149,9 +149,9 @@ module.exports = {
|
|||||||
async handleVreq (req, res) {
|
async handleVreq (req, res) {
|
||||||
const { adminId } = res.locals.user;
|
const { adminId } = res.locals.user;
|
||||||
if (!adminId) return res.json({ code: 1, msg: '非法访问' });
|
if (!adminId) return res.json({ code: 1, msg: '非法访问' });
|
||||||
const { vreqId } = req.body.query;
|
const { vreqId, op, info } = req.body.query; // op 1为通过验证 0为拒绝验证
|
||||||
return await Vreq.findById(vreqId).then(vreq => {
|
return await Vreq.findById(vreqId).then(vreq => {
|
||||||
handleReq(vreq, adminId).then(data => {
|
handleReq(vreq, adminId, op, info).then(data => {
|
||||||
return res.json(data);
|
return res.json(data);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ function createVerifyReq (data) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleReq (vreq, adminName) {
|
async function handleReq (vreq, adminName, op, info) {
|
||||||
|
if (+op === 1) { // 认证通过
|
||||||
await User.findByIdAndUpdate(vreq.userId, {
|
await User.findByIdAndUpdate(vreq.userId, {
|
||||||
verifyLevel: vreq.verifyLevel
|
verifyLevel: vreq.verifyLevel
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
@@ -20,7 +21,8 @@ async function handleReq (vreq, adminName) {
|
|||||||
err: err.msg
|
err: err.msg
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
await vreq.updateOne({ status: 1, info: '审核通过', handler: adminName })
|
const resInfo = info || '审核通过';
|
||||||
|
await vreq.updateOne({ status: 1, info: resInfo, handler: adminName })
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
mylog.error(err);
|
mylog.error(err);
|
||||||
return {
|
return {
|
||||||
@@ -33,6 +35,22 @@ async function handleReq (vreq, adminName) {
|
|||||||
code: 0,
|
code: 0,
|
||||||
msg: '认证成功'
|
msg: '认证成功'
|
||||||
};
|
};
|
||||||
|
} else if (+op === 0) {
|
||||||
|
const resInfo = info || '审核不通过';
|
||||||
|
await vreq.updateOne({ status: 2, info: resInfo, handler: adminName })
|
||||||
|
.catch(err => {
|
||||||
|
mylog.error(err);
|
||||||
|
return {
|
||||||
|
code: 2,
|
||||||
|
msg: 'error',
|
||||||
|
err: err.msg
|
||||||
|
};
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
code: 0,
|
||||||
|
msg: '认证成功'
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
Reference in New Issue
Block a user