How to run Cron Job in Node.js application that uses cluster module?

后端 未结 5 1174
隐瞒了意图╮
隐瞒了意图╮ 2021-02-01 04:02

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

5条回答
  •  不要未来只要你来
    2021-02-01 04:42

    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.

提交回复
热议问题