我正试图通过应用引擎运行几个长时间运行的脚本。我是一个初学者,这是我使用App和express的第一个项目。
我正在用快递处理节点中的请求。
当我(或cron作业)向我的任何端点发送请求时,脚本似乎运行良好,直到一个随机点~5-10分钟后,我得到以下日志:
项目接收终止信号上的"/_ah/stop“request
“
我不知道为什么会发生这种事。
我的app.yaml:
runtime: nodejs10
instance_class: B2
basic_scaling:
max_instances: 25
idle_timeout: 12m请求处理程序代码:
app.get("/longRunningFunctionOne", async (req, res) => {
await longRunningFunctionOne();
res.send("DONE");
});
app.get("/longRunningFunctionTwo", async (req, res) => {
await longRunningFunctionTwo();
res.send("DONE");
});
app.get("/_ah/start", async (req, res) => {
res.sendStatus(200);
});在本地运行时绝对没有问题。知道我在做什么来得到过早的/_ah/停止请求吗?因为我使用的是基本的缩放,所以我不会得到超时。谷歌将其描述为:
"24小时执行HTTP请求和任务队列任务。如果应用程序在此时限内不返回请求,app将中断请求处理程序,并发出一个错误供您的代码处理。“
有什么想法吗?也许与我如何处理/_ah/start有关,这只是一次黑暗中的射击?
发布于 2020-07-05 22:22:47
我发现,由于我向应用程序发送非常少的请求,所以在脚本完成/之前,我就点击了实例空闲超时。
因此,即使应用程序上仍然运行着一个任务,因为它在最后的15分钟内没有收到http请求,它还是会发送/_ah/stop请求并关闭实例。
为了在脚本运行时保持实例的活力,我创建了一个函数,它每分钟向应用程序发送一个请求,以保持其正常运行,而不触发空闲超时。
我称之为Beegees“活着”的功能:
const appUrl = "https://myurl.appspot.com/";
app.get("/script1", async (req, res) => {
res.send("running script 1");
stayAlive(script1);
});
app.get("/script2", async (req, res) => {
res.send("running script 2");
stayAlive(script2);
});
app.get("/", async (req, res) => {
res.send(" ");
});
app.listen(process.env.PORT || 4000, () => {
console.log(`Listening on port ${process.env.PORT || 4000}`);
});
const stayAlive = async (mainAsyncFunc) => {
const intervalId = setInterval(sendAppRequest, 60000);
await mainAsyncFunc();
clearInterval(intervalId);
};
const sendAppRequest = () => {
console.log("Stayin alive");
axios.get(appUrl);
};听起来有点奇怪但很管用。如果你知道更好的方法,请告诉我。
https://stackoverflow.com/questions/62713011
复制相似问题