I\'m using node-cron module for scheduling tasks in Node.js application. I also want run the application in several processes using core cluster module.
Running applica
If are using PM2,
You can use an environment variable provided by PM2 itself called NODE_APP_INSTANCE
which requires PM2 2.5 or greater.
NODE_APP_INSTANCE
environment variable can be used to determine difference between process, for example you may want to run a cronjob only on one process, you can just do this
if(process.env.NODE_APP_INSTANCE == 0) {
//schedule your cron job here since this part will be executed for only one cluster
}
,
Since two processes can never have the same number.
More Info on PM2 official doc here.