Skip to content

Commit

Permalink
Merge pull request #16 from devsapp/support-stateful-async-invocation-id
Browse files Browse the repository at this point in the history
feat: add --stateful-async-invocation-id args
  • Loading branch information
wss-git authored Nov 16, 2021
2 parents bf6593e + 9c10415 commit 978e57f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default class FcRemoteInvoke {
credentials,
isHelp,
invocationType,
statefulAsyncInvocationId,
} = await this.handlerInputs(inputs);
await this.report('fc-remote-invoke', 'invoke', credentials?.AccountID);

Expand All @@ -33,7 +34,7 @@ export default class FcRemoteInvoke {
fcClient = await fcCommon.makeFcClient({ ...inputs, props: { region: props.region }});
}
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> {
Expand All @@ -50,7 +51,7 @@ export default class FcRemoteInvoke {

const parsedArgs: {[key: string]: any} = core.commandParse({ ...inputs, args }, {
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: {
'help': 'h',
'event': 'e',
Expand All @@ -73,6 +74,7 @@ export default class FcRemoteInvoke {
'event-stdin': eventStdin,
'invocation-type': invocationType = 'sync',
'domain-name': domainName,
'stateful-async-invocation-id': statefulAsyncInvocationId,
} = argsData;
const eventPayload = { event, eventFile, eventStdin };
// @ts-ignore: 判断三个值有几个真
Expand Down Expand Up @@ -111,6 +113,7 @@ export default class FcRemoteInvoke {
eventPayload,
isHelp: false,
invocationType: _.upperFirst(invocationType),
statefulAsyncInvocationId,
};
}

Expand Down
20 changes: 12 additions & 8 deletions src/lib/remote-invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class RemoteInvoke {
this.accountId = accountId;
}

async invoke (props: IProperties, eventPayload: IEventPayload, { invocationType }) {
async invoke (props: IProperties, eventPayload: IEventPayload, { invocationType, statefulAsyncInvocationId }) {
const event = await Event.eventPriority(eventPayload);
logger.debug(`event: ${event}`);

Expand All @@ -32,12 +32,13 @@ export default class RemoteInvoke {
const payload: any = { event, serviceName, functionName, qualifier };
if (_.isEmpty(httpTriggers)) {
payload.invocationType = invocationType;
payload.statefulAsyncInvocationId = statefulAsyncInvocationId;
payload.event = event;
await this.eventInvoke(payload);
} else {
payload.region = region;
payload.event = this.getJsonEvent(event);

await this.httpInvoke(payload);
}
}
Expand All @@ -48,9 +49,9 @@ export default class RemoteInvoke {
payload.headers = {};
}
payload.headers['X-Fc-Log-Type'] = 'Tail';

const { body, headers } = await got(url, payload);

this.showLog(headers['x-fc-log-result']);
logger.log('\nFC Invoke Result:', 'green');
console.log(body);
Expand All @@ -74,22 +75,25 @@ export default class RemoteInvoke {
functionName,
event,
qualifier = 'LATEST',
invocationType
invocationType,
statefulAsyncInvocationId
}) {

if (invocationType === 'Sync') {
const rs = await this.fcClient.invokeFunction(serviceName, functionName, event, {
'X-Fc-Log-Type': 'Tail',
'X-Fc-Invocation-Type': invocationType
'X-Fc-Invocation-Type': invocationType,
}, qualifier);

this.showLog(rs.headers['x-fc-log-result']);
logger.log('\nFC Invoke Result:', 'green');
console.log(rs.data);
console.log('\n');
} else {
logger.debug(`Stateful async invocation id: ${statefulAsyncInvocationId}`);
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);
const rId = headers['x-fc-request-id'];

Expand Down Expand Up @@ -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');
}
}
}
}

0 comments on commit 978e57f

Please sign in to comment.