Merge pull request #16 from devsapp/support-stateful-async-invocation-id

feat: add --stateful-async-invocation-id args
This commit is contained in:
wss-git 2021-11-16 13:29:42 +08:00 committed by GitHub
commit 978e57fef7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 10 deletions

View File

@ -19,6 +19,7 @@ export default class FcRemoteInvoke {
credentials, credentials,
isHelp, isHelp,
invocationType, invocationType,
statefulAsyncInvocationId,
} = await this.handlerInputs(inputs); } = await this.handlerInputs(inputs);
await this.report('fc-remote-invoke', 'invoke', credentials?.AccountID); await this.report('fc-remote-invoke', 'invoke', credentials?.AccountID);
@ -33,7 +34,7 @@ export default class FcRemoteInvoke {
fcClient = await fcCommon.makeFcClient({ ...inputs, props: { region: props.region }}); fcClient = await fcCommon.makeFcClient({ ...inputs, props: { region: props.region }});
} }
const remoteInvoke = new RemoteInvoke(fcClient, credentials.AccountID); const remoteInvoke = new RemoteInvoke(fcClient, credentials.AccountID);
await remoteInvoke.invoke(props, eventPayload, { invocationType }); await remoteInvoke.invoke(props, eventPayload, { invocationType, statefulAsyncInvocationId });
} }
private async report(componentName: string, command: string, accountID: string): Promise<void> { private async report(componentName: string, command: string, accountID: string): Promise<void> {
@ -50,7 +51,7 @@ export default class FcRemoteInvoke {
const parsedArgs: {[key: string]: any} = core.commandParse({ ...inputs, args }, { const parsedArgs: {[key: string]: any} = core.commandParse({ ...inputs, args }, {
boolean: ['help', 'event-stdin'], boolean: ['help', 'event-stdin'],
string: ['invocation-type', 'event', 'event-file', 'region', 'domain-name','service-name', 'function-name', 'qualifier'], string: ['invocation-type', 'event', 'event-file', 'region', 'domain-name','service-name', 'function-name', 'qualifier', 'stateful-async-invocation-id'],
alias: { alias: {
'help': 'h', 'help': 'h',
'event': 'e', 'event': 'e',
@ -73,6 +74,7 @@ export default class FcRemoteInvoke {
'event-stdin': eventStdin, 'event-stdin': eventStdin,
'invocation-type': invocationType = 'sync', 'invocation-type': invocationType = 'sync',
'domain-name': domainName, 'domain-name': domainName,
'stateful-async-invocation-id': statefulAsyncInvocationId,
} = argsData; } = argsData;
const eventPayload = { event, eventFile, eventStdin }; const eventPayload = { event, eventFile, eventStdin };
// @ts-ignore: 判断三个值有几个真 // @ts-ignore: 判断三个值有几个真
@ -111,6 +113,7 @@ export default class FcRemoteInvoke {
eventPayload, eventPayload,
isHelp: false, isHelp: false,
invocationType: _.upperFirst(invocationType), invocationType: _.upperFirst(invocationType),
statefulAsyncInvocationId,
}; };
} }

View File

@ -13,7 +13,7 @@ export default class RemoteInvoke {
this.accountId = accountId; this.accountId = accountId;
} }
async invoke (props: IProperties, eventPayload: IEventPayload, { invocationType }) { async invoke (props: IProperties, eventPayload: IEventPayload, { invocationType, statefulAsyncInvocationId }) {
const event = await Event.eventPriority(eventPayload); const event = await Event.eventPriority(eventPayload);
logger.debug(`event: ${event}`); logger.debug(`event: ${event}`);
@ -32,12 +32,13 @@ export default class RemoteInvoke {
const payload: any = { event, serviceName, functionName, qualifier }; const payload: any = { event, serviceName, functionName, qualifier };
if (_.isEmpty(httpTriggers)) { if (_.isEmpty(httpTriggers)) {
payload.invocationType = invocationType; payload.invocationType = invocationType;
payload.statefulAsyncInvocationId = statefulAsyncInvocationId;
payload.event = event; payload.event = event;
await this.eventInvoke(payload); await this.eventInvoke(payload);
} else { } else {
payload.region = region; payload.region = region;
payload.event = this.getJsonEvent(event); payload.event = this.getJsonEvent(event);
await this.httpInvoke(payload); await this.httpInvoke(payload);
} }
} }
@ -48,9 +49,9 @@ export default class RemoteInvoke {
payload.headers = {}; payload.headers = {};
} }
payload.headers['X-Fc-Log-Type'] = 'Tail'; payload.headers['X-Fc-Log-Type'] = 'Tail';
const { body, headers } = await got(url, payload); const { body, headers } = await got(url, payload);
this.showLog(headers['x-fc-log-result']); this.showLog(headers['x-fc-log-result']);
logger.log('\nFC Invoke Result:', 'green'); logger.log('\nFC Invoke Result:', 'green');
console.log(body); console.log(body);
@ -74,13 +75,14 @@ export default class RemoteInvoke {
functionName, functionName,
event, event,
qualifier = 'LATEST', qualifier = 'LATEST',
invocationType invocationType,
statefulAsyncInvocationId
}) { }) {
if (invocationType === 'Sync') { if (invocationType === 'Sync') {
const rs = await this.fcClient.invokeFunction(serviceName, functionName, event, { const rs = await this.fcClient.invokeFunction(serviceName, functionName, event, {
'X-Fc-Log-Type': 'Tail', 'X-Fc-Log-Type': 'Tail',
'X-Fc-Invocation-Type': invocationType 'X-Fc-Invocation-Type': invocationType,
}, qualifier); }, qualifier);
this.showLog(rs.headers['x-fc-log-result']); this.showLog(rs.headers['x-fc-log-result']);
@ -88,8 +90,10 @@ export default class RemoteInvoke {
console.log(rs.data); console.log(rs.data);
console.log('\n'); console.log('\n');
} else { } else {
logger.debug(`Stateful async invocation id: ${statefulAsyncInvocationId}`);
const { headers } = await this.fcClient.invokeFunction(serviceName, functionName, event, { const { headers } = await this.fcClient.invokeFunction(serviceName, functionName, event, {
'X-Fc-Invocation-Type': invocationType 'X-Fc-Invocation-Type': invocationType,
'X-Fc-Stateful-Async-Invocation-Id': statefulAsyncInvocationId,
}, qualifier); }, qualifier);
const rId = headers['x-fc-request-id']; const rId = headers['x-fc-request-id'];
@ -177,4 +181,4 @@ export default class RemoteInvoke {
throw new Error('handler event error. Example: https://github.com/devsapp/fc-remote-invoke/blob/master/example/http.json'); throw new Error('handler event error. Example: https://github.com/devsapp/fc-remote-invoke/blob/master/example/http.json');
} }
} }
} }