Restrict total threads

So machines with a small amount of cores build faster
v1.18.x
Peter Jaszkowiak 8 years ago
parent 7ccfb5cdc0
commit e8caee3c4c

@ -2,7 +2,6 @@
var async = require('async'); var async = require('async');
var winston = require('winston'); var winston = require('winston');
var os = require('os');
var nconf = require('nconf'); var nconf = require('nconf');
var padstart = require('lodash.padstart'); var padstart = require('lodash.padstart');
@ -181,7 +180,7 @@ function build(targets, callback) {
async.series([ async.series([
beforeBuild, beforeBuild,
function (next) { function (next) {
var parallel = os.cpus().length > 1 && !nconf.get('series'); var parallel = !nconf.get('series');
if (parallel) { if (parallel) {
winston.info('[build] Building in parallel mode'); winston.info('[build] Building in parallel mode');
} else { } else {

@ -5,6 +5,7 @@ var async = require('async');
var fs = require('fs'); var fs = require('fs');
var childProcess = require('child_process'); var childProcess = require('child_process');
var os = require('os'); var os = require('os');
var winston = require('winston');
var less = require('less'); var less = require('less');
var postcss = require('postcss'); var postcss = require('postcss');
var autoprefixer = require('autoprefixer'); var autoprefixer = require('autoprefixer');
@ -39,6 +40,10 @@ function setupDebugging() {
var children = []; var children = [];
Minifier.maxThreads = os.cpus().length - 1;
winston.verbose('[minifier] utilizing a maximum of ' + Minifier.maxThreads + ' additional threads');
Minifier.killAll = function () { Minifier.killAll = function () {
children.forEach(function (child) { children.forEach(function (child) {
child.kill('SIGTERM'); child.kill('SIGTERM');
@ -65,13 +70,14 @@ function forkAction(action, callback) {
children.push(proc); children.push(proc);
proc.on('message', function (message) { proc.on('message', function (message) {
proc.kill();
removeChild(proc);
if (message.type === 'error') { if (message.type === 'error') {
proc.kill(); return callback(message.err);
return callback(new Error(message.message));
} }
if (message.type === 'end') { if (message.type === 'end') {
proc.kill();
callback(null, message.result); callback(null, message.result);
} }
}); });
@ -85,10 +91,6 @@ function forkAction(action, callback) {
type: 'action', type: 'action',
action: action, action: action,
}); });
proc.on('close', function () {
removeChild(proc);
});
} }
var actions = {}; var actions = {};
@ -109,7 +111,7 @@ if (process.env.minifier_child) {
if (err) { if (err) {
process.send({ process.send({
type: 'error', type: 'error',
message: err.message, err: err,
}); });
return; return;
} }
@ -124,7 +126,7 @@ if (process.env.minifier_child) {
} }
function executeAction(action, fork, callback) { function executeAction(action, fork, callback) {
if (fork) { if (fork && children.length < Minifier.maxThreads) {
forkAction(action, callback); forkAction(action, callback);
} else { } else {
if (typeof actions[action.act] !== 'function') { if (typeof actions[action.act] !== 'function') {

Loading…
Cancel
Save