From 4f0e93732a47c44d27ec294ec4d36a9caa4778eb Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 17 Nov 2016 12:56:00 +0300 Subject: [PATCH] #5211 remove passing js/css between procs --- app.js | 14 ---------- loader.js | 65 +++---------------------------------------- src/meta/css.js | 43 +++------------------------- src/meta/js.js | 47 ++++++------------------------- src/meta/templates.js | 17 +++-------- src/socket.io/meta.js | 16 +++++------ src/socket.io/user.js | 2 +- src/webserver.js | 22 ++++----------- 8 files changed, 34 insertions(+), 192 deletions(-) diff --git a/app.js b/app.js index 466e81745b..1832095052 100644 --- a/app.js +++ b/app.js @@ -164,20 +164,6 @@ function start() { case 'reload': meta.reload(); break; - case 'js-propagate': - meta.js.target = message.data; - emitter.emit('meta:js.compiled'); - winston.verbose('[cluster] Client-side javascript and mapping propagated to worker %s', process.pid); - break; - case 'css-propagate': - meta.css.cache = message.cache; - meta.css.acpCache = message.acpCache; - emitter.emit('meta:css.compiled'); - winston.verbose('[cluster] Stylesheets propagated to worker %s', process.pid); - break; - case 'templates:compiled': - emitter.emit('templates:compiled'); - break; } }); diff --git a/loader.js b/loader.js index 6b388affcd..df528871e9 100644 --- a/loader.js +++ b/loader.js @@ -22,15 +22,7 @@ var pidFilePath = __dirname + '/pidfile', workers = [], Loader = { - timesStarted: 0, - js: { - target: {} - }, - css: { - cache: undefined, - acpCache: undefined - }, - templatesCompiled: false + timesStarted: 0 }; Loader.init = function (callback) { @@ -79,37 +71,13 @@ Loader.addWorkerEvents = function (worker) { if (!(worker.suicide || code === 0)) { console.log('[cluster] Spinning up another process...'); - forkWorker(worker.index, worker.isPrimary, true); + forkWorker(worker.index, worker.isPrimary); } }); worker.on('message', function (message) { if (message && typeof message === 'object' && message.action) { switch (message.action) { - case 'ready': - if (Loader.js.target['nodebb.min.js'] && Loader.js.target['acp.min.js'] && !worker.isPrimary) { - worker.send({ - action: 'js-propagate', - data: Loader.js.target - }); - } - - if (Loader.css.cache && !worker.isPrimary) { - worker.send({ - action: 'css-propagate', - cache: Loader.css.cache, - acpCache: Loader.css.acpCache - }); - } - - if (Loader.templatesCompiled && !worker.isPrimary) { - worker.send({ - action: 'templates:compiled' - }); - } - - - break; case 'restart': console.log('[cluster] Restarting...'); Loader.restart(); @@ -118,31 +86,6 @@ Loader.addWorkerEvents = function (worker) { console.log('[cluster] Reloading...'); Loader.reload(); break; - case 'js-propagate': - Loader.js.target = message.data; - - Loader.notifyWorkers({ - action: 'js-propagate', - data: message.data - }, worker.pid); - break; - case 'css-propagate': - Loader.css.cache = message.cache; - Loader.css.acpCache = message.acpCache; - - Loader.notifyWorkers({ - action: 'css-propagate', - cache: message.cache, - acpCache: message.acpCache - }, worker.pid); - break; - case 'templates:compiled': - Loader.templatesCompiled = true; - - Loader.notifyWorkers({ - action: 'templates:compiled', - }, worker.pid); - break; } } }); @@ -153,7 +96,7 @@ Loader.start = function (callback) { console.log('Clustering enabled: Spinning up ' + numProcs + ' process(es).\n'); for (var x = 0; x < numProcs; ++x) { - forkWorker(x, x === 0, false); + forkWorker(x, x === 0); } if (callback) { @@ -161,7 +104,7 @@ Loader.start = function (callback) { } }; -function forkWorker(index, isPrimary, isRestart) { +function forkWorker(index, isPrimary) { var ports = getPorts(); var args = []; diff --git a/src/meta/css.js b/src/meta/css.js index d664fdd5d2..3336fc0a0e 100644 --- a/src/meta/css.js +++ b/src/meta/css.js @@ -11,7 +11,6 @@ var postcss = require('postcss'); var clean = require('postcss-clean'); var plugins = require('../plugins'); -var emitter = require('../emitter'); var db = require('../database'); var file = require('../file'); var utils = require('../../public/src/utils'); @@ -24,10 +23,6 @@ module.exports = function (Meta) { Meta.css.minify = function (callback) { callback = callback || function () {}; - if (nconf.get('isPrimary') !== 'true') { - winston.verbose('[meta/css] Cluster worker ' + process.pid + ' skipping LESS/CSS compilation'); - return callback(); - } winston.verbose('[meta/css] Minifying LESS/CSS'); db.getObjectFields('config', ['theme:type', 'theme:id'], function (err, themeData) { @@ -84,23 +79,8 @@ module.exports = function (Meta) { async.series([ async.apply(minify, source, paths, 'cache'), async.apply(minify, acpSource, paths, 'acpCache') - ], function (err, minified) { - if (err) { - return callback(err); - } - - // Propagate to other workers - if (process.send) { - process.send({ - action: 'css-propagate', - cache: minified[0], - acpCache: minified[1] - }); - } - - emitter.emit('meta:css.compiled'); - - callback(); + ], function (err) { + callback(err); }); }); }); @@ -110,23 +90,8 @@ module.exports = function (Meta) { async.series([ async.apply(Meta.css.loadFile, path.join(__dirname, '../../public/stylesheet.css'), 'cache'), async.apply(Meta.css.loadFile, path.join(__dirname, '../../public/admin.css'), 'acpCache') - ], function (err, minified) { - if (err) { - return callback(err); - } - - // Propagate to other workers - if (process.send) { - process.send({ - action: 'css-propagate', - cache: Meta.css.cache, - acpCache: Meta.css.acpCache - }); - } - - emitter.emit('meta:css.compiled'); - - callback(); + ], function (err) { + callback(err); }); }; diff --git a/src/meta/js.js b/src/meta/js.js index cfb588125d..a68d7a53de 100644 --- a/src/meta/js.js +++ b/src/meta/js.js @@ -8,7 +8,6 @@ var nconf = require('nconf'); var fs = require('fs'); var file = require('../file'); var plugins = require('../plugins'); -var emitter = require('../emitter'); var utils = require('../../public/src/utils'); module.exports = function (Meta) { @@ -122,14 +121,6 @@ module.exports = function (Meta) { }; Meta.js.minify = function (target, callback) { - if (nconf.get('isPrimary') !== 'true') { - if (typeof callback === 'function') { - callback(); - } - - return; - } - winston.verbose('[meta/js] Minifying ' + target); var forkProcessParams = setupDebugging(); @@ -137,7 +128,10 @@ module.exports = function (Meta) { Meta.js.target[target] = {}; - Meta.js.prepare(target, function () { + Meta.js.prepare(target, function (err) { + if (err) { + return callback(err); + } minifier.send({ action: 'js', minify: global.env !== 'development', @@ -153,24 +147,10 @@ module.exports = function (Meta) { winston.verbose('[meta/js] ' + target + ' minification complete'); minifier.kill(); - if (process.send && Meta.js.target['nodebb.min.js'] && Meta.js.target['acp.min.js']) { - process.send({ - action: 'js-propagate', - data: Meta.js.target - }); - } - if (nconf.get('local-assets') === undefined || nconf.get('local-assets') !== false) { - return Meta.js.commitToFile(target, function () { - if (typeof callback === 'function') { - callback(); - } - }); + return Meta.js.commitToFile(target, callback); } else { - emitter.emit('meta:js.compiled'); - if (typeof callback === 'function') { - return callback(); - } + return callback(); } break; @@ -178,11 +158,7 @@ module.exports = function (Meta) { winston.error('[meta/js] Could not compile ' + target + ': ' + message.message); minifier.kill(); - if (typeof callback === 'function') { - callback(new Error(message.message)); - } else { - process.exit(0); - } + callback(new Error(message.message)); break; } }); @@ -236,13 +212,7 @@ module.exports = function (Meta) { Meta.js.commitToFile = function (target, callback) { fs.writeFile(path.join(__dirname, '../../public/' + target), Meta.js.target[target].cache, function (err) { - if (err) { - winston.error('[meta/js] ' + err.message); - process.exit(0); - } - - emitter.emit('meta:js.compiled'); - callback(); + callback(err); }); }; @@ -277,7 +247,6 @@ module.exports = function (Meta) { map: files[1] || '' }; - emitter.emit('meta:js.compiled'); callback(); }); }); diff --git a/src/meta/templates.js b/src/meta/templates.js index 6d8f579034..4e7f934624 100644 --- a/src/meta/templates.js +++ b/src/meta/templates.js @@ -8,7 +8,6 @@ var path = require('path'); var fs = require('fs'); var nconf = require('nconf'); -var emitter = require('../emitter'); var plugins = require('../plugins'); var utils = require('../../public/src/utils'); @@ -18,11 +17,6 @@ var searchIndex = {}; Templates.compile = function (callback) { callback = callback || function () {}; - if (nconf.get('isPrimary') === 'false') { - emitter.emit('templates:compiled'); - return callback(); - } - compile(callback); }; @@ -149,15 +143,12 @@ function compile(callback) { return callback(err); } - compileIndex(viewsPath, function () { + compileIndex(viewsPath, function (err) { + if (err) { + return callback(err); + } winston.verbose('[meta/templates] Successfully compiled templates.'); - emitter.emit('templates:compiled'); - if (process.send) { - process.send({ - action: 'templates:compiled' - }); - } callback(); }); }); diff --git a/src/socket.io/meta.js b/src/socket.io/meta.js index 13fe018654..35a5da17e1 100644 --- a/src/socket.io/meta.js +++ b/src/socket.io/meta.js @@ -1,15 +1,15 @@ 'use strict'; -var meta = require('../meta'), - user = require('../user'), - topics = require('../topics'), - emitter = require('../emitter'), +var meta = require('../meta'); +var user = require('../user'); +var topics = require('../topics'); +var emitter = require('../emitter'); - websockets = require('./'), +var websockets = require('./'); - SocketMeta = { - rooms: {} - }; +var SocketMeta = { + rooms: {} +}; SocketMeta.reconnected = function (socket, data, callback) { if (socket.uid) { diff --git a/src/socket.io/user.js b/src/socket.io/user.js index 1c322fe1e4..41c2e9c2c5 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -1,6 +1,6 @@ 'use strict'; -var async = require('async'); +var async = require('async'); var winston = require('winston'); var user = require('../user'); diff --git a/src/webserver.js b/src/webserver.js index 37e242e889..964ae60bfd 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -62,24 +62,16 @@ module.exports.listen = function (callback) { logger.init(app); - emitter.all(['templates:compiled', 'meta:js.compiled', 'meta:css.compiled'], function () { + initializeNodeBB(function (err) { + if (err) { + return callback(err); + } + winston.info('NodeBB Ready'); emitter.emit('nodebb:ready'); listen(callback); }); - - initializeNodeBB(function (err) { - if (err) { - winston.error(err); - process.exit(); - } - if (process.send) { - process.send({ - action: 'ready' - }); - } - }); }; function initializeNodeBB(callback) { @@ -108,10 +100,6 @@ function initializeNodeBB(callback) { }, function (next) { async.series([ - async.apply(function(next) { - emitter.emit('templates:compiled'); - setImmediate(next); - }), async.apply(meta.js.getFromFile, 'nodebb.min.js'), async.apply(meta.js.getFromFile, 'acp.min.js'), async.apply(meta.css.getFromFile),