Skip to content

Commit 955dcdc

Browse files
committed
feat: optimize scf module
1 parent bc541d6 commit 955dcdc

File tree

9 files changed

+142
-133
lines changed

9 files changed

+142
-133
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# [1.13.0](https://github.com/serverless-tencent/tencent-component-toolkit/compare/v1.12.15...v1.13.0) (2020-07-27)
2+
3+
4+
### Bug Fixes
5+
6+
* **cdn:** make switch for https and forceRedirect to on ([bc541d6](https://github.com/serverless-tencent/tencent-component-toolkit/commit/bc541d62d2b97a9fecd95d7a68873052022d03d3))
7+
* **cdn:** PurgePathCache param Paths ([da79e1d](https://github.com/serverless-tencent/tencent-component-toolkit/commit/da79e1d127b05a4ea0e68a3064573c9ab8d4bc8b))
8+
9+
10+
### Features
11+
12+
* optimize scf module ([d6d884c](https://github.com/serverless-tencent/tencent-component-toolkit/commit/d6d884ca52c5ed6b31125d8b5b21a13526395826))
13+
114
## [1.12.15](https://github.com/serverless-tencent/tencent-component-toolkit/compare/v1.12.14...v1.12.15) (2020-07-21)
215

316

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tencent-component-toolkit",
3-
"version": "1.12.15",
3+
"version": "1.13.0",
44
"description": "Tencent component toolkit",
55
"main": "src/index.js",
66
"scripts": {

src/modules/apigw/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ class Apigw {
198198
const output = {
199199
path: endpoint.path,
200200
method: endpoint.method,
201+
apiName: endpoint.apiName || 'index',
201202
apiId: undefined,
202203
created: false,
203204
};
@@ -640,7 +641,7 @@ class Apigw {
640641

641642
apiList.push(curApi);
642643
console.log(
643-
`Deployment successful for the api named ${endpoint.apiName} in the ${this.region} region.`,
644+
`Deployment successful for the api named ${curApi.apiName} in the ${this.region} region.`,
644645
);
645646
}
646647

src/modules/cdn/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Cdn {
3434
console.log(`Purging CDN caches, it will work in 5 minutes...`);
3535
try {
3636
await PurgePathCache(this.capi, {
37-
Urls: urls,
37+
Paths: urls,
3838
FlushType: flushType,
3939
});
4040
} catch (e) {

src/modules/scf/index.js

Lines changed: 112 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,25 @@ class Scf {
8989
}
9090

9191
// check function status
92-
// because craeting function is asynchronous
92+
// because creating/upadting function is asynchronous
93+
// if not become Active in 120 * 1000 miniseconds, return request result, and throw error
9394
async checkStatus(namespace = 'default', functionName, qualifier = '$LATEST') {
9495
console.log(`Checking function ${functionName} status ...`);
95-
const initialInfo = await this.getFunction(namespace, functionName, qualifier);
96+
let initialInfo = await this.getFunction(namespace, functionName, qualifier);
9697
let status = initialInfo.Status;
97-
let times = 200;
98+
let times = 120;
9899
while (CONFIGS.waitStatus.indexOf(status) !== -1 && times > 0) {
99-
const tempFunc = await this.getFunction(namespace, functionName, qualifier);
100-
status = tempFunc.Status;
101-
await sleep(300);
100+
initialInfo = await this.getFunction(namespace, functionName, qualifier);
101+
status = initialInfo.Status;
102+
await sleep(1000);
102103
times = times - 1;
103104
}
104-
return status !== 'Active' ? false : true;
105+
return status !== 'Active' ? initialInfo : true;
105106
}
106107

107108
// create function
108109
async createFunction(inputs) {
109-
console.log(`Creating funtion ${inputs.name} in ${this.region} ... `);
110+
console.log(`Creating function ${inputs.name} in ${this.region} ... `);
110111
const functionInputs = await formatFunctionInputs(this.region, inputs);
111112
functionInputs.Action = 'CreateFunction';
112113
const funcInfo = await this.scfClient.request(functionInputs);
@@ -124,7 +125,7 @@ class Scf {
124125

125126
// update function code
126127
async updateFunctionCode(inputs, funcInfo) {
127-
console.log(`Updating funtion ${inputs.name}'s code in ${this.region} ...`);
128+
console.log(`Updating function ${inputs.name}'s code in ${this.region} ...`);
128129
const functionInputs = await formatFunctionInputs(this.region, inputs);
129130
const updateFunctionConnfigure = {
130131
Action: 'UpdateFunctionCode',
@@ -151,7 +152,7 @@ class Scf {
151152

152153
// update function configure
153154
async updatefunctionConfigure(inputs, funcInfo) {
154-
console.log(`Updating funtion ${inputs.name}'s configure in ${this.region} ...`);
155+
console.log(`Updating function ${inputs.name}'s configure in ${this.region} ...`);
155156
const functionInputs = await formatFunctionInputs(this.region, inputs);
156157
functionInputs.Action = 'UpdateFunctionConfiguration';
157158
functionInputs.Timeout = inputs.timeout || funcInfo.Timeout;
@@ -176,115 +177,106 @@ class Scf {
176177

177178
// deploy SCF triggers
178179
async deployTrigger(funcInfo, inputs) {
179-
if (inputs.events) {
180-
console.log(`Deploying ${inputs.name}'s triggers in ${this.region}.`);
180+
console.log(`Deploying ${inputs.name}'s triggers in ${this.region}.`);
181181

182-
// should check function status is active, then continue
183-
await this.isOperationalStatus(inputs.namespace, inputs.name);
184-
185-
// remove all old triggers
186-
const oldTriggers = funcInfo.Triggers || [];
187-
for (let tIdx = 0, len = oldTriggers.length; tIdx < len; tIdx++) {
188-
const curTrigger = oldTriggers[tIdx];
189-
190-
if (curTrigger.Type === 'apigw') {
191-
// TODO: now apigw can not sync in SCF trigger list
192-
// await this.apigwClient.remove(curTrigger);
193-
} else {
194-
console.log(`Deleting ${curTrigger.Type} triggers: ${curTrigger.TriggerName}.`);
195-
const delRes = await this.scfClient.request({
196-
Action: 'DeleteTrigger',
197-
Version: '2018-04-16',
198-
Region: this.region,
199-
FunctionName: funcInfo.FunctionName,
200-
Namespace: funcInfo.Namespace,
201-
Type: curTrigger.Type,
202-
TriggerDesc: curTrigger.TriggerDesc,
203-
TriggerName: curTrigger.TriggerName,
204-
});
205-
if (delRes.Response && delRes.Response.Error) {
206-
throw new TypeError(
207-
'API_SCF_DeleteTrigger',
208-
JSON.stringify(delRes.Response),
209-
null,
210-
delRes.Response.RequestId,
211-
);
212-
}
182+
// should check function status is active, then continue
183+
await this.isOperationalStatus(inputs.namespace, inputs.name);
184+
185+
// remove all old triggers
186+
const oldTriggers = funcInfo.Triggers || [];
187+
for (let tIdx = 0, len = oldTriggers.length; tIdx < len; tIdx++) {
188+
const curTrigger = oldTriggers[tIdx];
189+
190+
if (curTrigger.Type === 'apigw') {
191+
// TODO: now apigw can not sync in SCF trigger list
192+
// await this.apigwClient.remove(curTrigger);
193+
} else {
194+
console.log(`Deleting ${curTrigger.Type} triggers: ${curTrigger.TriggerName}.`);
195+
const delRes = await this.scfClient.request({
196+
Action: 'DeleteTrigger',
197+
Version: '2018-04-16',
198+
Region: this.region,
199+
FunctionName: funcInfo.FunctionName,
200+
Namespace: funcInfo.Namespace,
201+
Type: curTrigger.Type,
202+
TriggerDesc: curTrigger.TriggerDesc,
203+
TriggerName: curTrigger.TriggerName,
204+
});
205+
if (delRes.Response && delRes.Response.Error) {
206+
throw new TypeError(
207+
'API_SCF_DeleteTrigger',
208+
JSON.stringify(delRes.Response),
209+
null,
210+
delRes.Response.RequestId,
211+
);
213212
}
214213
}
214+
}
215215

216-
// create all new triggers
217-
const deployTriggerResult = [];
218-
for (let i = 0; i < inputs.events.length; i++) {
219-
const event = inputs.events[i];
220-
const eventType = Object.keys(event)[0];
221-
222-
if (eventType === 'apigw') {
223-
const { triggerInputs } = formatTrigger(
224-
eventType,
225-
this.region,
226-
funcInfo,
227-
event[eventType],
228-
inputs.needSetTraffic,
229-
);
230-
try {
231-
const apigwOutput = await this.apigwClient.deploy(triggerInputs);
216+
// create all new triggers
217+
const deployTriggerResult = [];
218+
for (let i = 0; i < inputs.events.length; i++) {
219+
const event = inputs.events[i];
220+
const eventType = Object.keys(event)[0];
221+
222+
if (eventType === 'apigw') {
223+
const { triggerInputs } = formatTrigger(
224+
eventType,
225+
this.region,
226+
funcInfo,
227+
event[eventType],
228+
inputs.needSetTraffic,
229+
);
230+
try {
231+
const apigwOutput = await this.apigwClient.deploy(triggerInputs);
232232

233-
deployTriggerResult.push(apigwOutput);
234-
} catch (e) {
235-
throw e;
236-
}
237-
} else {
238-
const { triggerInputs } = formatTrigger(
239-
eventType,
240-
this.region,
241-
funcInfo,
242-
event[eventType],
243-
);
233+
deployTriggerResult.push(apigwOutput);
234+
} catch (e) {
235+
throw e;
236+
}
237+
} else {
238+
const { triggerInputs } = formatTrigger(eventType, this.region, funcInfo, event[eventType]);
244239

245-
console.log(`Creating ${eventType} triggers: ${event[eventType].name}.`);
246-
const { Response } = await this.scfClient.request(triggerInputs);
240+
console.log(`Creating ${eventType} triggers: ${event[eventType].name}.`);
241+
const { Response } = await this.scfClient.request(triggerInputs);
247242

248-
if (Response && Response.Error) {
249-
throw new TypeError(
250-
'API_SCF_CreateTrigger',
251-
JSON.stringify(Response),
252-
null,
253-
Response.RequestId,
254-
);
255-
}
256-
deployTriggerResult.push(Response.TriggerInfo);
243+
if (Response && Response.Error) {
244+
throw new TypeError(
245+
'API_SCF_CreateTrigger',
246+
JSON.stringify(Response),
247+
null,
248+
Response.RequestId,
249+
);
257250
}
251+
deployTriggerResult.push(Response.TriggerInfo);
258252
}
259-
funcInfo.Triggers = deployTriggerResult;
260-
return deployTriggerResult;
261253
}
254+
funcInfo.Triggers = deployTriggerResult;
255+
return deployTriggerResult;
262256
}
263257

264258
// deploy tags
265259
async deployTags(funcInfo, inputs) {
266-
if (inputs.tags) {
267-
console.log(`Adding tags for funtion ${inputs.name} in ${this.region} ... `);
268-
const deleteTags = {};
269-
for (let i = 0; i < funcInfo.Tags.length; i++) {
270-
if (!inputs.tags.hasOwnProperty(funcInfo.Tags[i].Key)) {
271-
deleteTags[funcInfo.Tags[i].Key] = funcInfo.Tags[i].Value;
272-
}
260+
console.log(`Adding tags for function ${inputs.name} in ${this.region} ... `);
261+
const deleteTags = {};
262+
for (let i = 0; i < funcInfo.Tags.length; i++) {
263+
if (!inputs.tags.hasOwnProperty(funcInfo.Tags[i].Key)) {
264+
deleteTags[funcInfo.Tags[i].Key] = funcInfo.Tags[i].Value;
273265
}
274-
const res = await this.tagClient.deploy({
275-
resource: `qcs::scf:${this.region}::lam/${funcInfo.FunctionId}`,
276-
replaceTags: inputs.tags,
277-
deleteTags: deleteTags,
278-
});
266+
}
267+
const res = await this.tagClient.deploy({
268+
resource: `qcs::scf:${this.region}::lam/${funcInfo.FunctionId}`,
269+
replaceTags: inputs.tags,
270+
deleteTags: deleteTags,
271+
});
279272

280-
if (res.Response && res.Response.Error) {
281-
throw new TypeError(
282-
'API_TAG_ModifyResourceTags',
283-
JSON.stringify(res.Response),
284-
null,
285-
res.Response.RequestId,
286-
);
287-
}
273+
if (res.Response && res.Response.Error) {
274+
throw new TypeError(
275+
'API_TAG_ModifyResourceTags',
276+
JSON.stringify(res.Response),
277+
null,
278+
res.Response.RequestId,
279+
);
288280
}
289281
}
290282

@@ -425,14 +417,11 @@ class Scf {
425417
*/
426418
async isOperationalStatus(namespace, functionName, qualifier = '$LATEST') {
427419
// after create/update function, should check function status is active, then continue
428-
const functionStatus = await this.checkStatus(namespace, functionName, qualifier);
429-
if (functionStatus === false) {
430-
throw new TypeError(
431-
'API_SCF_isOperationalStatus',
432-
`Function ${functionName} upgrade failed. Please check function status.`,
433-
);
420+
const res = await this.checkStatus(namespace, functionName, qualifier);
421+
if (res === true) {
422+
return true;
434423
}
435-
return true;
424+
throw new TypeError('API_SCF_isOperationalStatus', JSON.stringify(res), null, res.RequestId);
436425
}
437426

438427
// deploy SCF flow
@@ -475,13 +464,13 @@ class Scf {
475464
});
476465
inputs.lastVersion = FunctionVersion;
477466
outputs.LastVersion = FunctionVersion;
467+
468+
// should check function status is active, then continue
469+
await this.isOperationalStatus(namespace, inputs.name, inputs.lastVersion);
478470
}
479471
inputs.needSetTraffic =
480472
inputs.traffic !== undefined && inputs.lastVersion && inputs.lastVersion !== '$LATEST';
481473
if (inputs.needSetTraffic) {
482-
// should check function status is active, then continue
483-
await this.isOperationalStatus(namespace, inputs.name, inputs.lastVersion);
484-
485474
await this.updateAliasTraffic({
486475
functionName: funcInfo.FunctionName,
487476
region: this.region,
@@ -523,16 +512,20 @@ class Scf {
523512
}
524513
} catch (e) {
525514
// no op
515+
console.log('API_SCF_getAlias', e.message);
526516
}
527517

528-
if (inputs.tags || inputs.events) {
529-
if (!funcInfo) {
530-
funcInfo = await this.getFunction(namespace, inputs.name);
531-
}
532-
await Promise.all([this.deployTags(funcInfo, inputs), this.deployTrigger(funcInfo, inputs)]);
518+
// create/update tags
519+
if (inputs.tags) {
520+
await this.deployTags(funcInfo, inputs);
521+
}
522+
523+
// create/update/delete triggers
524+
if (inputs.events) {
525+
await this.deployTrigger(funcInfo, inputs);
533526
}
534527

535-
console.log(`Deployed funtion ${funcInfo.FunctionName}.`);
528+
console.log(`Deploy function ${funcInfo.FunctionName} success.`);
536529
return outputs;
537530
}
538531

src/modules/scf/index.test.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ClientTest {
1212
name: 'express-test',
1313
code: {
1414
bucket: 'sls-cloudfunction-ap-guangzhou-code',
15-
object: 'express_component_3gainhv-1591085216.zip',
15+
object: 'express_component_27ararf-1594798167.zip',
1616
},
1717
handler: 'sl_handler.handler',
1818
runtime: 'Nodejs12.16',
@@ -72,11 +72,7 @@ class ClientTest {
7272

7373
// 1. deploy test
7474
const result = await scf.deploy(scfDemo);
75-
try{
76-
console.log(JSON.stringify(result));
77-
} catch (e) {
78-
console.log(e);
79-
}
75+
console.log(JSON.stringify(result));
8076

8177
// 2. publish version test
8278
// const res = await scf.publishVersion({

0 commit comments

Comments
 (0)