使异步节点脚本 运行 永远

Make async node script run forever

我搜索了很多,但还没有找到解决我问题的具体方法。 我有一个异步函数,它轮询数据库作为节点脚本,当使用节点 12 时,它永远 运行s,但在 v14 中,实现发生了变化,它在 运行ning 一次后立即关闭。

(async function pollDatabase() {
    const db = new Db();

    return async.forever(async function (pollFn) {
 
        const query = "SELECT * FROM templates WHERE data->>'isProcessed'='0' LIMIT 1;";
        const templates = await db.query(query);

        if (!templates || !templates.length) {
            setTimeout(pollFn, 1000);
            return;
        }

        await doSomethingWithTemplateAndReExecuteThisFunction(templates[0], pollFn);
    });
})();

奇怪的是,例如,一个快速服务器只是保持 运行ning,但我还没有弄清楚它是如何工作的。我不打算将此后台脚本转换为服务器。关于什么是使 运行 永远成为某种后台任务的最佳方法,有什么想法吗?目前它是一个 docker 容器,仅包含此脚本。

最后我自己解决了。我不知道为什么切换到 Node 14 会发生这种情况,但过时的 pg-promisebluebird 库的组合似乎是罪魁祸首。我想这可能与现在在 Node 14 中处理 Promises 的方式有关。

旧版本: pg-promise@3.2.6bluebird@2.10.2

新版本: pg-promise@^10.9.4 使用本机 Promise 实现