@@ -89,24 +89,25 @@ class Scf {
89
89
}
90
90
91
91
// 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
93
94
async checkStatus ( namespace = 'default' , functionName , qualifier = '$LATEST' ) {
94
95
console . log ( `Checking function ${ functionName } status ...` ) ;
95
- const initialInfo = await this . getFunction ( namespace , functionName , qualifier ) ;
96
+ let initialInfo = await this . getFunction ( namespace , functionName , qualifier ) ;
96
97
let status = initialInfo . Status ;
97
- let times = 200 ;
98
+ let times = 120 ;
98
99
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 ) ;
102
103
times = times - 1 ;
103
104
}
104
- return status !== 'Active' ? false : true ;
105
+ return status !== 'Active' ? initialInfo : true ;
105
106
}
106
107
107
108
// create function
108
109
async createFunction ( inputs ) {
109
- console . log ( `Creating funtion ${ inputs . name } in ${ this . region } ... ` ) ;
110
+ console . log ( `Creating function ${ inputs . name } in ${ this . region } ... ` ) ;
110
111
const functionInputs = await formatFunctionInputs ( this . region , inputs ) ;
111
112
functionInputs . Action = 'CreateFunction' ;
112
113
const funcInfo = await this . scfClient . request ( functionInputs ) ;
@@ -124,7 +125,7 @@ class Scf {
124
125
125
126
// update function code
126
127
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 } ...` ) ;
128
129
const functionInputs = await formatFunctionInputs ( this . region , inputs ) ;
129
130
const updateFunctionConnfigure = {
130
131
Action : 'UpdateFunctionCode' ,
@@ -151,7 +152,7 @@ class Scf {
151
152
152
153
// update function configure
153
154
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 } ...` ) ;
155
156
const functionInputs = await formatFunctionInputs ( this . region , inputs ) ;
156
157
functionInputs . Action = 'UpdateFunctionConfiguration' ;
157
158
functionInputs . Timeout = inputs . timeout || funcInfo . Timeout ;
@@ -176,115 +177,106 @@ class Scf {
176
177
177
178
// deploy SCF triggers
178
179
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 } .` ) ;
181
181
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
+ ) ;
213
212
}
214
213
}
214
+ }
215
215
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 ) ;
232
232
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 ] ) ;
244
239
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 ) ;
247
242
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
+ ) ;
257
250
}
251
+ deployTriggerResult . push ( Response . TriggerInfo ) ;
258
252
}
259
- funcInfo . Triggers = deployTriggerResult ;
260
- return deployTriggerResult ;
261
253
}
254
+ funcInfo . Triggers = deployTriggerResult ;
255
+ return deployTriggerResult ;
262
256
}
263
257
264
258
// deploy tags
265
259
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 ;
273
265
}
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
+ } ) ;
279
272
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
+ ) ;
288
280
}
289
281
}
290
282
@@ -425,14 +417,11 @@ class Scf {
425
417
*/
426
418
async isOperationalStatus ( namespace , functionName , qualifier = '$LATEST' ) {
427
419
// 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 ;
434
423
}
435
- return true ;
424
+ throw new TypeError ( 'API_SCF_isOperationalStatus' , JSON . stringify ( res ) , null , res . RequestId ) ;
436
425
}
437
426
438
427
// deploy SCF flow
@@ -475,13 +464,13 @@ class Scf {
475
464
} ) ;
476
465
inputs . lastVersion = FunctionVersion ;
477
466
outputs . LastVersion = FunctionVersion ;
467
+
468
+ // should check function status is active, then continue
469
+ await this . isOperationalStatus ( namespace , inputs . name , inputs . lastVersion ) ;
478
470
}
479
471
inputs . needSetTraffic =
480
472
inputs . traffic !== undefined && inputs . lastVersion && inputs . lastVersion !== '$LATEST' ;
481
473
if ( inputs . needSetTraffic ) {
482
- // should check function status is active, then continue
483
- await this . isOperationalStatus ( namespace , inputs . name , inputs . lastVersion ) ;
484
-
485
474
await this . updateAliasTraffic ( {
486
475
functionName : funcInfo . FunctionName ,
487
476
region : this . region ,
@@ -523,16 +512,20 @@ class Scf {
523
512
}
524
513
} catch ( e ) {
525
514
// no op
515
+ console . log ( 'API_SCF_getAlias' , e . message ) ;
526
516
}
527
517
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 ) ;
533
526
}
534
527
535
- console . log ( `Deployed funtion ${ funcInfo . FunctionName } .` ) ;
528
+ console . log ( `Deploy function ${ funcInfo . FunctionName } success .` ) ;
536
529
return outputs ;
537
530
}
538
531
0 commit comments