Forráskód Böngészése

Sails can work in cluster too (#2999)

Sails should work in cluster like any other nodejs framework
Herman Klushin 7 éve
szülő
commit
1db74b2f15
1 módosított fájl, 48 hozzáadás és 29 törlés
  1. 48 29
      frameworks/JavaScript/sailsjs/app.js

+ 48 - 29
frameworks/JavaScript/sailsjs/app.js

@@ -21,39 +21,58 @@
 // Ensure we're in the project directory, so relative paths work as expected
 // no matter where we actually lift from.
 process.chdir(__dirname);
+const cluster = require('cluster'),
+      numCPUs = require('os').cpus().length;
 
-// Ensure a "sails" can be located:
-(function() {
-  var sails;
-  try {
-    sails = require('sails');
-  } catch (e) {
-    console.error('To run an app using `node app.js`, you usually need to have a version of `sails` installed in the same directory as your app.');
-    console.error('To do that, run `npm install sails`');
-    console.error('');
-    console.error('Alternatively, if you have sails installed globally (i.e. you did `npm install -g sails`), you can use `sails lift`.');
-    console.error('When you run `sails lift`, your app will still use a local `./node_modules/sails` dependency if it exists,');
-    console.error('but if it doesn\'t, the app will run with the global sails instead!');
-    return;
+if (cluster.isMaster) {
+  // Fork workers.
+  for (let i = 0; i < numCPUs; i++) {
+    cluster.fork();
   }
 
-  // Try to get `rc` dependency
-  var rc;
-  try {
-    rc = require('rc');
-  } catch (e0) {
+  cluster.on('exit', (worker, code, signal) => {
+    if (signal) {
+      console.log(`worker ${worker.process.pid} was killed by signal: ${signal}`);
+    } else if (code !== 0) {
+      console.log(`worker ${worker.process.pid} exited with error code: ${code}`);
+    } else {
+      console.log(`worker ${worker.process.pid} success!`);
+    }
+  });
+} else {
+  // Ensure a "sails" can be located:
+  (function() {
+    var sails;
     try {
-      rc = require('sails/node_modules/rc');
-    } catch (e1) {
-      console.error('Could not find dependency: `rc`.');
-      console.error('Your `.sailsrc` file(s) will be ignored.');
-      console.error('To resolve this, run:');
-      console.error('npm install rc --save');
-      rc = function () { return {}; };
+      sails = require('sails');
+    } catch (e) {
+      console.error('To run an app using `node app.js`, you usually need to have a version of `sails` installed in the same directory as your app.');
+      console.error('To do that, run `npm install sails`');
+      console.error('');
+      console.error('Alternatively, if you have sails installed globally (i.e. you did `npm install -g sails`), you can use `sails lift`.');
+      console.error('When you run `sails lift`, your app will still use a local `./node_modules/sails` dependency if it exists,');
+      console.error('but if it doesn\'t, the app will run with the global sails instead!');
+      return;
+    }
+
+    // Try to get `rc` dependency
+    var rc;
+    try {
+      rc = require('rc');
+    } catch (e0) {
+      try {
+        rc = require('sails/node_modules/rc');
+      } catch (e1) {
+        console.error('Could not find dependency: `rc`.');
+        console.error('Your `.sailsrc` file(s) will be ignored.');
+        console.error('To resolve this, run:');
+        console.error('npm install rc --save');
+        rc = function () { return {}; };
+      }
     }
-  }
 
 
-  // Start server
-  sails.lift(rc('sails'));
-})();
+    // Start server
+    sails.lift(rc('sails'));
+  })();
+}