Node.js的超时重启API调用
平台:
我在sails.js的API,并在react一个前端。前端和后端之间的呼叫被用做fetch api。
更多信息:
在一些API端点的我当然要执行一个外部文件,在这一点上我使用execfile()的node.js功能,我必须等待它要执行向前端回应。
问题是什么?
如果文件在很短的时间执行,例如少于1分钟一切运行良好,双方预期出现的问题,但如果(在这种情况下)的文件需要1分钟以上来执行,有东西触发对API的第二个电话(我不知道这是被调用,但我测试了postman同一个端点,所以我怀疑我没有这个问题的反应/取-API)和API调用具有相同数据从第一个电话是重做。这将导致运行两次文件。
一些更奇怪的是,如果你有DevTools网络督察开启第二次通话不会出现,但没有的sailjs点,这种行为的文件中。
在sails.js端点的实施例:
/**
* FooController
*/
const execFile = require("child_process").execFile;
module.exports = {
foo: async (req, res) => {
let result = await module.exports._executeScript(req.body).catch(() => {
res.status(500).json({ error: "something has occurred" });
});
res.json(result);
},
_executeScript: body => {
return new Promise((resolve, reject) => {
let args = [process.cwd() + "/scripts/" + "myExternalFile.js", body];
let elem = await module.exports
._execScript(args)
.catch(err => reject(err));
resolve(elem);
});
},
_execScript: args => {
return new Promise((resolve, reject) => {
try {
execFile("node", args, { timeout: 150000 }, (error, stdout, stderr) => {
if (error || (stderr != null && stderr !== "")) {
console.error(error);
} else {
console.log(stdout);
}
let output = { stdout: stdout, stderr: stderr };
resolve(output);
});
} catch (err) {
reject(err);
}
});
}
};
组分的例子与取调用反应:
import React from "react";
import { notification } from "antd";
import "./../App.css";
import Oauth from "./../helper/Oauth";
import Config from "./../config.json";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
syncInAction: false,
data: null
};
}
componentDidMount() {
this.handleSync();
}
async handleSync() {
let response = await fetch(Config.apiLink + "/foo/foo", {
method: "POST",
mode: "cors",
headers: {
Authorization: Oauth.isLoggedIn(),
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(this.state.myData)
}).catch(err => {
notification.error({
key: "catch-ApiFail",
message: "Erro"
});
});
let json = await response.json();
this.setState({
syncInAction: false,
data: json
});
}
render() {
return <div>{this.state.data}</div>;
}
}
export default App;
什么是我的预期目标/行为:
不要紧,如果呼叫需要1分钟或10小时,该文件只能被调用一次,当它完成,然后是,它可以返回到前端。
注意例子不具有原来的代码并没有经过测试。是代码的简化版本,以解释行为
回答如下:我最终解决问题,显然nodejs has a default timing of 2 minutes在服务器上,并且可以改写错过这个timout或增加它。这只是添加一行代码在把foo()端点的开始,问题就解决了。
重做调用的行为是,它没有记录,而且奇怪的是不使用时,邮递员有这样的行为,但这里是谁需要它的解决方案。
最后结果:
foo: async (req, res) => {
req.setTimeout(0);
let result = await module.exports._executeScript(req.body).catch(() => {
res.status(500).json({ error: "something has occurred" });
});
res.json(result);
};
Node.js的超时重启API调用
平台:
我在sails.js的API,并在react一个前端。前端和后端之间的呼叫被用做fetch api。
更多信息:
在一些API端点的我当然要执行一个外部文件,在这一点上我使用execfile()的node.js功能,我必须等待它要执行向前端回应。
问题是什么?
如果文件在很短的时间执行,例如少于1分钟一切运行良好,双方预期出现的问题,但如果(在这种情况下)的文件需要1分钟以上来执行,有东西触发对API的第二个电话(我不知道这是被调用,但我测试了postman同一个端点,所以我怀疑我没有这个问题的反应/取-API)和API调用具有相同数据从第一个电话是重做。这将导致运行两次文件。
一些更奇怪的是,如果你有DevTools网络督察开启第二次通话不会出现,但没有的sailjs点,这种行为的文件中。
在sails.js端点的实施例:
/**
* FooController
*/
const execFile = require("child_process").execFile;
module.exports = {
foo: async (req, res) => {
let result = await module.exports._executeScript(req.body).catch(() => {
res.status(500).json({ error: "something has occurred" });
});
res.json(result);
},
_executeScript: body => {
return new Promise((resolve, reject) => {
let args = [process.cwd() + "/scripts/" + "myExternalFile.js", body];
let elem = await module.exports
._execScript(args)
.catch(err => reject(err));
resolve(elem);
});
},
_execScript: args => {
return new Promise((resolve, reject) => {
try {
execFile("node", args, { timeout: 150000 }, (error, stdout, stderr) => {
if (error || (stderr != null && stderr !== "")) {
console.error(error);
} else {
console.log(stdout);
}
let output = { stdout: stdout, stderr: stderr };
resolve(output);
});
} catch (err) {
reject(err);
}
});
}
};
组分的例子与取调用反应:
import React from "react";
import { notification } from "antd";
import "./../App.css";
import Oauth from "./../helper/Oauth";
import Config from "./../config.json";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
syncInAction: false,
data: null
};
}
componentDidMount() {
this.handleSync();
}
async handleSync() {
let response = await fetch(Config.apiLink + "/foo/foo", {
method: "POST",
mode: "cors",
headers: {
Authorization: Oauth.isLoggedIn(),
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(this.state.myData)
}).catch(err => {
notification.error({
key: "catch-ApiFail",
message: "Erro"
});
});
let json = await response.json();
this.setState({
syncInAction: false,
data: json
});
}
render() {
return <div>{this.state.data}</div>;
}
}
export default App;
什么是我的预期目标/行为:
不要紧,如果呼叫需要1分钟或10小时,该文件只能被调用一次,当它完成,然后是,它可以返回到前端。
注意例子不具有原来的代码并没有经过测试。是代码的简化版本,以解释行为
回答如下:我最终解决问题,显然nodejs has a default timing of 2 minutes在服务器上,并且可以改写错过这个timout或增加它。这只是添加一行代码在把foo()端点的开始,问题就解决了。
重做调用的行为是,它没有记录,而且奇怪的是不使用时,邮递员有这样的行为,但这里是谁需要它的解决方案。
最后结果:
foo: async (req, res) => {
req.setTimeout(0);
let result = await module.exports._executeScript(req.body).catch(() => {
res.status(500).json({ error: "something has occurred" });
});
res.json(result);
};