瀏覽代碼

run cron only on the first instance/cluster of the app

Pouria Ezzati 1 年之前
父節點
當前提交
c8a49d83fd
共有 1 個文件被更改,包括 9 次插入1 次删除
  1. 9 1
      server/server.js

+ 9 - 1
server/server.js

@@ -16,7 +16,15 @@ const links = require("./handlers/links.handler");
 const routes = require("./routes");
 const utils = require("./utils");
 
-require("./cron");
+
+// run the cron jobs
+// the app might be running in cluster mode (multiple instances) so run the cron job only on one cluster (the first one)
+// NODE_APP_INSTANCE variable is added by pm2 automatically, if you're using something else to cluster your app, then make sure to set this variable
+if (env.NODE_APP_INSTANCE === 0) {
+  require("./cron");
+}
+
+// intialize passport authentication library
 require("./passport");
 
 // create express app