Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add optional timeout setting after which the child process is killed …

…if it has not finished executing.
  • Loading branch information...
commit d7c09c312c7c7fd9c5ccf57243699972b16645f7 1 parent 0634ca3
@Kami authored
Showing with 33 additions and 0 deletions.
  1. +33 −0 lib/worker.js
View
33 lib/worker.js
@@ -62,6 +62,7 @@ Worker.prototype.isTerminated = function () {
exports.Worker = Worker;
function WorkerChild (eventDest, filename, options) {
+ var timeout_id;
var self = this;
this.eventDest = eventDest;
this.filename = filename;
@@ -80,18 +81,50 @@ function WorkerChild (eventDest, filename, options) {
});
this.child.addListener("exit", function (code) {
+ if (!this.terminated) {
+ this.terminated = true;
+ }
+
+ if (self.timeout_id) {
+ clearTimeout(self.timeout_id);
+ }
+
self.eventDest.emit("exit", code);
debug(self.child.pid + ": exit "+code);
});
+ if (options.timeout) {
+ // Set a timeout handler which kills the process if it has not finished
+ // executing after options.timeout number of milliseconds.
+ timeout_id = setTimeout(function() {
+ self.timeoutHandler();
+ }, options.timeout);
+ }
+ else {
+ timeout_id = null;
+ }
+
this.buffer = "";
this.active = false;
this.terminated = false;
this.queue = [];
+
+ this.options = options;
+ this.timeout_id = timeout_id;
}
WorkerChild.prototype = {
+ timeoutHandler: function() {
+ if (!this.terminated) {
+ this.child.kill('SIGTERM');
+
+ err = new Error('Child killed, because it did not finish after '
+ + this.options.timeout + ' milliseconds');
+ this.eventDest.emit("error", err);
+ }
+ },
+
postMessage: function (message) {
if (!this.active && this.terminated) {
throw new Error('Cannot post message to a terminated child');
Please sign in to comment.
Something went wrong with that request. Please try again.