Skip to content

Node.js based forum software built for the modern web

js propagation for cluster module, so only 1 thread minifies the js

Julian Lame5a35ce37871
diff --git a/app.js b/app.js
index f6c0313abe..52e3e000db 100644
--- a/app.js
+++ b/app.js
@@ -28,6 +28,7 @@ var fs = require('fs'),
semver = require('semver'),
winston = require('winston'),
path = require('path'),
+ cluster = require('cluster'),
pkg = require('./package.json'),
utils = require('./public/src/utils.js');
@@ -158,10 +159,15 @@ function start() {
process.on('SIGINT', shutdown);
process.on('SIGHUP', restart);
process.on('message', function(message) {
- switch(message) {
+ switch(message.action) {
case 'reload':
meta.reload();
break;
+ case 'js-propagate':
+ meta.js.cache = message.cache;
+ meta.js.map = message.map;
+ winston.info('[cluster] Client-side javascript and mapping propagated to worker ' + cluster.worker.id);
+ break;
}
})
process.on('uncaughtException', function(err) {
diff --git a/loader.js b/loader.js
index 65d8381c3e..a5b90084f8 100644
--- a/loader.js
+++ b/loader.js
@@ -131,6 +131,18 @@ Loader.init = function() {
console.log('[cluster] Reloading...');
Loader.reload();
break;
+ case 'js-propagate':
+ var otherWorkers = Object.keys(cluster.workers).filter(function(worker_id) {
+ return parseInt(worker_id, 10) !== parseInt(worker.id, 10);
+ });
+ otherWorkers.forEach(function(worker_id) {
+ cluster.workers[worker_id].send({
+ action: 'js-propagate',
+ cache: message.cache,
+ map: message.map
+ });
+ });
+ break;
}
}
});
@@ -180,7 +192,9 @@ Loader.restart = function(callback) {
Loader.reload = function() {
Object.keys(cluster.workers).forEach(function(worker_id) {
- cluster.workers[worker_id].send('reload');
+ cluster.workers[worker_id].send({
+ action: 'reload'
+ });
});
};
diff --git a/src/meta/js.js b/src/meta/js.js
index f0950bcbf8..c51b5ec402 100644
--- a/src/meta/js.js
+++ b/src/meta/js.js
@@ -7,6 +7,7 @@ var winston = require('winston'),
_ = require('underscore'),
os = require('os'),
nconf = require('nconf'),
+ cluster = require('cluster'),
plugins = require('../plugins'),
emitter = require('../emitter'),
@@ -120,70 +121,85 @@ module.exports = function(Meta) {
};
Meta.js.minify = function(minify, callback) {
- var minifier = Meta.js.minifierProc = fork('minifier.js', {
- silent: true
- }),
- minifiedStream = minifier.stdio[1],
- minifiedString = '',
- mapStream = minifier.stdio[2],
- mapString = '',
- step = 0,
- onComplete = function() {
- if (step === 0) {
- return step++;
- }
+ if (!cluster.isWorker || process.env.cluster_setup === 'true') {
+ var minifier = Meta.js.minifierProc = fork('minifier.js', {
+ silent: true
+ }),
+ minifiedStream = minifier.stdio[1],
+ minifiedString = '',
+ mapStream = minifier.stdio[2],
+ mapString = '',
+ step = 0,
+ onComplete = function() {
+ if (step === 0) {
+ return step++;
+ }
- Meta.js.cache = minifiedString;
- Meta.js.map = mapString;
- winston.info('[meta/js] Compilation complete');
- emitter.emit('meta:js.compiled');
- minifier.kill();
- if (typeof callback === 'function') {
- callback();
- }
- };
+ Meta.js.cache = minifiedString;
+ Meta.js.map = mapString;
+ winston.info('[meta/js] Compilation complete');
+ emitter.emit('meta:js.compiled');
+ minifier.kill();
+
+ if (cluster.isWorker) {
+ process.send({
+ action: 'js-propagate',
+ cache: minifiedString,
+ map: mapString
+ });
+ }
- minifiedStream.on('data', function(buffer) {
- minifiedString += buffer.toString();
- });
- mapStream.on('data', function(buffer) {
- mapString += buffer.toString();
- });
+ if (typeof callback === 'function') {
+ callback();
+ }
+ };
- minifier.on('message', function(message) {
- switch(message.type) {
- case 'end':
- if (message.payload === 'script') {
- winston.info('[meta/js] Successfully minified.');
- onComplete();
- } else if (message.payload === 'mapping') {
- winston.info('[meta/js] Retrieved Mapping.');
- onComplete();
- }
- break;
- case 'hash':
- Meta.js.hash = message.payload;
- break;
- case 'error':
- winston.error('[meta/js] Could not compile client-side scripts! ' + message.payload.message);
- minifier.kill();
- if (typeof callback === 'function') {
- callback(new Error(message.payload.message));
- } else {
- process.exit(0);
+ minifiedStream.on('data', function(buffer) {
+ minifiedString += buffer.toString();
+ });
+ mapStream.on('data', function(buffer) {
+ mapString += buffer.toString();
+ });
+
+ minifier.on('message', function(message) {
+ switch(message.type) {
+ case 'end':
+ if (message.payload === 'script') {
+ winston.info('[meta/js] Successfully minified.');
+ onComplete();
+ } else if (message.payload === 'mapping') {
+ winston.info('[meta/js] Retrieved Mapping.');
+ onComplete();
+ }
+ break;
+ case 'hash':
+ Meta.js.hash = message.payload;
+ break;
+ case 'error':
+ winston.error('[meta/js] Could not compile client-side scripts! ' + message.payload.message);
+ minifier.kill();
+ if (typeof callback === 'function') {
+ callback(new Error(message.payload.message));
+ } else {
+ process.exit(0);
+ }
+ break;
}
- break;
- }
- });
+ });
- Meta.js.prepare(function() {
- minifier.send({
- action: 'js',
- relativePath: nconf.get('url') + '/',
- minify: minify,
- scripts: Meta.js.scripts.all
+ Meta.js.prepare(function() {
+ minifier.send({
+ action: 'js',
+ relativePath: nconf.get('url') + '/',
+ minify: minify,
+ scripts: Meta.js.scripts.all
+ });
});
- });
+ } else {
+ if (typeof callback === 'function') {
+ callback();
+ }
+ }
};
Meta.js.killMinifier = function(callback) {