Skip to content

Commit 51b1457

Browse files
author
Amoki
committed
one child for multiple task
1 parent 6fce3f1 commit 51b1457

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

lib/helpers/child-process.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ var url = require('url');
1616
var rarity = require('rarity');
1717

1818
var file_path;
19-
var path = '/tmp/AFH-' + crypto.randomBytes(20).toString('hex');
19+
var path;
2020

2121
process.on('message', function(task) {
2222
file_path = task.file_path;
23+
path = '/tmp/AFH-' + crypto.randomBytes(20).toString('hex');
2324
var hydrate = require(task.functionPath);
2425
async.waterfall([
2526
/**
@@ -73,8 +74,6 @@ process.on('message', function(task) {
7374
err: err,
7475
changes: changes
7576
});
76-
process.disconnect();
77-
process.exit(0);
7877
});
7978
});
8079

@@ -87,4 +86,5 @@ process.on('SIGTERM', function() {
8786
catch (err) {}
8887
}
8988
process.disconnect();
89+
process.exit(0);
9090
});

lib/helpers/hydrater.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,29 @@ var lib = require('../index.js');
1919

2020
var HydrationError = lib.HydrationError;
2121

22+
var tasksPerProcess = process.env.TASKS_PER_PROCESS || 100;
23+
var taskCount = 0;
24+
var currentChild;
25+
26+
var getOrForkChild = function() {
27+
taskCount += 1;
28+
//console.log("currentChild", currentChild)
29+
if (taskCount < tasksPerProcess && currentChild) {
30+
currentChild.removeAllListeners();
31+
currentChild.stdout.removeAllListeners();
32+
currentChild.stderr.removeAllListeners();
33+
return currentChild;
34+
}
35+
else {
36+
taskCount = 0;
37+
if(currentChild) {
38+
currentChild.kill('SIGKILL');
39+
}
40+
currentChild = fork(__dirname + '/child-process.js', {silent: true});
41+
return currentChild;
42+
}
43+
};
44+
2245
module.exports = function(hydraterFunction, logger, errLogger) {
2346
if(!errLogger) {
2447
errLogger = logger;
@@ -39,7 +62,7 @@ module.exports = function(hydraterFunction, logger, errLogger) {
3962

4063
async.waterfall([
4164
function performHydration(cb) {
42-
var child = fork(__dirname + '/child-process.js', {silent: true});
65+
var child = getOrForkChild();
4366
var stderr = "";
4467
var stdout = "";
4568
var timeout;
@@ -50,6 +73,10 @@ module.exports = function(hydraterFunction, logger, errLogger) {
5073
var cleaner = function(err, changes) {
5174
if(!cleaner.called) {
5275
cleaner.called = true;
76+
if(err) {
77+
currentChild.kill('SIGKILL');
78+
currentChild = null;
79+
}
5380
cb(err, changes);
5481
}
5582
if(stdout !== "") {
@@ -132,6 +159,7 @@ module.exports = function(hydraterFunction, logger, errLogger) {
132159
if(child.connected) {
133160
child.kill('SIGKILL');
134161
}
162+
currentChild = null;
135163
cleaner(null, changes);
136164
}, process.env.TIMEOUT / 6 || 10 * 1000);
137165
}

test/cleaning.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ describe('Hydration should be cleaned every time', function() {
5757
hydrate(task, function(err, changes) {
5858
// + one process when working
5959
changes.metadata.nodeCount.should.eql(nodeProccessesAtStart + 1);
60-
// +0 process after work
60+
// + one process after work if it didn't crash
6161
shellExec('ps aux | grep "[n]ode" -c', function(err, stdout) {
62-
parseInt(stdout).should.be.eql(nodeProccessesAtStart);
62+
parseInt(stdout).should.be.eql(nodeProccessesAtStart + 1);
6363
cb(err);
6464
});
6565
});
@@ -79,7 +79,7 @@ describe('Hydration should be cleaned every time', function() {
7979
shellExec('ps aux | grep "[n]ode" -c', cb);
8080
},
8181
function setNodeProcessesNumber(stdout, stderr, cb) {
82-
nodeProccessesAtStart = parseInt(stdout);
82+
nodeProccessesAtStart = parseInt(stdout) - 1; // Cause we still have previous child
8383
cb();
8484
},
8585
function hydrateManyTimes(cb) {

0 commit comments

Comments
 (0)