Commit f7caeb24 authored by 姜登's avatar 姜登

nodelimit

parent ba39b29e
Pipeline #7111 passed with stage
in 4 seconds
......@@ -53,6 +53,7 @@ class OrderController extends Controller {
let result = { code: 2, msg: '任务还在执行或未输入', data: {} };
if (data) {
if (data.status === 'success') {
await service.partner.appKeyLimit(appKey);
const result = await service.storage.read(orderSn, appKey);
ctx.body = {
code: 0,
......
'use strict';
module.exports = app => {
const { DataTypes } = app.Sequelize;
const nodeLimit = app.model.define('nodeLimit', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true,
field: 'id',
},
companyName: {
type: DataTypes.STRING(255),
allowNull: true,
field: 'companyName',
},
appKey: {
type: DataTypes.STRING(255),
allowNull: false,
field: 'appKey',
},
type: {
type: DataTypes.STRING(255),
allowNull: true,
field: 'type',
},
currentCount: {
type: DataTypes.INTEGER(20),
allowNull: true,
defaultValue: 0,
field: 'currentCount',
},
limitCount: {
type: DataTypes.INTEGER(20),
allowNull: true,
defaultValue: 0,
field: 'limitCount',
},
createTime: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
field: 'createTime',
},
updateTime: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
field: 'updateTime',
},
text: {
type: DataTypes.STRING(255),
allowNull: true,
field: 'text1',
},
text2: {
type: DataTypes.STRING(255),
allowNull: true,
field: 'text2',
},
},
{
tableName: 'node_limit',
timestamps: false,
});
return nodeLimit;
};
......@@ -27,6 +27,49 @@ class PartnerService extends Service {
}
return;
}
async appKeyLimit(appKey) {
const { ctx, partnerAPI: { redisappKeyLimitPrefix } } = this;
let data = await this.app.redis.get(redisappKeyLimitPrefix);
if (data) {
data = JSON.parse(data);
if (data.includes(appKey)) {
let limit = await this.app.redis.get(redisappKeyLimitPrefix + appKey);
if (limit) {
limit = JSON.parse(limit);
if (limit.currentCount >= limit.limitCount) {
ctx.throw(422, { message: 'appKey查询数量超过限制,请联系技术人员' });
} else {
// limit.currentCount++;
// await this.app.redis.set(redisappKeyLimitPrefix + appKey, JSON.stringify({ currentCount: limit.currentCount, limitCount: limit.limitCount }), 'EX', 300);
await ctx.model.query('UPDATE node_limit set currentCount = currentCount + 1 where appKey = ?', {
type: 'UPDATE',
replacements: [appKey],
});
}
}
}
} else {
const dbRes = await ctx.model.NodeLimit.findAll({
attributes: ['appKey', 'currentCount', 'limitCount'],
});
if (dbRes) {
const appKeyArray = [];
for (const item of dbRes) {
const { appKey, currentCount, limitCount } = item;
appKeyArray.push(appKey);
await this.app.redis.set(redisappKeyLimitPrefix + appKey, JSON.stringify({ currentCount, limitCount }), 'EX', 300);
}
await this.app.redis.set(redisappKeyLimitPrefix, JSON.stringify(appKeyArray), 'EX', 300);
if (appKeyArray.includes(appKey)) {
await ctx.model.query('UPDATE node_limit set currentCount = currentCount + 1 where appKey = ?', {
type: 'UPDATE',
replacements: [appKey],
});
}
}
}
}
}
module.exports = PartnerService;
......@@ -31,6 +31,7 @@ module.exports = () => {
config.partnerAPI = {
host: 'https://uat-nginx.jianbing.com/zeus-api/v1',
fetchScripts: '/chaos/partners/scripts',
redisappKeyLimitPrefix: 'URANUS.HF.PARNTERS.Limit',
};
config.scriptsAPI = {
......
......@@ -75,6 +75,7 @@ module.exports = () => {
host: process.env.PARTNERAPI_HOST || 'https://uat-nginx.jianbing.com/zeus-api/v1',
fetchTheme: '/chaos/hf/partners',
fetchScripts: '/chaos/partners/scripts',
redisappKeyLimitPrefix: 'URANUS.HF.PARNTERS.Limit',
};
config.lockKeys = {
......
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