@@ -56,14 +56,15 @@ class Scf {
5656 }
5757
5858 // get function detail
59- async getFunction ( namespace , functionName , showCode ) {
59+ async getFunction ( namespace , functionName , qualifier = '$LATEST' , showCode = false ) {
6060 try {
6161 const funcInfo = await this . scfClient . request ( {
6262 Action : 'GetFunction' ,
6363 Version : '2018-04-16' ,
6464 Region : this . region ,
6565 FunctionName : functionName ,
6666 Namespace : namespace ,
67+ Qualifier : qualifier ,
6768 ShowCode : showCode ? 'TRUE' : 'FALSE' ,
6869 } ) ;
6970 if ( funcInfo . Response && funcInfo . Response . Error ) {
@@ -89,13 +90,13 @@ class Scf {
8990
9091 // check function status
9192 // because craeting function is asynchronous
92- async checkStatus ( namespace , functionName ) {
93+ async checkStatus ( namespace = 'default' , functionName , qualifier = '$LATEST' ) {
9394 console . log ( `Checking function ${ functionName } status ...` ) ;
94- const initialInfo = await this . getFunction ( namespace , functionName ) ;
95+ const initialInfo = await this . getFunction ( namespace , functionName , qualifier ) ;
9596 let status = initialInfo . Status ;
9697 let times = 200 ;
9798 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 ) ;
99100 status = tempFunc . Status ;
100101 await sleep ( 300 ) ;
101102 times = times - 1 ;
@@ -178,17 +179,8 @@ class Scf {
178179 if ( inputs . events ) {
179180 console . log ( `Deploying ${ inputs . name } 's triggers in ${ this . region } .` ) ;
180181
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 ) ;
192184
193185 // remove all old triggers
194186 const oldTriggers = funcInfo . Triggers || [ ] ;
@@ -426,6 +418,23 @@ class Scf {
426418 return res . Response ;
427419 }
428420
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+
429438 // deploy SCF flow
430439 async deploy ( inputs = { } ) {
431440 // whether auto create/bind role
@@ -443,28 +452,18 @@ class Scf {
443452 funcInfo = await this . getFunction ( namespace , inputs . name ) ;
444453 } else {
445454 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+
454459 await this . updatefunctionConfigure ( inputs , funcInfo ) ;
455460
456461 // after updating function, get latest function info
457462 funcInfo = await this . getFunction ( namespace , inputs . name ) ;
458463 }
459464
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 ) ;
468467
469468 const outputs = funcInfo ;
470469 if ( inputs . publish ) {
@@ -480,6 +479,9 @@ class Scf {
480479 inputs . needSetTraffic =
481480 inputs . traffic !== undefined && inputs . lastVersion && inputs . lastVersion !== '$LATEST' ;
482481 if ( inputs . needSetTraffic ) {
482+ // should check function status is active, then continue
483+ await this . isOperationalStatus ( namespace , inputs . name , inputs . lastVersion ) ;
484+
483485 await this . updateAliasTraffic ( {
484486 functionName : funcInfo . FunctionName ,
485487 region : this . region ,
0 commit comments