@@ -92,7 +92,7 @@ class Scf {
92
92
console . log ( `Checking function ${ functionName } status ...` ) ;
93
93
let status = 'Updating' ;
94
94
let times = 200 ;
95
- while ( ( status == 'Updating' || status == 'Creating' ) && times > 0 ) {
95
+ while ( ( status === 'Updating' || status = == 'Creating' ) && times > 0 ) {
96
96
const tempFunc = await this . getFunction ( namespace , functionName ) ;
97
97
status = tempFunc . Status ;
98
98
await sleep ( 300 ) ;
@@ -317,6 +317,7 @@ class Scf {
317
317
* @param {object } inputs publish version parameter
318
318
*/
319
319
async publishVersion ( inputs ) {
320
+ console . log ( `Publish function ${ inputs . functionName } version...` ) ;
320
321
const publishInputs = {
321
322
Action : 'PublishVersion' ,
322
323
Version : '2018-04-16' ,
@@ -326,6 +327,7 @@ class Scf {
326
327
Namespace : inputs . namespace || 'default' ,
327
328
} ;
328
329
const res = await this . scfClient . request ( publishInputs ) ;
330
+
329
331
if ( res . Response && res . Response . Error ) {
330
332
throw new TypeError (
331
333
'API_SCF_PublishVersion' ,
@@ -334,6 +336,9 @@ class Scf {
334
336
res . Response . RequestId ,
335
337
) ;
336
338
}
339
+ console . log (
340
+ `Published function ${ inputs . functionName } version ${ res . Response . FunctionVersion } ` ,
341
+ ) ;
337
342
return res . Response ;
338
343
}
339
344
@@ -364,6 +369,11 @@ class Scf {
364
369
}
365
370
366
371
async updateAliasTraffic ( inputs ) {
372
+ console . log (
373
+ `Config function ${ inputs . functionName } traffic ${ 1 - inputs . traffic } for version ${
374
+ inputs . lastVersion
375
+ } ...`,
376
+ ) ;
367
377
const publishInputs = {
368
378
Action : 'UpdateAlias' ,
369
379
Version : '2018-04-16' ,
@@ -386,6 +396,11 @@ class Scf {
386
396
res . Response . RequestId ,
387
397
) ;
388
398
}
399
+ console . log (
400
+ `Config function ${ inputs . functionName } traffic ${ 1 - inputs . traffic } for version ${
401
+ inputs . lastVersion
402
+ } success`,
403
+ ) ;
389
404
return res . Response ;
390
405
}
391
406
@@ -406,6 +421,7 @@ class Scf {
406
421
funcInfo = await this . getFunction ( namespace , inputs . name ) ;
407
422
} else {
408
423
await this . updateFunctionCode ( inputs , funcInfo ) ;
424
+ // update function code, need wait for function active status
409
425
const functionStatus = await this . checkStatus ( namespace , inputs . name ) ;
410
426
if ( functionStatus === false ) {
411
427
throw new TypeError (
@@ -416,27 +432,52 @@ class Scf {
416
432
await this . updatefunctionConfigure ( inputs , funcInfo ) ;
417
433
}
418
434
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 ;
420
445
if ( inputs . tags || inputs . events ) {
421
446
if ( ! funcInfo ) {
422
447
funcInfo = await this . getFunction ( namespace , inputs . name ) ;
423
448
}
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
- }
430
449
await Promise . all ( [ this . deployTags ( funcInfo , inputs ) , this . deployTrigger ( funcInfo , inputs ) ] ) ;
431
450
}
432
451
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
+
433
474
console . log ( `Deployed funtion ${ funcInfo . FunctionName } .` ) ;
434
- return output ;
475
+ return outputs ;
435
476
}
436
477
437
478
// 移除函数的主逻辑
438
479
async remove ( inputs = { } ) {
439
- console . log ( `Deleteing function ${ inputs . functionName || inputs . FunctionName } ...` ) ;
480
+ console . log ( `Deleting function ${ inputs . functionName || inputs . FunctionName } ...` ) ;
440
481
const functionName = inputs . functionName || inputs . FunctionName ;
441
482
const namespace = inputs . namespace || inputs . Namespace || CONFIGS . defaultNamespace ;
442
483
0 commit comments