使异步节点脚本永远运行

时间:2021-03-05 10:00:58

标签: node.js typescript asynchronous task

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

(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);
    });
})();

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

1 个答案:

答案 0 :(得分:0)

最后我自己解决了。我不知道为什么会在切换到 Node 14 时发生这种情况,但过时的 version: '3.7' services: nginx: image: nginx:1-alpine networks: - oldcity volumes: - nginx_cache:/var/cache/nginx - nginx_run:/var/run - /etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro - /etc/nginx/sites/:/etc/nginx/sites/:ro - /srv/www/cdn:/usr/share/nginx/html:ro deploy: mode: replicated replicas: 1 placement: constraints: [node.hostname == holod] labels: - "traefik.enable=true" - "traefik.docker.network=oldcity" - "traefik.port=80" - "traefik.protocol=http" - "traefik.screenshots.frontend.rule=Host:scr.vodolaz095.life" - "traefik.cdn.frontend.rule=HostRegexp:{catchall:.*}" - "traefik.cdn.frontend.priority=2" volumes: nginx_cache: nginx_run: networks: oldcity: driver: overlay external: true pg-promise 库的组合似乎是罪魁祸首。我想这可能与 Node 14 现在处理 Promise 的方式有关。

旧版本: bluebirdpg-promise@3.2.6

新版本: bluebird@2.10.2 与原生 Promise 实现

相关问题