长 运行 App Engine 脚本 "Quitting on terminated signal"

Long Running App Engine Script "Quitting on terminated signal"

我正在尝试通过 App Engine 运行 几个长的 运行ning 脚本。我是初学者,这是我使用 App Engine 和 express 的第一个项目。

我正在使用 express 在 Node 中处理请求。

当我(或 cron 作业)向我的任何端点发送请求时,脚本似乎 运行 正常,直到大约 5-10 分钟后的一个随机点,我得到以下日志:

  1. 项目收到“/_ah/stop”请求
  2. “正在终止信号”消息
  3. “启动程序失败:用户应用程序失败,退出代码为 -1(有关详细信息,请参阅 stdout/stderr 日志):信号:已终止”

我不明白为什么会这样。

我的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);
});

在本地 运行ning 时绝对没有问题。知道我在做什么来获得过早的 /_ah/stop 请求吗?因为我使用的是基本缩放,所以我不会超时。 google 将其描述为:

“24 小时用于 HTTP 请求和任务队列任务。如果您的应用程序未在此时间限制内 return 请求,App Engine 会中断请求处理程序并发出错误以供您的代码处理。 “

有什么想法吗?也许与我处理 /_ah/start 的方式有关,这只是在黑暗中的一枪?

我发现因为我很少向应用程序发送请求,所以在我的脚本完成之前我遇到了实例空闲超时/

因此,即使任务仍在应用程序上 运行,因为它在过去 ~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);
};

看起来有点奇怪,但它确实有效。如果您知道更好的方法,请告诉我。