pm2 how to avoid schedule cron lock in nestjs

Viewed 26

I use pm2 to run the service. I have a total of 2 clusters.

And I created a schedule job in nestjs and executed the schedule, but both clusters ran schedule and the database was locked.

how can i avoid this?

below is my ecosystem.js

module.exports = {
  apps: [
    {

      name: 'my_app',
      script: 'dist/main.js',
      instances: 0,
      exec_mode: 'cluster',
      listen_timeout: 10000,
      kill_timeout: 1000,
    },
  ],
};

1 Answers

i can use process.env.pm_id

  1. pm2 list
  2. check my pm_id list
@Cron('* * * * ... ')
myCron(){
if(process.env.pm_id === '0') {
 ...
}
}

Related