节点JS-在集群模式下或在负载平衡器后面执行多次的cron作业

发布于 2025-02-09 18:35:17 字数 543 浏览 1 评论 0原文

我有使用PM2在EC2实例上以群集模式工作的Nestjs服务器。

我通过使用PM2进程的名称启动使用多个名称配置来启动服务器,成功设置了Cron作业仅在集群模式下执行的CRON作业。

{
    "apps":[
        {
            "script":"dist/main.js",
            "instances": "1",
            "exec_mode": "cluster",
            "name":"queue"
        },
        {
            "script":"dist/main.js",
            "instances": "1",
            "exec_mode": "cluster",
            "name":"coco"
        }

    ]
}

但是我想知道当多个实例在负载平衡器后面时如何处理这种情况。由于作业是在每个实例上安排的,并使用远程数据库的相同数据执行了多次。

任何帮助将不胜感激。

I have a NestJS server working in cluster mode on ec2 instance using pm2.

I have successfully set up cron jobs executing only one time in the cluster mode by starting the server with multiple name configurations using the name of the pm2 process.

{
    "apps":[
        {
            "script":"dist/main.js",
            "instances": "1",
            "exec_mode": "cluster",
            "name":"queue"
        },
        {
            "script":"dist/main.js",
            "instances": "1",
            "exec_mode": "cluster",
            "name":"coco"
        }

    ]
}

But I want to know how to handle this case when multiple instances are behind the load balancer. As the jobs are scheduled on each instance and executed multiple times with the same data from the remote database.

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文