0

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.

Harish Jangra
  • 381
  • 5
  • 16
  • I don't understand your configuration for the cron jobs. What software are you using to run the cron jobs? – jfriend00 Jun 23 '22 at 05:11
  • @jfriend00 cron jobs are scheduled by the node js code only. https://docs.nestjs.com/techniques/task-scheduling – Harish Jangra Jun 23 '22 at 05:22
  • So, why are you running the cron jobs as clustered if you only want one process to run them? – jfriend00 Jun 23 '22 at 05:32
  • my project is running in cluster mode. so cron jobs are included in that. @jfriend00 – Harish Jangra Jun 24 '22 at 07:43
  • 1
    Well, you need some way of assigning a cron job to a specific instance so all instances in the cluster aren't running it. It would probably be simplest to implement if one instance took responsibility for all the cron jobs and the other instances knew not to run any cron jobs themselves. – jfriend00 Jun 24 '22 at 07:49
  • 1
    Our project has cronjobs which is supposed to run on only one Cluster. Inside yaml file use "concurrencyPolicy: Forbid" property. This will allow it to only run on one cluster at once – Utkarsh Raj Jun 24 '22 at 08:10

0 Answers0