Commit 40b73ea3 authored by qinhaitao's avatar qinhaitao

refactor: 💡 lockupdate

parent 37a6c434
......@@ -32,11 +32,11 @@ export default class AwardsController {
/**
* 权益重新领取
*/
receiveEnamePrize(context: IContext<IReceiveEnameParams>, {}: {}, [awardSer]: [AwardsService]): Promise<import("taobao-mini-sdk/lib/utils/resultmodel").Result<string | boolean | object | object[] | number[] | string[], false> | import("taobao-mini-sdk/lib/utils/resultmodel").Result<CodeType | IAwards, true>>;
receiveEnamePrize(context: IContext<IReceiveEnameParams>, {}: {}, [awardSer]: [AwardsService]): Promise<import("taobao-mini-sdk/lib/utils/resultmodel").Result<string | boolean | object | string[] | object[] | number[], false> | import("taobao-mini-sdk/lib/utils/resultmodel").Result<CodeType | IAwards, true>>;
/**
* 概率抽奖
*/
drawLotteryPrize(context: IContext<IParams>, { userInfo }: IControllerInfos, [awardSer]: [AwardsService]): Promise<import("taobao-mini-sdk/lib/utils/resultmodel").Result<string | boolean | object | object[] | number[] | string[], false> | import("taobao-mini-sdk/lib/utils/resultmodel").Result<{
drawLotteryPrize(context: IContext<IParams>, { userInfo }: IControllerInfos, [awardSer]: [AwardsService]): Promise<import("taobao-mini-sdk/lib/utils/resultmodel").Result<string | boolean | object | string[] | object[] | number[], false> | import("taobao-mini-sdk/lib/utils/resultmodel").Result<{
data: {
type: number;
name: string;
......
......@@ -18,5 +18,5 @@ export default class StatController {
/**
* 统计数据
*/
getStats(context: IContext<IGetStatsParams>): Promise<import("taobao-mini-sdk/lib/utils/resultmodel").Result<string | boolean | object | object[] | number[] | string[], false> | import("taobao-mini-sdk/lib/utils/resultmodel").Result<any, true>>;
getStats(context: IContext<IGetStatsParams>): Promise<import("taobao-mini-sdk/lib/utils/resultmodel").Result<string | boolean | object | string[] | object[] | number[], false> | import("taobao-mini-sdk/lib/utils/resultmodel").Result<any, true>>;
}
......@@ -15,8 +15,8 @@ declare const _default: {
list: IAwards[];
}>>;
receiveObjectPrize: (context: IContext<import("./controller/awards.controller").IReceiveOjectParams>, {}: {}, [awardSer]: [import("./service").AwardsService]) => Promise<IResult<boolean>>;
receiveEnamePrize: (context: IContext<import("./controller/awards.controller").IReceiveEnameParams>, {}: {}, [awardSer]: [import("./service").AwardsService]) => Promise<import("taobao-mini-sdk/lib/utils/resultmodel").Result<string | boolean | object | object[] | number[] | string[], false> | import("taobao-mini-sdk/lib/utils/resultmodel").Result<import("taobao-mini-sdk/lib/utils/codetypes").CodeType | IAwards, true>>;
receiveEnamePrize: (context: IContext<import("./controller/awards.controller").IReceiveEnameParams>, {}: {}, [awardSer]: [import("./service").AwardsService]) => Promise<import("taobao-mini-sdk/lib/utils/resultmodel").Result<string | boolean | object | string[] | object[] | number[], false> | import("taobao-mini-sdk/lib/utils/resultmodel").Result<import("taobao-mini-sdk/lib/utils/codetypes").CodeType | IAwards, true>>;
doHelp: (context: IContext<IParams>, { userInfo, vipInfo, activityInfo }: IControllerInfos, [userService, taskService, accessService]: [import("./service/user.service").default, import("./service").TaskService, import("./service").AccessService]) => Promise<IResult<boolean>>;
getStats: (context: IContext<import("./controller/stat.controller").IGetStatsParams>) => Promise<import("taobao-mini-sdk/lib/utils/resultmodel").Result<string | boolean | object | object[] | number[] | string[], false> | import("taobao-mini-sdk/lib/utils/resultmodel").Result<any, true>>;
getStats: (context: IContext<import("./controller/stat.controller").IGetStatsParams>) => Promise<import("taobao-mini-sdk/lib/utils/resultmodel").Result<string | boolean | object | string[] | object[] | number[], false> | import("taobao-mini-sdk/lib/utils/resultmodel").Result<any, true>>;
};
export default _default;
/** @format */
export interface IDaoQuery {
dao: IBaseDao;
query: IMongoQuery;
}
/**
* 并发更新
* @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
*/
export declare const lockUpdate: (daoqueryArr: any, fn: any, times?: number) => Promise<any>;
export declare const lockUpdate: (daoqueryArr: IDaoQuery[], fn: () => {}, times?: number) => Promise<{}>;
/**
* 调用示例
*/
This diff is collapsed.
{"version":3,"file":"lockUpdate.js","sourceRoot":"","sources":["../../../src/utils/package/lockUpdate.ts"],"names":[],"mappings":";AAAA,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEd,6CAA4C;AAE5C;;;;;GAKG;AACU,QAAA,UAAU,GAAG,UAAO,WAAW,EAAE,EAAE,EAAE,KAAS;IAAT,sBAAA,EAAA,SAAS;;QAoDzD,SAAS,OAAO,CAAC,EAAE;YACjB,OAAO,IAAI,OAAO,CAAC,UAAA,OAAO;gBACxB,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;YACzB,CAAC,CAAC,CAAA;QACJ,CAAC;;;;;oBAvDD,IAAI,OAAO,WAAW,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE;wBAClE,OAAO,CAAC,GAAG,CAAC,qCAAiB,CAAC,CAAA;wBAC9B,sBAAO,KAAK,EAAA;qBACb;oBACD,IAAI,OAAO,WAAW,KAAK,QAAQ,EAAE;wBACnC,WAAW,GAAG,CAAC,WAAW,CAAC,CAAA;qBAC5B;oBAEG,IAAI,GAAG,CAAC,CAAA;oBACZ,OAAO,CAAC,GAAG,CAAC,wDAAgB,EAAE,KAAK,CAAC,CAAA;oBAChC,OAAO,GAAG,KAAK,CAAA;;;;;;;;;oCAIX,SAAS,GAAG,EAAE,CAAA;oCAClB,WAAW,CAAC,OAAO,CAAC,UAAM,QAAQ;;;;wDAChB,qBAAM,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,EAAE,UAAU,EAAE,CAAC,EAAE,EAAE,CAAC,EAAA;;oDAAlF,SAAS,GAAG,SAAsE;oDACtF,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;;;;yCAC1B,CAA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CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,EAAE,UAAU,EAAE,CAAC,EAAE,EAAE,CAAC,EAAA;;wCAA7E,IAAI,GAAG,SAAsE;wCACjF,OAAO,CAAC,GAAG,CAAC,iBAAK,KAAK,8CAAkB,EAAE,IAAI,CAAC,CAAA;;;;6BAChD,CAAC,CAAA;wBACF,sBAAO,sBAAU,CAAC,kBAAkB,EAAA;qBACrC;;;;;CAMF,CAAA;AAED;;GAEG;AACH,MAAM;AACN,sFAAsF;AACtF,8DAA8D;AAC9D,8BAA8B;AAC9B,6BAA6B;AAC7B,SAAS;AACT,MAAM;AACN,oBAAoB;AACpB,cAAc;AACd,0CAA0C;AAC1C,IAAI;AACJ,eAAe"}
\ No newline at end of file
{"version":3,"file":"lockUpdate.js","sourceRoot":"","sources":["../../../src/utils/package/lockUpdate.ts"],"names":[],"mappings":";AAAA,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEd,6CAA4C;AAO5C;;;;;GAKG;AACU,QAAA,UAAU,GAAG,UAAO,WAAwB,EAAE,EAAY,EAAE,KAAS;IAAT,sBAAA,EAAA,SAAS;;QAmDhF,SAAS,OAAO,CAAC,EAAU;YACzB,OAAO,IAAI,OAAO,CAAC,UAAA,OAAO;gBACxB,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;YACzB,CAAC,CAAC,CAAA;QACJ,CAAC;;;;;oBAtDD,IAAI,OAAO,WAAW,KAAK,QAAQ,EAAE;wBACnC,OAAO,CAAC,GAAG,CAAC,qCAAiB,CAAC,CAAA;wBAC9B,sBAAO,KAAK,EAAA;qBACb;oBACD,IAAI,CAAC,CAAC,WAAW,YAAY,KAAK,CAAC,EAAE;wBACnC,WAAW,GAAG,CAAC,WAAW,CAAC,CAAA;qBAC5B;oBACG,KAAK,GAAG,KAAK,CAAA;oBACjB,KAAS,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,WAAW,CAA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C,IAAI,CAAC,CAAA;oCACf,sBAAO,IAAI,EAAA;;;yBACZ,EAAE,EAAE,CAAC,EAAA;wBANN,sBAAO,SAMD,EAAA;;;;CACP;AAED;;GAEG;AACH,MAAM;AACN,sFAAsF;AACtF,8DAA8D;AAC9D,8BAA8B;AAC9B,6BAA6B;AAC7B,SAAS;AACT,MAAM;AACN,oBAAoB;AACpB,cAAc;AACd,0CAA0C;AAC1C,IAAI;AACJ,eAAe"}
\ No newline at end of file
......@@ -2,40 +2,49 @@
import { CODE_TYPES } from '../../constants'
export interface IDaoQuery {
dao: IBaseDao
query: IMongoQuery
}
/**
* 并发更新
* @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
*/
export const lockUpdate = async (daoqueryArr, fn, times = 5) => {
if (typeof daoqueryArr !== 'object' && !Array.isArray(daoqueryArr)) {
export const lockUpdate = async (daoqueryArr: IDaoQuery[], fn: () => {}, times = 5) => {
if (typeof daoqueryArr !== 'object') {
console.log(`lockUpdate 参数错误`)
return false
}
if (typeof daoqueryArr === 'object') {
if (!(daoqueryArr instanceof Array)) {
daoqueryArr = [daoqueryArr]
}
let error = false
for (let i = 0; i < daoqueryArr.length; i++) {
let daoquery = daoqueryArr[i]
if (!daoquery.dao || !daoquery.query) {
error = true
console.log(`daoquery`, daoquery)
break
}
}
if (error) {
throw Error(`调用lockUpdate方法参数不符合规范`)
}
let time = 0
console.log(`进入update并发处理次数`, times)
let success = false
try {
while (time++ < times && !success) {
// 依次锁住表
let updateRes = []
daoqueryArr.forEach(async daoquery => {
let canupdate = await daoquery.dao.update(daoquery.query, { $set: { lockStatus: 2 } })
updateRes.push(canupdate)
})
let updateRes = await lock(daoqueryArr)
console.log(`updateRes`, updateRes)
// 若都锁定成功了,执行内部调用
if (!daoqueryArr.includes(0)) {
if (updateRes.every(v => v === 1)) {
const ret = await fn()
daoqueryArr.forEach(async daoquery => {
let ures = await daoquery.dao.update(daoquery.query, { $set: { lockStatus: 1 } })
console.log(`更新lockStatus为0`, ures)
})
console.log(`完成调用,解锁操作执行--------------`)
await unLock(daoqueryArr)
success = true
return ret
} else {
......@@ -46,27 +55,41 @@ export const lockUpdate = async (daoqueryArr, fn, times = 5) => {
}
} catch (e) {
console.log(e)
daoqueryArr.forEach(async daoquery => {
let ures = await daoquery.dao.update(daoquery.query, { $set: { lockStatus: 1 } })
console.log(`更新lockStatus为0`, ures)
})
success = false
await unLock(daoqueryArr)
success = true
}
console.log(`daoqueryArr`, daoqueryArr)
// 稍后重试
if (!success) {
daoqueryArr.forEach(async daoquery => {
let ures = await daoquery.dao.update(daoquery.query, { $set: { lockStatus: 1 } })
console.log(`操作${times}次后更新lockStatus为0`, ures)
})
await unLock(daoqueryArr)
return CODE_TYPES.ERROR_UPDATE_RETRY
}
function waitFor(ms) {
function waitFor(ms: number) {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}
}
async function unLock(daoqueryArr) {
return await daoqueryArr.reduce(async (prev, daoquery) => {
let ures = await daoquery.dao.update(daoquery.query, { $set: { lockStatus: 1 } })
console.log(`更新lockStatus为1`, ures)
prev.push(ures)
return prev
}, [])
}
async function lock(daoqueryArr) {
return await daoqueryArr.reduce(async (prev, daoquery) => {
console.log(`daoquery`, daoquery)
let ures = await daoquery.dao.update(daoquery.query, { $set: { lockStatus: 2 } })
console.log(`更新lockStatus为2`, ures)
prev.push(ures)
return prev
}, [])
}
/**
* 调用示例
*/
......
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