Compare commits

..

1 Commits

Author SHA1 Message Date
wss-git 853611b69f fix: invoke function add timeout 2021-12-27 18:09:00 +08:00
6 changed files with 21 additions and 5 deletions

2
dist/index.js vendored
View File

@ -33,7 +33,7 @@ You also can refer to the usage of fc-api and execute [s cli fc-api -h] for help
FC Invoke Result:`,"green"),console.log(a),se.log(`
`)}async getHttpTrigger(r,i){let{data:o}=await this.fcClient.listTriggers(r,i);if(se.debug(`get listTriggers: ${JSON.stringify(o)}`),ri.default.isEmpty(o.triggers))return[];let a=o.triggers.filter(c=>c.triggerType==="http"||c.triggerType==="https");return ri.default.isEmpty(a)?[]:a}async eventInvoke({serviceName:r,functionName:i,event:o,qualifier:a="LATEST",invocationType:c,statefulAsyncInvocationId:f}){if(c==="Sync"){let d=await this.fcClient.invokeFunction(r,i,o,{"X-Fc-Log-Type":"Tail","X-Fc-Invocation-Code-Version":"Latest","X-Fc-Invocation-Type":c},a);this.showLog(d.headers["x-fc-log-result"]),se.log(`
FC Invoke Result:`,"green"),console.log(d.data),console.log(`
`)}else{se.debug(`Stateful async invocation id: ${f}`);let{headers:d}=await this.fcClient.invokeFunction(r,i,o,{"X-Fc-Invocation-Code-Version":"Latest","X-Fc-Invocation-Type":c,"X-Fc-Stateful-Async-Invocation-Id":f||""},a),g=d["x-fc-request-id"];se.log(`
`)}else{se.debug(`Stateful async invocation id: ${f}`);let{headers:d}=await this.fcClient.invokeFunction(r,i,o,{"X-Fc-Invocation-Code-Version":"Latest","X-Fc-Invocation-Type":c,"X-Fc-Stateful-Async-Invocation-Id":f},a),g=d["x-fc-request-id"];se.log(`
${r}/${i} async invoke success.
${g?`request id: ${g}
`:""}`,"green")}}async httpInvoke({region:r,serviceName:i,functionName:o,event:a,qualifier:c}){let f=c?`.${c}`:"";a.path=`/proxy/${i}${f}/${o}/${a.path||""}`,se.log(`Request url: https://${this.accountId}.${r}.fc.aliyuncs.com/2016-08-15/proxy/${i}${f}/${o}/`),await this.request(a)}async request(r){let{headers:i={},queries:o,method:a="GET",path:c,body:f}=r;i["X-Fc-Log-Type"]||(i["X-Fc-Log-Type"]="Tail"),i["X-Fc-Invocation-Code-Version"]||(i["X-Fc-Invocation-Code-Version"]="Latest");let d;try{let g=a.toLocaleUpperCase();se.debug(`method is ${g}.`),se.debug("start invoke."),g==="GET"?d=await this.fcClient.costom_request("GET",c,o,null,i):g==="POST"?d=await this.fcClient.costom_request("POST",c,o,f,i):g==="PUT"?d=await this.fcClient.costom_request("PUT",c,null,f,i):g==="DELETE"?d=await this.fcClient.costom_request("DELETE",c,o,null,i):a.toLocaleUpperCase()==="PATCH"?d=await this.fcClient.costom_request("PATCH",c,o,f,i):a.toLocaleUpperCase()==="HEAD"?d=await this.fcClient.costom_request("HEAD",c,o,f,i):se.error(`Does not support ${a} requests temporarily.`)}catch(g){throw se.debug(g),g.message==="Unexpected token r in JSON at position 0"&&g.stack.includes("/fc2/lib/client.js")&&g.stack.includes("at Client.request")?new Error("The body in http responss is not in json format, but the content-type in response header is application/json. We recommend that you make the format of the response body be consistent with the content-type in response header."):g}se.debug("end invoke."),(d==null?void 0:d.err)?(this.showLog(d.headers["x-fc-log-result"]),se.log(`

View File

@ -1,6 +1,6 @@
{
"name": "fc-remote-invoke",
"version": "0.0.22",
"version": "0.0.20",
"description": "This is a component demo for Serverless Devs Tool ",
"keywords": [
"Serverless",

View File

@ -2,7 +2,7 @@ Type: Component
Name: fc-remote-invoke
Provider:
- 其它
Version: 0.0.22
Version: 0.0.21
Description: 初始化component模板
HomePage: https://www.serverless-devs.com
Tags: #标签详情

View File

@ -15,6 +15,7 @@ export default class FcRemoteInvoke {
async invoke(inputs: InputProps): Promise<any> {
const {
props,
timeout,
eventPayload,
credentials,
isHelp,
@ -31,7 +32,7 @@ export default class FcRemoteInvoke {
let fcClient;
if (!props.domainName) {
const fcCommon = await core.loadComponent('devsapp/fc-common');
fcClient = await fcCommon.makeFcClient({ ...inputs, props: { region: props.region }});
fcClient = await fcCommon.makeFcClient({ ...inputs, props: { region: props.region, timeout }});
}
const remoteInvoke = new RemoteInvoke(fcClient, credentials.AccountID);
await remoteInvoke.invoke(props, eventPayload, { invocationType, statefulAsyncInvocationId });
@ -51,6 +52,7 @@ export default class FcRemoteInvoke {
const parsedArgs: {[key: string]: any} = core.commandParse({ ...inputs, args }, {
boolean: ['help', 'event-stdin'],
number: ['timeout'],
string: ['invocation-type', 'event', 'event-file', 'region', 'domain-name','service-name', 'function-name', 'qualifier', 'stateful-async-invocation-id'],
alias: {
'help': 'h',
@ -108,8 +110,20 @@ export default class FcRemoteInvoke {
throw new Error('region/serviceName(service-name)/functionName(function-name) can not be empty.');
}
// 超时时间获取的原理https://github.com/devsapp/fc/issues/480
const propsTimeout = argsData.timeout || inputs.props?.timeout;
let timeout = 600;
if (_.isNumber(propsTimeout)) {
if (_.isEmpty(inputs.props?.runtime) || inputs.props?.runtime === 'custom-container') {
timeout = propsTimeout + 7 * 60;
} else {
timeout = propsTimeout + 2 * 60;
}
}
return {
props,
timeout,
credentials: inputs.credentials,
eventPayload,
isHelp: false,

View File

@ -25,8 +25,10 @@ export interface IProperties {
region: string;
serviceName: string;
functionName: string;
runtime?: string;
qualifier?: string;
domainName?: string;
timeout?: string;
}
export function isProperties(args: any): args is IProperties {
if (!args) {

View File

@ -95,7 +95,7 @@ export default class RemoteInvoke {
const { headers } = await this.fcClient.invokeFunction(serviceName, functionName, event, {
'X-Fc-Invocation-Code-Version': 'Latest',
'X-Fc-Invocation-Type': invocationType,
'X-Fc-Stateful-Async-Invocation-Id': statefulAsyncInvocationId || "",
'X-Fc-Stateful-Async-Invocation-Id': statefulAsyncInvocationId,
}, qualifier);
const rId = headers['x-fc-request-id'];