Skip to content

Commit 8b5e6a4

Browse files
author
Amoki
committed
kill with sigterm before send a sigkill
1 parent 2320bcd commit 8b5e6a4

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

lib/helpers/child-process.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ var request = require('supertest');
1515
var url = require('url');
1616
var rarity = require('rarity');
1717

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

1921
process.on('message', function(task) {
22+
file_path = task.file_path;
2023
var hydrate = require(task.functionPath);
21-
var path = '/tmp/AFH-' + crypto.randomBytes(20).toString('hex');
2224
async.waterfall([
2325
/**
2426
* Download the file from task.file_path, store it in a temporary file if there is file_path
@@ -70,6 +72,16 @@ process.on('message', function(task) {
7072
err: err,
7173
changes: changes
7274
});
75+
process.disconnect();
7376
process.exit(0);
7477
});
7578
});
79+
80+
81+
process.on('SIGTERM', function() {
82+
if(file_path) {
83+
fs.unlinkSync(path);
84+
}
85+
process.disconnect();
86+
process.exit(0);
87+
});

lib/helpers/hydrater.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
var async = require('async');
11-
var shellFork = require('child_process').fork;
11+
var fork = require('child_process').fork;
1212
var request = require('supertest');
1313
var restify = require('restify');
1414
var url = require('url');
@@ -39,7 +39,7 @@ module.exports = function(hydraterFunction, logger, errLogger) {
3939

4040
async.waterfall([
4141
function performHydration(cb) {
42-
var child = shellFork(__dirname + '/child-process.js', {silent: true});
42+
var child = fork(__dirname + '/child-process.js', {silent: true});
4343
var stderr = "";
4444
var stdout = "";
4545
var timeout;
@@ -49,6 +49,9 @@ module.exports = function(hydraterFunction, logger, errLogger) {
4949
*/
5050
var cleaner = function(err, changes) {
5151
if(!cleaner.called) {
52+
if(child.connected) {
53+
child.kill('SIGKILL');
54+
}
5255
cleaner.called = true;
5356
cb(err, changes);
5457
}
@@ -110,7 +113,7 @@ module.exports = function(hydraterFunction, logger, errLogger) {
110113
if(err && err._hydrationError) {
111114
res.changes = {};
112115
res.changes.hydration_errored = true;
113-
res.changes.hydration_error = res.err.message;
116+
res.changes.hydration_error = err.message;
114117
err = null;
115118
}
116119
cleaner(err, res.changes);
@@ -121,8 +124,11 @@ module.exports = function(hydraterFunction, logger, errLogger) {
121124
var changes = {};
122125
changes.hydration_errored = true;
123126
changes.hydration_error = "Task took too long.";
124-
child.kill('SIGKILL');
125-
cleaner(null, changes);
127+
errLogger('Killing task: ' + ((task.file_path) ? task.file_path : task.document.id));
128+
child.kill('SIGTERM');
129+
setTimeout(function() {
130+
cleaner(null, changes);
131+
}, 10 * 1000);
126132
}
127133
}, process.env.TIMEOUT || 60 * 1000);
128134
},

0 commit comments

Comments
 (0)