question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PM2 cluster mode - Jobs are duplicated

See original GitHub issue

Hi,

I run my application in cluster mode with pm2 pm2 start -i 4 myApp.js

I see that the jobs is executed 4 times, normal, but is it a way to prevent the duplication of the jobs ?

Thanks.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

16reactions
Jahnuxcommented, Apr 30, 2019

I’ve pass through this problem in using the instance number of the process environment, without any requirements.

In my case it does the job, look at this :

( the appis my nodeApplication (express) )

        this.app.isMainCluster = parseInt(process.env.NODE_APP_INSTANCE) === 0;

	if (this.app.isMainCluster || this.app.env === 'dev') {
		console.log('IS THE MAINN INSTANCE');
		_.each(this.cronJobs, cronJob => {
			cron.schedule(cronJob.timing, cronJob.job);
		});
	}
2reactions
anjarulrobincommented, Jan 19, 2021

I have faced the similar problem. Then to solve the issue i have configured my pm2.json as (here script is your startup file name)

{
    "apps": [
        {
            "script": "bootstrap.js",
            "instances": "1",
            "exec_mode": "cluster",
            "combine_logs": true,
            "name": "primary",
            "out_file": "./logs/out.log",
            "error_file": "./logs/error.log",
            "env_production": {
                "name": "prod-primary",
                "PORT": 3661,
                "NODE_ENV": "production"
            },
            "env_development": {
                "name": "develop-primary",
                "PORT": 3660,
                "NODE_ENV": "development"
            }
        },
        {
            "script": "bootstrap.js",
            "instances": "-1",
            "exec_mode": "cluster",
            "combine_logs": true,
            "name": "replica",
            "out_file": "./logs/out.log",
            "error_file": "./logs/error.log",
            "env_production": {
                "name": "prod-replica",
                "PORT": 3661,
                "NODE_ENV": "production"
            },
            "env_development": {
                "name": "develop-replica",
                "PORT": 3660,
                "NODE_ENV": "development"
            }
        }
    ]
}

Here i have passed two apps in apps array, and used quite similar configuration except the name of the instance. exec_mode is cluster so that all the instances can share the same port. Now if we run the project using the command pm2 start pm2.json Then you will find process.env.name = “primary” or “replica” in your projects code. Then it’s your choice on which process you will run the cron jobs. You can also run in production mode. to do so you will have to run the command pm2 start pm2.json --env production Then you will get process.env.name = “prod-replica” or “prod-primary”.

Now in code, if you want to check the process name without knowing on which environment the code is running you can name the instances as “something-primary” and “something-replica” for both your development or, then in your code simply use regular expression to be confirmed on which process the cron job should execute.

And you can also scale up your replica instance without any problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node cron job executions duplicates - Stack Overflow
I noticed a pattern that it seems to happen when I reboot the bot several times during the day and the next day...
Read more >
PM2: Run cron-job from single process in cluster mode
So this works fine if we run the code in fork mode (single process). But the problem occurs when we run the project...
Read more >
Cluster Mode - PM2
The cluster mode allows networked Node.js applications (http(s)/tcp/udp server) to be scaled across all CPUs available, without any code modifications.
Read more >
Strapi cron using pm2 cluster mode
I am using pm2 to run strapi in cluster mode. The thing is that I use Strapi cron and I think that its...
Read more >
3 node.js scalability problems and how to solve them
... not using cluster mode, and poorly designed cron jobs, are the most ... scaling of node.js server and duplication of job execution....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found