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,70 +121,85 @@ module.exports = function(Meta) {
}; };
Meta.js.minify = function(minify, callback) { Meta.js.minify = function(minify, callback) {
var minifier = Meta.js.minifierProc = fork('minifier.js', { if (!cluster.isWorker || process.env.cluster_setup === 'true') {
silent: true var minifier = Meta.js.minifierProc = fork('minifier.js', {
}), silent: true
minifiedStream = minifier.stdio[1], }),
minifiedString = '', minifiedStream = minifier.stdio[1],
mapStream = minifier.stdio[2], minifiedString = '',
mapString = '', mapStream = minifier.stdio[2],
step = 0, mapString = '',
onComplete = function() { step = 0,
if (step === 0) { onComplete = function() {
return step++; if (step === 0) {
} return step++;
}
Meta.js.cache = minifiedString; Meta.js.cache = minifiedString;
Meta.js.map = mapString; Meta.js.map = mapString;
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 (typeof callback === 'function') {
callback(); if (cluster.isWorker) {
} process.send({
}; action: 'js-propagate',
cache: minifiedString,
map: mapString
});
}
minifiedStream.on('data', function(buffer) { if (typeof callback === 'function') {
minifiedString += buffer.toString(); callback();
}); }
mapStream.on('data', function(buffer) { };
mapString += buffer.toString();
});
minifier.on('message', function(message) { minifiedStream.on('data', function(buffer) {
switch(message.type) { minifiedString += buffer.toString();
case 'end': });
if (message.payload === 'script') { mapStream.on('data', function(buffer) {
winston.info('[meta/js] Successfully minified.'); mapString += buffer.toString();
onComplete(); });
} else if (message.payload === 'mapping') {
winston.info('[meta/js] Retrieved Mapping.'); minifier.on('message', function(message) {
onComplete(); switch(message.type) {
} case 'end':
break; if (message.payload === 'script') {
case 'hash': winston.info('[meta/js] Successfully minified.');
Meta.js.hash = message.payload; onComplete();
break; } else if (message.payload === 'mapping') {
case 'error': winston.info('[meta/js] Retrieved Mapping.');
winston.error('[meta/js] Could not compile client-side scripts! ' + message.payload.message); onComplete();
minifier.kill(); }
if (typeof callback === 'function') { break;
callback(new Error(message.payload.message)); case 'hash':
} else { Meta.js.hash = message.payload;
process.exit(0); 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() { Meta.js.prepare(function() {
minifier.send({ minifier.send({
action: 'js', action: 'js',
relativePath: nconf.get('url') + '/', relativePath: nconf.get('url') + '/',
minify: minify, minify: minify,
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