Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added timeout and logic for cleaning child and his children processes #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function wkhtmltopdf(input, options, callback) {

keys.forEach(function(key) {
var val = options[key];
if (key === 'ignore' || key === 'debug' || key === 'debugStdOut') { // skip adding the ignore/debug keys
if (key === 'ignore' || key === 'debug' || key === 'debugStdOut' || key === 'timeout') { // skip adding the ignore/debug keys
return false;
}

Expand Down Expand Up @@ -124,6 +124,15 @@ function wkhtmltopdf(input, options, callback) {
var child = spawn(wkhtmltopdf.shell, ['-c', 'set -o pipefail ; ' + args.join(' ') + ' | cat'], spawnOptions);
}

var timeout
if (options.timeout) {
timeout = setTimeout(function () {
var timeoutError = new Error('Child process terminated due to timeout');
timeoutError.code = '_EXIT_TIMEOUT';
handleError(timeoutError);
}, options.timeout*1000);
}

var stream = child.stdout;

// call the callback with null error when the process exits successfully
Expand All @@ -132,6 +141,7 @@ function wkhtmltopdf(input, options, callback) {
stderrMessages.push('wkhtmltopdf exited with code ' + code);
handleError(stderrMessages);
} else if (callback) {
clearTimeout(timeout);
callback(null, stream); // stream is child.stdout
}
});
Expand Down Expand Up @@ -162,8 +172,14 @@ function wkhtmltopdf(input, options, callback) {
} else if (err) {
errObj = new Error(err);
}
child.removeAllListeners('exit');
child.kill();
clearTimeout(timeout);
if(spawnOptions.detached) {
// this is the way closing child process and his children, when is spawned detached
process.kill(-child.pid);
} else {
child.removeAllListeners('exit');
child.kill();
}
// call the callback if there is one

if (callback) {
Expand Down