diff --git a/src/meta/build.js b/src/meta/build.js index e88bbb17e9..cc3321923f 100644 --- a/src/meta/build.js +++ b/src/meta/build.js @@ -2,7 +2,6 @@ var async = require('async'); var winston = require('winston'); -var os = require('os'); var nconf = require('nconf'); var padstart = require('lodash.padstart'); @@ -181,7 +180,7 @@ function build(targets, callback) { async.series([ beforeBuild, function (next) { - var parallel = os.cpus().length > 1 && !nconf.get('series'); + var parallel = !nconf.get('series'); if (parallel) { winston.info('[build] Building in parallel mode'); } else { diff --git a/src/meta/minifier.js b/src/meta/minifier.js index b1f2888b16..53ca9f5f28 100644 --- a/src/meta/minifier.js +++ b/src/meta/minifier.js @@ -5,6 +5,7 @@ var async = require('async'); var fs = require('fs'); var childProcess = require('child_process'); var os = require('os'); +var winston = require('winston'); var less = require('less'); var postcss = require('postcss'); var autoprefixer = require('autoprefixer'); @@ -39,6 +40,10 @@ function setupDebugging() { var children = []; +Minifier.maxThreads = os.cpus().length - 1; + +winston.verbose('[minifier] utilizing a maximum of ' + Minifier.maxThreads + ' additional threads'); + Minifier.killAll = function () { children.forEach(function (child) { child.kill('SIGTERM'); @@ -65,13 +70,14 @@ function forkAction(action, callback) { children.push(proc); proc.on('message', function (message) { + proc.kill(); + removeChild(proc); + if (message.type === 'error') { - proc.kill(); - return callback(new Error(message.message)); + return callback(message.err); } if (message.type === 'end') { - proc.kill(); callback(null, message.result); } }); @@ -85,10 +91,6 @@ function forkAction(action, callback) { type: 'action', action: action, }); - - proc.on('close', function () { - removeChild(proc); - }); } var actions = {}; @@ -109,7 +111,7 @@ if (process.env.minifier_child) { if (err) { process.send({ type: 'error', - message: err.message, + err: err, }); return; } @@ -124,7 +126,7 @@ if (process.env.minifier_child) { } function executeAction(action, fork, callback) { - if (fork) { + if (fork && children.length < Minifier.maxThreads) { forkAction(action, callback); } else { if (typeof actions[action.act] !== 'function') {