From 51b14579b767935171c7f571c5d9b8f32ac08543 Mon Sep 17 00:00:00 2001 From: Amoki Date: Thu, 9 Oct 2014 12:42:58 +0200 Subject: [PATCH 1/2] one child for multiple task --- lib/helpers/child-process.js | 6 +++--- lib/helpers/hydrater.js | 30 +++++++++++++++++++++++++++++- test/cleaning.js | 6 +++--- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/lib/helpers/child-process.js b/lib/helpers/child-process.js index f128199..099fd32 100644 --- a/lib/helpers/child-process.js +++ b/lib/helpers/child-process.js @@ -16,10 +16,11 @@ var url = require('url'); var rarity = require('rarity'); var file_path; -var path = '/tmp/AFH-' + crypto.randomBytes(20).toString('hex'); +var path; process.on('message', function(task) { file_path = task.file_path; + path = '/tmp/AFH-' + crypto.randomBytes(20).toString('hex'); var hydrate = require(task.functionPath); async.waterfall([ /** @@ -73,8 +74,6 @@ process.on('message', function(task) { err: err, changes: changes }); - process.disconnect(); - process.exit(0); }); }); @@ -87,4 +86,5 @@ process.on('SIGTERM', function() { catch (err) {} } process.disconnect(); + process.exit(0); }); diff --git a/lib/helpers/hydrater.js b/lib/helpers/hydrater.js index 6abbcc4..968daf0 100644 --- a/lib/helpers/hydrater.js +++ b/lib/helpers/hydrater.js @@ -19,6 +19,29 @@ var lib = require('../index.js'); var HydrationError = lib.HydrationError; +var tasksPerProcess = process.env.TASKS_PER_PROCESS || 100; +var taskCount = 0; +var currentChild; + +var getOrForkChild = function() { + taskCount += 1; + //console.log("currentChild", currentChild) + if (taskCount < tasksPerProcess && currentChild) { + currentChild.removeAllListeners(); + currentChild.stdout.removeAllListeners(); + currentChild.stderr.removeAllListeners(); + return currentChild; + } + else { + taskCount = 0; + if(currentChild) { + currentChild.kill('SIGKILL'); + } + currentChild = fork(__dirname + '/child-process.js', {silent: true}); + return currentChild; + } +}; + module.exports = function(hydraterFunction, logger, errLogger) { if(!errLogger) { errLogger = logger; @@ -39,7 +62,7 @@ module.exports = function(hydraterFunction, logger, errLogger) { async.waterfall([ function performHydration(cb) { - var child = fork(__dirname + '/child-process.js', {silent: true}); + var child = getOrForkChild(); var stderr = ""; var stdout = ""; var timeout; @@ -50,6 +73,10 @@ module.exports = function(hydraterFunction, logger, errLogger) { var cleaner = function(err, changes) { if(!cleaner.called) { cleaner.called = true; + if(err) { + currentChild.kill('SIGKILL'); + currentChild = null; + } cb(err, changes); } if(stdout !== "") { @@ -132,6 +159,7 @@ module.exports = function(hydraterFunction, logger, errLogger) { if(child.connected) { child.kill('SIGKILL'); } + currentChild = null; cleaner(null, changes); }, process.env.TIMEOUT / 6 || 10 * 1000); } diff --git a/test/cleaning.js b/test/cleaning.js index 6530ea7..1a7949a 100644 --- a/test/cleaning.js +++ b/test/cleaning.js @@ -57,9 +57,9 @@ describe('Hydration should be cleaned every time', function() { hydrate(task, function(err, changes) { // + one process when working changes.metadata.nodeCount.should.eql(nodeProccessesAtStart + 1); - // +0 process after work + // + one process after work if it didn't crash shellExec('ps aux | grep "[n]ode" -c', function(err, stdout) { - parseInt(stdout).should.be.eql(nodeProccessesAtStart); + parseInt(stdout).should.be.eql(nodeProccessesAtStart + 1); cb(err); }); }); @@ -79,7 +79,7 @@ describe('Hydration should be cleaned every time', function() { shellExec('ps aux | grep "[n]ode" -c', cb); }, function setNodeProcessesNumber(stdout, stderr, cb) { - nodeProccessesAtStart = parseInt(stdout); + nodeProccessesAtStart = parseInt(stdout) - 1; // Cause we still have previous child cb(); }, function hydrateManyTimes(cb) { From 9eb0673f333e9518cc37cb6c4c328c4b369b7d68 Mon Sep 17 00:00:00 2001 From: Amoki Date: Thu, 9 Oct 2014 12:47:23 +0200 Subject: [PATCH 2/2] remove console.log --- lib/helpers/hydrater.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/helpers/hydrater.js b/lib/helpers/hydrater.js index 968daf0..b0ea519 100644 --- a/lib/helpers/hydrater.js +++ b/lib/helpers/hydrater.js @@ -25,7 +25,6 @@ var currentChild; var getOrForkChild = function() { taskCount += 1; - //console.log("currentChild", currentChild) if (taskCount < tasksPerProcess && currentChild) { currentChild.removeAllListeners(); currentChild.stdout.removeAllListeners();