Skip to content

Commit d4ff421

Browse files
feat: 提交一段有问题的代码
1 parent b23d364 commit d4ff421

File tree

3 files changed

+51
-36
lines changed

3 files changed

+51
-36
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"homepage": "https://github.com/shouldnotappearcalm/yapi-plugin-interface-oauth2#readme",
2323
"dependencies": {
2424
"add": "^2.0.6",
25+
"async-lock": "^1.2.4",
2526
"axios": "^0.19.0",
2627
"global": "^4.4.0",
2728
"yarn": "^1.22.0",

utils/syncTokenUtil.js

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
const schedule = require('node-schedule');
2+
var AsyncLock = require('async-lock');
23
const projectModel = require('models/project.js');
34
const oauthModel = require('../model/oauthModel.js');
45
const yapi = require('yapi.js');
56
const jobMap = new Map();
67

8+
var lock = new AsyncLock({timeout: 5000});
9+
710
class syncTokenUtils {
811
constructor(ctx) {
912
yapi.commons.log(
@@ -117,44 +120,50 @@ class syncTokenUtils {
117120
'token更新定时器触发, getTokenUrl:' + oauthData.get_token_url
118121
);
119122
//对定时任务存在的必要性做判断
120-
let projectData;
121-
try {
122-
projectData = await this.preRefresh(oauthData);
123-
} catch (e) {
124-
return;
125-
}
123+
lock.acquire(oauthData.project_id, async function(done) {
124+
let projectData;
125+
try {
126+
projectData = await this.preRefresh(oauthData);
127+
} catch (e) {
128+
return;
129+
}
126130

127-
let projectId = projectData._id;
128-
let getTokenUrl = oauthData.get_token_url;
129-
let method = oauthData.request_type;
130-
let headers_data = oauthData.headers_data;
131-
let result;
132-
try {
133-
if (method === 'GET') {
134-
let params = oauthData.params;
135-
result = await this.execGetToken(getTokenUrl, method, headers_data, params);
136-
} else {
137-
let dataType = oauthData.dataType;
138-
let formData = oauthData.form_data;
139-
let dataJson = oauthData.data_json;
140-
result = await this.execGetToken(
141-
getTokenUrl,
142-
method,
143-
headers_data,
144-
formData,
145-
dataJson,
146-
dataType
147-
);
131+
let projectId = projectData._id;
132+
let getTokenUrl = oauthData.get_token_url;
133+
let method = oauthData.request_type;
134+
let headers_data = oauthData.headers_data;
135+
let result;
136+
try {
137+
if (method === 'GET') {
138+
let params = oauthData.params;
139+
result = await this.execGetToken(getTokenUrl, method, headers_data, params);
140+
} else {
141+
let dataType = oauthData.dataType;
142+
let formData = oauthData.form_data;
143+
let dataJson = oauthData.data_json;
144+
result = await this.execGetToken(
145+
getTokenUrl,
146+
method,
147+
headers_data,
148+
formData,
149+
dataJson,
150+
dataType
151+
);
152+
}
153+
let accessToken = this.getTokenByPath(result, oauthData.token_path);
154+
//更新到对应的env上;
155+
await this.updateProjectToken(accessToken, oauthData, projectData);
156+
this.saveSyncLog(0, '环境:【' + oauthData.env_name + '】更新新的token【' + accessToken + '】成功', '1', projectId);
157+
yapi.commons.log('环境:【' + oauthData.env_name + '】更新token成功');
158+
done();
159+
} catch (e) {
160+
this.saveSyncLog(-1, '环境:【' + oauthData.env_name + '】数据格式出错,请检查', '1', projectId);
161+
yapi.commons.log('环境:【' + oauthData.env_name + '】获取数据失败' + e.message);
162+
done();
148163
}
149-
let accessToken = this.getTokenByPath(result, oauthData.token_path);
150-
//更新到对应的env上;
151-
await this.updateProjectToken(accessToken, oauthData, projectData);
152-
this.saveSyncLog(0, '环境:【' + oauthData.env_name + '】更新新的token【' + accessToken + '】成功', '1', projectId);
153-
yapi.commons.log('环境:【' + oauthData.env_name + '】更新token成功');
154-
} catch (e) {
155-
this.saveSyncLog(-1, '环境:【' + oauthData.env_name + '】数据格式出错,请检查', '1', projectId);
156-
yapi.commons.log('环境:【' + oauthData.env_name + '】获取数据失败' + e.message);
157-
}
164+
}, function(err, ret) {
165+
yapi.commons.log('更新 token 失败,' + err.message);
166+
}, {});
158167
}
159168

160169
/**

0 commit comments

Comments
 (0)