PM2 cluster mode - Jobs are duplicated
See original GitHub issueHi,
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:
- Created 5 years ago
- Reactions:3
- Comments:9 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
app
is my nodeApplication (express) )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)
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 commandpm2 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.