Strapi cron using pm2 cluster mode

Sorry for reviving this, but as it ranks quite high on google for “strapi pm2 cluster”, I figured I’ll post a possible solution for running cron jobs in cluster mode.

In my project, I do this:

  const isMainCluster = parseInt(process.env.NODE_APP_INSTANCE, 10) === 0;
  if (isMainCluster || !process.env.NODE_APP_INSTANCE) {
    startCronHere();
  }

When running the project in cluster mode, each instance will have the NODE_APP_INSTANCE so in the code above I only run the cron job when it’s 0. Note that I also run the cron if the NODE_APP_INSTANCE is undefined (this is usually the case when the app is not running in a cluster). Hope this helps someone :v:

2 Likes