Skip to content

Commit 0a6a442

Browse files
committed
fix(scf): support async retry config
1 parent 8ced3a0 commit 0a6a442

File tree

6 files changed

+123
-26
lines changed

6 files changed

+123
-26
lines changed

__tests__/scf/async.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { sleep } from '@ygkit/request';
2+
import { Scf } from '../../src';
3+
import { ScfDeployInputs } from '../../src/modules/scf/interface';
4+
5+
describe('Scf', () => {
6+
const credentials = {
7+
SecretId: process.env.TENCENT_SECRET_ID,
8+
SecretKey: process.env.TENCENT_SECRET_KEY,
9+
};
10+
const scf = new Scf(credentials);
11+
12+
const inputs: ScfDeployInputs = {
13+
name: `serverless-test-${Date.now()}`,
14+
code: {
15+
bucket: process.env.BUCKET,
16+
object: 'express_code.zip',
17+
},
18+
namespace: 'test',
19+
role: 'SCF_QcsRole',
20+
handler: 'sl_handler.handler',
21+
runtime: 'Nodejs12.16',
22+
region: 'ap-guangzhou',
23+
description: 'Created by Serverless',
24+
memorySize: 256,
25+
timeout: 20,
26+
tags: {
27+
test: 'test',
28+
},
29+
environment: {
30+
variables: {
31+
TEST: 'value',
32+
},
33+
},
34+
};
35+
let outputs;
36+
37+
test('[asyncRunEnable and traceEnable] create', async () => {
38+
await sleep(3000);
39+
delete inputs.cls;
40+
inputs.asyncRunEnable = true;
41+
inputs.traceEnable = true;
42+
inputs.msgTTL = 3600;
43+
inputs.retryNum = 0;
44+
outputs = await scf.deploy(inputs);
45+
46+
const asyncConfig = await scf.scf.getAsyncRetryConfig(inputs, {} as any);
47+
48+
expect(outputs.AsyncRunEnable).toBe('TRUE');
49+
expect(outputs.TraceEnable).toBe('TRUE');
50+
expect(asyncConfig).toEqual({
51+
AsyncTriggerConfig: {
52+
MsgTTL: 3600,
53+
RetryConfig: [
54+
{ ErrorCode: ['default'], RetryNum: 0, RetryInterval: 60 },
55+
{ ErrorCode: ['432'], RetryNum: -1, RetryInterval: 60 },
56+
],
57+
},
58+
RequestId: expect.any(String),
59+
});
60+
});
61+
test('[asyncRunEnable and traceEnable] update', async () => {
62+
await sleep(3000);
63+
inputs.asyncRunEnable = true;
64+
inputs.traceEnable = false;
65+
outputs = await scf.deploy(inputs);
66+
67+
expect(outputs.AsyncRunEnable).toBe('TRUE');
68+
expect(outputs.TraceEnable).toBe('FALSE');
69+
});
70+
test('[asyncRunEnable and traceEnable] remove', async () => {
71+
const res = await scf.remove({
72+
functionName: inputs.name,
73+
...outputs,
74+
});
75+
expect(res).toEqual(true);
76+
});
77+
});

__tests__/scf/base.test.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -526,30 +526,4 @@ describe('Scf', () => {
526526
});
527527
expect(res).toEqual(true);
528528
});
529-
test('[asyncRunEnable and traceEnable] create', async () => {
530-
await sleep(3000);
531-
delete inputs.cls;
532-
inputs.asyncRunEnable = true;
533-
inputs.traceEnable = true;
534-
outputs = await scf.deploy(inputs);
535-
536-
expect(outputs.AsyncRunEnable).toBe('TRUE');
537-
expect(outputs.TraceEnable).toBe('TRUE');
538-
});
539-
test('[asyncRunEnable and traceEnable] update', async () => {
540-
await sleep(3000);
541-
inputs.asyncRunEnable = true;
542-
inputs.traceEnable = false;
543-
outputs = await scf.deploy(inputs);
544-
545-
expect(outputs.AsyncRunEnable).toBe('TRUE');
546-
expect(outputs.TraceEnable).toBe('FALSE');
547-
});
548-
test('[asyncRunEnable and traceEnable] remove', async () => {
549-
const res = await scf.remove({
550-
functionName: inputs.name,
551-
...outputs,
552-
});
553-
expect(res).toEqual(true);
554-
});
555529
});

src/modules/scf/apis.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const ACTIONS = [
77
'GetFunction',
88
'UpdateFunctionCode',
99
'UpdateFunctionConfiguration',
10+
'GetFunctionEventInvokeConfig',
11+
'UpdateFunctionEventInvokeConfig',
1012
'CreateTrigger',
1113
'DeleteTrigger',
1214
'PublishVersion',

src/modules/scf/entities/scf.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,36 @@ export default class ScfEntity extends BaseEntity {
216216
return true;
217217
}
218218

219+
// 获取异步函数配置
220+
async getAsyncRetryConfig(inputs: ScfCreateFunctionInputs, funcInfo: FunctionInfo) {
221+
const reqParams = {
222+
Namespace: inputs.namespace || funcInfo.Namespace,
223+
FunctionName: inputs.name || funcInfo.FunctionName,
224+
Qualifier: inputs.qualifier || funcInfo.Qualifier || '$LATEST',
225+
};
226+
227+
const reqInputs: Partial<typeof reqParams> = reqParams;
228+
229+
const res = await this.request({ Action: 'GetFunctionEventInvokeConfig', ...reqInputs });
230+
return res;
231+
}
232+
async updateAsyncRetry(inputs: ScfCreateFunctionInputs, funcInfo: FunctionInfo) {
233+
console.log(`Updating function ${inputs.name} async retry configure, region ${this.region}`);
234+
const reqParams = {
235+
Namespace: inputs.namespace || funcInfo.Namespace,
236+
FunctionName: inputs.name || funcInfo.FunctionName,
237+
AsyncTriggerConfig: {
238+
MsgTTL: inputs.msgTTL || 21600,
239+
RetryConfig: [{ RetryNum: inputs.retryNum ?? 2 }],
240+
},
241+
};
242+
243+
const reqInputs: Partial<typeof reqParams> = reqParams;
244+
245+
await this.request({ Action: 'UpdateFunctionEventInvokeConfig', ...reqInputs });
246+
return true;
247+
}
248+
219249
// delete function
220250
async delete({ namespace, functionName }: { namespace: string; functionName: string }) {
221251
namespace = namespace || CONFIGS.defaultNamespace;

src/modules/scf/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,13 @@ export default class Scf {
288288
await this.scf.updateConfigure(inputs, funcInfo);
289289
}
290290

291+
await this.scf.isOperational({ namespace, functionName });
292+
293+
// 如果是异步函数,判断是否需要更新异步调用重试配置
294+
if (inputs.asyncRunEnable) {
295+
await this.scf.updateAsyncRetry(inputs, funcInfo!);
296+
}
297+
291298
funcInfo = await this.scf.isOperational({ namespace, functionName });
292299

293300
const outputs = (funcInfo as any) || ({} as ScfDeployOutputs);

src/modules/scf/interface.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export interface FunctionInfo {
8787
Tags: Tag[];
8888
ClsLogsetId: string;
8989
ClsTopicId: string;
90+
Qualifier: string;
9091
}
9192

9293
export interface ScfPublishVersionInputs {
@@ -192,6 +193,8 @@ export interface ScfCreateFunctionInputs {
192193
userId?: string;
193194
}[];
194195

196+
qualifier?: string;
197+
195198
asyncRunEnable?: undefined | boolean;
196199
traceEnable?: undefined | boolean;
197200
installDependency?: undefined | boolean;
@@ -209,6 +212,10 @@ export interface ScfCreateFunctionInputs {
209212
// 启动命令参数
210213
args?: string;
211214
};
215+
216+
// 异步调用重试配置
217+
msgTTL?: number; // 消息保留时间,单位秒
218+
retryNum?: number; // 重试次数
212219
}
213220

214221
export interface ScfUpdateAliasTrafficInputs {

0 commit comments

Comments
 (0)