Skip to content

Commit a86d745

Browse files
committed
Merge pull request #36 from AnyFetch/one-child-to-rule-many-tasks
one child for multiple task
2 parents 6fce3f1 + 9eb0673 commit a86d745

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-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: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,28 @@ 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+
if (taskCount < tasksPerProcess && currentChild) {
29+
currentChild.removeAllListeners();
30+
currentChild.stdout.removeAllListeners();
31+
currentChild.stderr.removeAllListeners();
32+
return currentChild;
33+
}
34+
else {
35+
taskCount = 0;
36+
if(currentChild) {
37+
currentChild.kill('SIGKILL');
38+
}
39+
currentChild = fork(__dirname + '/child-process.js', {silent: true});
40+
return currentChild;
41+
}
42+
};
43+
2244
module.exports = function(hydraterFunction, logger, errLogger) {
2345
if(!errLogger) {
2446
errLogger = logger;
@@ -39,7 +61,7 @@ module.exports = function(hydraterFunction, logger, errLogger) {
3961

4062
async.waterfall([
4163
function performHydration(cb) {
42-
var child = fork(__dirname + '/child-process.js', {silent: true});
64+
var child = getOrForkChild();
4365
var stderr = "";
4466
var stdout = "";
4567
var timeout;
@@ -50,6 +72,10 @@ module.exports = function(hydraterFunction, logger, errLogger) {
5072
var cleaner = function(err, changes) {
5173
if(!cleaner.called) {
5274
cleaner.called = true;
75+
if(err) {
76+
currentChild.kill('SIGKILL');
77+
currentChild = null;
78+
}
5379
cb(err, changes);
5480
}
5581
if(stdout !== "") {
@@ -132,6 +158,7 @@ module.exports = function(hydraterFunction, logger, errLogger) {
132158
if(child.connected) {
133159
child.kill('SIGKILL');
134160
}
161+
currentChild = null;
135162
cleaner(null, changes);
136163
}, process.env.TIMEOUT / 6 || 10 * 1000);
137164
}

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)