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

v1.18.x
Julian Lam 11 years ago
parent 08abbe19bc
commit e5a35ce378

@ -28,6 +28,7 @@ var fs = require('fs'),
semver = require('semver'), semver = require('semver'),
winston = require('winston'), winston = require('winston'),
path = require('path'), path = require('path'),
cluster = require('cluster'),
pkg = require('./package.json'), pkg = require('./package.json'),
utils = require('./public/src/utils.js'); utils = require('./public/src/utils.js');
@ -158,10 +159,15 @@ function start() {
process.on('SIGINT', shutdown); process.on('SIGINT', shutdown);
process.on('SIGHUP', restart); process.on('SIGHUP', restart);
process.on('message', function(message) { process.on('message', function(message) {
switch(message) { switch(message.action) {
case 'reload': case 'reload':
meta.reload(); meta.reload();
break; 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) { process.on('uncaughtException', function(err) {

@ -131,6 +131,18 @@ Loader.init = function() {
console.log('[cluster] Reloading...'); console.log('[cluster] Reloading...');
Loader.reload(); Loader.reload();
break; 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() { Loader.reload = function() {
Object.keys(cluster.workers).forEach(function(worker_id) { Object.keys(cluster.workers).forEach(function(worker_id) {
cluster.workers[worker_id].send('reload'); cluster.workers[worker_id].send({
action: 'reload'
});
}); });
}; };

@ -7,6 +7,7 @@ var winston = require('winston'),
_ = require('underscore'), _ = require('underscore'),
os = require('os'), os = require('os'),
nconf = require('nconf'), nconf = require('nconf'),
cluster = require('cluster'),
plugins = require('../plugins'), plugins = require('../plugins'),
emitter = require('../emitter'), emitter = require('../emitter'),
@ -120,6 +121,7 @@ module.exports = function(Meta) {
}; };
Meta.js.minify = function(minify, callback) { Meta.js.minify = function(minify, callback) {
if (!cluster.isWorker || process.env.cluster_setup === 'true') {
var minifier = Meta.js.minifierProc = fork('minifier.js', { var minifier = Meta.js.minifierProc = fork('minifier.js', {
silent: true silent: true
}), }),
@ -138,6 +140,15 @@ module.exports = function(Meta) {
winston.info('[meta/js] Compilation complete'); winston.info('[meta/js] Compilation complete');
emitter.emit('meta:js.compiled'); emitter.emit('meta:js.compiled');
minifier.kill(); minifier.kill();
if (cluster.isWorker) {
process.send({
action: 'js-propagate',
cache: minifiedString,
map: mapString
});
}
if (typeof callback === 'function') { if (typeof callback === 'function') {
callback(); callback();
} }
@ -184,6 +195,11 @@ module.exports = function(Meta) {
scripts: Meta.js.scripts.all scripts: Meta.js.scripts.all
}); });
}); });
} else {
if (typeof callback === 'function') {
callback();
}
}
}; };
Meta.js.killMinifier = function(callback) { Meta.js.killMinifier = function(callback) {

Loading…
Cancel
Save