@@ -56,14 +56,15 @@ class Scf {
56
56
}
57
57
58
58
// get function detail
59
- async getFunction ( namespace , functionName , showCode ) {
59
+ async getFunction ( namespace , functionName , qualifier = '$LATEST' , showCode = false ) {
60
60
try {
61
61
const funcInfo = await this . scfClient . request ( {
62
62
Action : 'GetFunction' ,
63
63
Version : '2018-04-16' ,
64
64
Region : this . region ,
65
65
FunctionName : functionName ,
66
66
Namespace : namespace ,
67
+ Qualifier : qualifier ,
67
68
ShowCode : showCode ? 'TRUE' : 'FALSE' ,
68
69
} ) ;
69
70
if ( funcInfo . Response && funcInfo . Response . Error ) {
@@ -89,13 +90,13 @@ class Scf {
89
90
90
91
// check function status
91
92
// because craeting function is asynchronous
92
- async checkStatus ( namespace , functionName ) {
93
+ async checkStatus ( namespace = 'default' , functionName , qualifier = '$LATEST' ) {
93
94
console . log ( `Checking function ${ functionName } status ...` ) ;
94
- const initialInfo = await this . getFunction ( namespace , functionName ) ;
95
+ const initialInfo = await this . getFunction ( namespace , functionName , qualifier ) ;
95
96
let status = initialInfo . Status ;
96
97
let times = 200 ;
97
98
while ( CONFIGS . waitStatus . indexOf ( status ) !== - 1 && times > 0 ) {
98
- const tempFunc = await this . getFunction ( namespace , functionName ) ;
99
+ const tempFunc = await this . getFunction ( namespace , functionName , qualifier ) ;
99
100
status = tempFunc . Status ;
100
101
await sleep ( 300 ) ;
101
102
times = times - 1 ;
@@ -178,17 +179,8 @@ class Scf {
178
179
if ( inputs . events ) {
179
180
console . log ( `Deploying ${ inputs . name } 's triggers in ${ this . region } .` ) ;
180
181
181
- // check function status, if is Active, so we can continue to create trigger for it
182
- const functionStatus = await this . checkStatus (
183
- inputs . namespace || CONFIGS . defaultNamespace ,
184
- inputs . name ,
185
- ) ;
186
- if ( functionStatus === false ) {
187
- throw new TypeError (
188
- 'API_SCF_GetFunction_STATUS' ,
189
- `Function ${ inputs . name } deploy trigger failed. Please check function status.` ,
190
- ) ;
191
- }
182
+ // should check function status is active, then continue
183
+ await this . isOperationalStatus ( inputs . namespace , inputs . name ) ;
192
184
193
185
// remove all old triggers
194
186
const oldTriggers = funcInfo . Triggers || [ ] ;
@@ -426,6 +418,23 @@ class Scf {
426
418
return res . Response ;
427
419
}
428
420
421
+ /**
422
+ * check whether function status is operational
423
+ * @param {string } namespace
424
+ * @param {string } functionName funcitn name
425
+ */
426
+ async isOperationalStatus ( namespace , functionName , qualifier = '$LATEST' ) {
427
+ // 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
+ ) ;
434
+ }
435
+ return true ;
436
+ }
437
+
429
438
// deploy SCF flow
430
439
async deploy ( inputs = { } ) {
431
440
// whether auto create/bind role
@@ -443,28 +452,18 @@ class Scf {
443
452
funcInfo = await this . getFunction ( namespace , inputs . name ) ;
444
453
} else {
445
454
await this . updateFunctionCode ( inputs , funcInfo ) ;
446
- // update function code, need wait for function active status
447
- const functionStatus = await this . checkStatus ( namespace , inputs . name ) ;
448
- if ( functionStatus === false ) {
449
- throw new TypeError (
450
- 'API_SCF_GetFunction_STATUS' ,
451
- `Function ${ inputs . name } upgrade failed. Please check function status.` ,
452
- ) ;
453
- }
455
+
456
+ // should check function status is active, then continue
457
+ await this . isOperationalStatus ( namespace , inputs . name ) ;
458
+
454
459
await this . updatefunctionConfigure ( inputs , funcInfo ) ;
455
460
456
461
// after updating function, get latest function info
457
462
funcInfo = await this . getFunction ( namespace , inputs . name ) ;
458
463
}
459
464
460
- // after create/update function, should check function status is active, then continue
461
- const functionStatus = await this . checkStatus ( namespace , inputs . name ) ;
462
- if ( functionStatus === false ) {
463
- throw new TypeError (
464
- 'API_SCF_GetFunction_STATUS' ,
465
- `Function ${ inputs . name } upgrade failed. Please check function status.` ,
466
- ) ;
467
- }
465
+ // should check function status is active, then continue
466
+ await this . isOperationalStatus ( namespace , inputs . name ) ;
468
467
469
468
const outputs = funcInfo ;
470
469
if ( inputs . publish ) {
@@ -480,6 +479,9 @@ class Scf {
480
479
inputs . needSetTraffic =
481
480
inputs . traffic !== undefined && inputs . lastVersion && inputs . lastVersion !== '$LATEST' ;
482
481
if ( inputs . needSetTraffic ) {
482
+ // should check function status is active, then continue
483
+ await this . isOperationalStatus ( namespace , inputs . name , inputs . lastVersion ) ;
484
+
483
485
await this . updateAliasTraffic ( {
484
486
functionName : funcInfo . FunctionName ,
485
487
region : this . region ,
0 commit comments