Skip to content

Commit c440912

Browse files
committed
fix: support publish traffic for deploy method
1 parent 9e73466 commit c440912

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

src/modules/scf/index.js

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class Scf {
9292
console.log(`Checking function ${functionName} status ...`);
9393
let status = 'Updating';
9494
let times = 200;
95-
while ((status == 'Updating' || status == 'Creating') && times > 0) {
95+
while ((status === 'Updating' || status === 'Creating') && times > 0) {
9696
const tempFunc = await this.getFunction(namespace, functionName);
9797
status = tempFunc.Status;
9898
await sleep(300);
@@ -317,6 +317,7 @@ class Scf {
317317
* @param {object} inputs publish version parameter
318318
*/
319319
async publishVersion(inputs) {
320+
console.log(`Publish function ${inputs.functionName} version...`);
320321
const publishInputs = {
321322
Action: 'PublishVersion',
322323
Version: '2018-04-16',
@@ -326,6 +327,7 @@ class Scf {
326327
Namespace: inputs.namespace || 'default',
327328
};
328329
const res = await this.scfClient.request(publishInputs);
330+
329331
if (res.Response && res.Response.Error) {
330332
throw new TypeError(
331333
'API_SCF_PublishVersion',
@@ -334,6 +336,9 @@ class Scf {
334336
res.Response.RequestId,
335337
);
336338
}
339+
console.log(
340+
`Published function ${inputs.functionName} version ${res.Response.FunctionVersion}`,
341+
);
337342
return res.Response;
338343
}
339344

@@ -364,6 +369,11 @@ class Scf {
364369
}
365370

366371
async updateAliasTraffic(inputs) {
372+
console.log(
373+
`Config function ${inputs.functionName} traffic ${1 - inputs.traffic} for version ${
374+
inputs.lastVersion
375+
}...`,
376+
);
367377
const publishInputs = {
368378
Action: 'UpdateAlias',
369379
Version: '2018-04-16',
@@ -386,6 +396,11 @@ class Scf {
386396
res.Response.RequestId,
387397
);
388398
}
399+
console.log(
400+
`Config function ${inputs.functionName} traffic ${1 - inputs.traffic} for version ${
401+
inputs.lastVersion
402+
} success`,
403+
);
389404
return res.Response;
390405
}
391406

@@ -406,6 +421,7 @@ class Scf {
406421
funcInfo = await this.getFunction(namespace, inputs.name);
407422
} else {
408423
await this.updateFunctionCode(inputs, funcInfo);
424+
// update function code, need wait for function active status
409425
const functionStatus = await this.checkStatus(namespace, inputs.name);
410426
if (functionStatus === false) {
411427
throw new TypeError(
@@ -416,27 +432,52 @@ class Scf {
416432
await this.updatefunctionConfigure(inputs, funcInfo);
417433
}
418434

419-
const output = funcInfo;
435+
// after create/update function, should check function status is active, then continue
436+
const functionStatus = await this.checkStatus(namespace, inputs.name);
437+
if (functionStatus === false) {
438+
throw new TypeError(
439+
'API_SCF_GetFunction_STATUS',
440+
`Function ${inputs.name} upgrade failed. Please check function status.`,
441+
);
442+
}
443+
444+
const outputs = funcInfo;
420445
if (inputs.tags || inputs.events) {
421446
if (!funcInfo) {
422447
funcInfo = await this.getFunction(namespace, inputs.name);
423448
}
424-
if ((await this.checkStatus(namespace, inputs.name)) === false) {
425-
throw new TypeError(
426-
'API_SCF_GetFunction_STATUS',
427-
`Function ${inputs.name} upgrade failed. Please check function status.`,
428-
);
429-
}
430449
await Promise.all([this.deployTags(funcInfo, inputs), this.deployTrigger(funcInfo, inputs)]);
431450
}
432451

452+
if (inputs.publish) {
453+
const { FunctionVersion } = await this.publishVersion({
454+
functionName: funcInfo.FunctionName,
455+
region: this.region,
456+
namespace,
457+
description: inputs.publishDescription,
458+
});
459+
inputs.lastVersion = FunctionVersion;
460+
outputs.LastVersion = FunctionVersion;
461+
}
462+
if (inputs.traffic !== undefined && inputs.lastVersion) {
463+
await this.updateAliasTraffic({
464+
functionName: funcInfo.FunctionName,
465+
region: this.region,
466+
traffic: inputs.traffic,
467+
lastVersion: inputs.lastVersion,
468+
aliasName: inputs.aliasName,
469+
description: inputs.aliasDescription,
470+
});
471+
outputs.Traffic = inputs.traffic;
472+
}
473+
433474
console.log(`Deployed funtion ${funcInfo.FunctionName}.`);
434-
return output;
475+
return outputs;
435476
}
436477

437478
// 移除函数的主逻辑
438479
async remove(inputs = {}) {
439-
console.log(`Deleteing function ${inputs.functionName || inputs.FunctionName} ...`);
480+
console.log(`Deleting function ${inputs.functionName || inputs.FunctionName} ...`);
440481
const functionName = inputs.functionName || inputs.FunctionName;
441482
const namespace = inputs.namespace || inputs.Namespace || CONFIGS.defaultNamespace;
442483

0 commit comments

Comments
 (0)