Skip to content

Commit 6a73ad2

Browse files
authored
Merge pull request #1088 from alibaba/fix-api-invoke-cmd-args-too-long-problem
fix api invoke cmd args too long problem;add response when api invoke…
2 parents 558dd0a + 87c0de8 commit 6a73ad2

3 files changed

Lines changed: 36 additions & 20 deletions

File tree

src/lib/docker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ function genDockerCmdOfCustomContainer(functionProps) {
257257
}
258258
return [];
259259
}
260+
// dockerode exec 在 windows 上有问题,用 exec 的 stdin 传递事件,当调用 stream.end() 时,会直接导致 exec 退出,且 ExitCode 为 null
260261
function genDockerCmdOfNonCustomContainer(functionProps, httpMode, invokeInitializer = true, event = null) {
261262
const cmd = ['-h', functionProps.Handler];
262263

@@ -291,7 +292,6 @@ function genDockerCmdOfNonCustomContainer(functionProps, httpMode, invokeInitial
291292
return cmd;
292293
}
293294

294-
// dockerode exec 在 windows 上有问题,用 exec 的 stdin 传递事件,当调用 stream.end() 时,会直接导致 exec 退出,且 ExitCode 为 null
295295
function generateDockerCmd(runtime, isLocalStartInit, { functionProps, httpMode, invokeInitializer = true, event = null }) {
296296
if (isCustomContainerRuntime(runtime)) {
297297
return genDockerCmdOfCustomContainer(functionProps);

src/lib/local/api-invoke.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const streams = require('memory-streams');
55
const { parseOutputStream, getFcReqHeaders } = require('./http');
66
const debug = require('debug')('fun:local');
77
const { validateSignature, getHttpRawBody, generateInitRequestOpts, requestUntilServerUp, generateInvokeRequestOpts } = require('./http');
8-
const { red } = require('colors');
8+
const { red, yellow } = require('colors');
99
const docker = require('../docker');
1010
const dockerOpts = require('../docker-opts');
1111
const uuid = require('uuid');
@@ -30,29 +30,28 @@ class ApiInvoke extends Invoke {
3030
this.cmd = docker.generateDockerCmd(this.runtime, false, {
3131
functionProps: this.functionProps,
3232
httpMode: true,
33-
invokeInitializer,
34-
event
33+
invokeInitializer
3534
});
3635

3736
const outputStream = new streams.WritableStream();
3837
const errorStream = new streams.WritableStream();
3938

4039
// check signature
4140
if (!await validateSignature(req, res, req.method)) { return; }
42-
const opts = await dockerOpts.generateLocalStartOpts(this.runtime,
43-
containerName,
44-
this.mounts,
45-
this.cmd,
46-
this.envs,
47-
{
48-
debugPort: this.debugPort,
49-
dockerUser: this.dockerUser,
50-
debugIde: this.debugIde,
51-
imageName: this.imageName,
52-
caPort: this.functionProps.CAPort
53-
});
5441
if (isCustomContainerRuntime(this.runtime)) {
55-
42+
const opts = await dockerOpts.generateLocalStartOpts(this.runtime,
43+
containerName,
44+
this.mounts,
45+
this.cmd,
46+
this.envs,
47+
{
48+
debugPort: this.debugPort,
49+
dockerUser: this.dockerUser,
50+
debugIde: this.debugIde,
51+
imageName: this.imageName,
52+
caPort: this.functionProps.CAPort
53+
}
54+
);
5655
const containerRunner = await docker.runContainer(opts, outputStream, errorStream, {
5756
serviceName: this.serviceName,
5857
functionName: this.functionName
@@ -74,12 +73,20 @@ class ApiInvoke extends Invoke {
7473
const requestOpts = generateInvokeRequestOpts(this.functionProps.CAPort, fcReqHeaders, event);
7574

7675
const respOfCustomContainer = await requestUntilServerUp(requestOpts, this.functionProps.Timeout || 3);
77-
// console.log(respOfCustomContainer.body);
76+
7877
// exit container
7978
this.responseOfCustomContainer(res, respOfCustomContainer);
8079
await docker.exitContainer(container);
8180
} else {
8281

82+
const opts = await dockerOpts.generateLocalInvokeOpts(this.runtime,
83+
containerName,
84+
this.mounts,
85+
this.cmd,
86+
this.debugPort,
87+
this.envs,
88+
this.dockerUser,
89+
this.debugIde);
8390
await docker.run(opts,
8491
event,
8592
outputStream,
@@ -97,6 +104,10 @@ class ApiInvoke extends Invoke {
97104
// responseApi
98105
response(outputStream, errorStream, res) {
99106
const errorResponse = errorStream.toString();
107+
// 当容器的输出为空异常时
108+
if (outputStream.toString() === '') {
109+
console.log(yellow('Warning: outputStream of CA container is empty'));
110+
}
100111

101112
let { statusCode, body, requestId, billedTime, memoryUsage } = parseOutputStream(outputStream);
102113

@@ -109,7 +120,12 @@ class ApiInvoke extends Invoke {
109120
'access-control-expose-headers': 'Date,x-fc-request-id,x-fc-error-type,x-fc-code-checksum,x-fc-invocation-duration,x-fc-max-memory-usage,x-fc-log-result,x-fc-invocation-code-version'
110121
};
111122

112-
res.status(statusCode);
123+
if (statusCode) {
124+
res.status(statusCode);
125+
} else {
126+
res.status(500);
127+
}
128+
113129

114130
// todo: fix body 后面多个换行的 bug
115131
if (errorResponse) { // process HandledInvocationError and UnhandledInvocationError

src/lib/local/http-invoke.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ class HttpInvoke extends Invoke {
229229
});
230230

231231
this._invokeInitializer = false;
232-
this.response(outputStream, errorStream, res);
233232
} catch (error) {
234233
console.log(red('Fun Error: ', errorStream.toString()));
235234

@@ -253,6 +252,7 @@ class HttpInvoke extends Invoke {
253252
}
254253
return;
255254
}
255+
this.response(outputStream, errorStream, res);
256256
}
257257
debug('http doInvoke exec end, begin to response');
258258
}

0 commit comments

Comments
 (0)