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

fix: lockUpdate

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