@@ -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
0 commit comments