Skip to content

Commit 359cbbc

Browse files
committed
Merge pull request #26 from AnyFetch/better-killing
kill with sigterm before send a sigkill
2 parents 2320bcd + 2ecb3d5 commit 359cbbc

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

lib/helpers/child-process.js

Lines changed: 16 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,19 @@ 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+
try {
84+
fs.unlinkSync(path);
85+
}
86+
catch (err) {}
87+
}
88+
process.disconnect();
89+
process.exit(0);
90+
});

lib/helpers/hydrater.js

Lines changed: 12 additions & 6 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;
@@ -81,7 +81,7 @@ module.exports = function(hydraterFunction, logger, errLogger) {
8181
});
8282

8383
child.on('exit', function(errCode) {
84-
if(errCode !== 0) {
84+
if(errCode !== 0 && errCode !== 143) {
8585
cleaner(new HydrationError("Child exiting with err code: " + errCode + stdout + stderr));
8686
}
8787
});
@@ -110,7 +110,7 @@ module.exports = function(hydraterFunction, logger, errLogger) {
110110
if(err && err._hydrationError) {
111111
res.changes = {};
112112
res.changes.hydration_errored = true;
113-
res.changes.hydration_error = res.err.message;
113+
res.changes.hydration_error = err.message;
114114
err = null;
115115
}
116116
cleaner(err, res.changes);
@@ -121,8 +121,14 @@ module.exports = function(hydraterFunction, logger, errLogger) {
121121
var changes = {};
122122
changes.hydration_errored = true;
123123
changes.hydration_error = "Task took too long.";
124-
child.kill('SIGKILL');
125-
cleaner(null, changes);
124+
errLogger('Killing task: ' + ((task.file_path) ? task.file_path : task.document.id));
125+
child.kill('SIGTERM');
126+
setTimeout(function() {
127+
if(child.connected) {
128+
child.kill('SIGKILL');
129+
}
130+
cleaner(null, changes);
131+
}, process.env.TIMEOUT / 6 || 10 * 1000);
126132
}
127133
}, process.env.TIMEOUT || 60 * 1000);
128134
},

0 commit comments

Comments
 (0)