Commit a6d6a813 authored by 汪忙忙's avatar 汪忙忙

fix: lockUpdate

parent 3d57e543
const {CODE_TYPES} = require('../../constants');
/**
* 并发更新
* @param {接收数组或对象} daoqueryArr : [{dao: userdao, query:{id:_id}}, {dao: activityprizedao, query: {id:_id}}]
* @param {接收数组或对象} daoqueryArr : [{dao: userdao, query:{_id:_id}}, {dao: activityprizedao, query: {userNick, status ...}}]
* @param {*} fn
* @param {*} times
*/
......@@ -10,7 +10,7 @@ const lockUpdate = async(daoqueryArr, fn, times=5) => {
console.log(`lockUpdate 参数错误`);
return false;
}
if (daoqueryArr instanceof Object) {
if (!daoqueryArr instanceof Array) {
daoqueryArr = [daoqueryArr];
}
let time = 0;
......@@ -26,8 +26,10 @@ const lockUpdate = async(daoqueryArr, fn, times=5) => {
if (!daoquery.dao || !daoquery.query) {
success = true;
error = true
console.log(`daoquery`, daoquery)
break
}else {
console.log(`daoquery`, daoquery)
let canupdate = await daoquery.dao.update(daoquery.query, {$set: {lockStatus: 2}});
updateRes.push(canupdate);
}
......@@ -35,10 +37,13 @@ const lockUpdate = async(daoqueryArr, fn, times=5) => {
if (error) {
return `调用lockUpdate方法参数不符合规范`
}
console.log(`updateRes`, updateRes)
// 若都锁定成功了,执行内部调用
if (!daoqueryArr.includes(0)) {
if (updateRes.every((v) => v === 1)) {
const ret = await fn();
console.log(`完成调用,解锁操作执行--------------`);
daoqueryArr.forEach(async daoquery => {
console.log(daoquery)
let ures = await daoquery.dao.update(daoquery.query, {$set: {lockStatus: 1}});
console.log(`更新lockStatus为1`, ures);
});
......@@ -54,13 +59,15 @@ const lockUpdate = async(daoqueryArr, fn, times=5) => {
console.log(e);
daoqueryArr.forEach(async daoquery => {
let ures = await daoquery.dao.update(daoquery.query, {$set: {lockStatus: 1}});
console.log(`更新lockStatus为0`, ures);
console.log(`更新lockStatus为1`, ures);
});
success = false;
}
console.log(`daoqueryArr`, daoqueryArr)
// 稍后重试
if (!success) {
daoqueryArr.forEach(async daoquery => {
console.log(daoquery)
let ures = await daoquery.dao.update(daoquery.query, {$set: {lockStatus: 1}});
console.log(`操作${times}次后更新lockStatus为1`, ures);
});
......@@ -75,6 +82,7 @@ const lockUpdate = async(daoqueryArr, fn, times=5) => {
module.exports = lockUpdate;
/**
* 调用示例
*/
......
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