Skip to content
Merged
Show file tree
Hide file tree
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
31 changes: 15 additions & 16 deletions src/WorkerHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ function handleEmittedStdPayload(handler, payload) {
// TODO: refactor if parallel task execution gets added
Object.values(handler.processing)
.forEach(task => task?.options?.on(payload));

Object.values(handler.tracking)
.forEach(task => task?.options?.on(payload));
.forEach(task => task?.options?.on(payload));
}

/**
Expand Down Expand Up @@ -306,7 +306,7 @@ function WorkerHandler(script, _options) {
task.options.on(response.payload);
}
}
}
}
}

if (response.method === CLEANUP_METHOD_ID) {
Expand Down Expand Up @@ -335,7 +335,7 @@ function WorkerHandler(script, _options) {
me.processing[id].resolver.reject(error);
}
}

me.processing = Object.create(null);
}

Expand Down Expand Up @@ -438,7 +438,7 @@ WorkerHandler.prototype.exec = function(method, params, resolver, options) {
options: options,
error,
};

// remove this task from the queue. It is already rejected (hence this
// catch event), and else it will be rejected again when terminating
delete me.processing[id];
Expand All @@ -454,31 +454,31 @@ WorkerHandler.prototype.exec = function(method, params, resolver, options) {
}

var promise = me.terminateAndNotify(true)
.then(function() {
.then(function() {
throw err;
}, function(err) {
throw err;
});

return promise;
});

me.worker.send({
id,
method: CLEANUP_METHOD_ID
method: CLEANUP_METHOD_ID
});


/**
* Sets a timeout to reject the cleanup operation if the message sent to the worker
* does not receive a response. see worker.tryCleanup for worker cleanup operations.
* Here we use the workerTerminateTimeout as the worker will be terminated if the timeout does invoke.
*
*
* We need this timeout in either case of a Timeout or Cancellation Error as if
* the worker does not send a message we still need to give a window of time for a response.
*
* The workerTermniateTimeout is used here if this promise is rejected the worker cleanup
* operations will occure.
*
* The workerTerminateTimeout is used here if this promise is rejected the worker cleanup
* operations will occur.
*/
me.tracking[id].timeoutId = setTimeout(function() {
me.tracking[id].resolver.reject(error);
Expand Down Expand Up @@ -625,7 +625,7 @@ WorkerHandler.prototype.terminateAndNotify = function (force, timeout) {
};

/**
* Wrapper error type to denote that a TimeoutError has already been proceesed
* Wrapper error type to denote that a TimeoutError has already been processed
* and we should skip cleanup operations
* @param {Promise.TimeoutError} timeoutError
*/
Expand Down Expand Up @@ -653,4 +653,3 @@ module.exports._setupBrowserWorker = setupBrowserWorker;
module.exports._setupWorkerThreadWorker = setupWorkerThreadWorker;
module.exports.ensureWorkerThreads = ensureWorkerThreads;
module.exports.TerminateError = TerminateError

8 changes: 4 additions & 4 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var Promise = require('./Promise').Promise;
var TERMINATE_METHOD_ID = '__workerpool-terminate__';

/**
* Special message by parent which causes a child process worker to perform cleaup
* Special message by parent which causes a child process worker to perform cleanup
* steps before determining if the child process worker should be terminated.
*/
var CLEANUP_METHOD_ID = '__workerpool-cleanup__';
Expand All @@ -35,7 +35,7 @@ var worker = {
// works in both node.js and the browser
var publicWorker = {
/**
* Registers listeners which will trigger when a task is timed out or cancled. If all listeners resolve, the worker executing the given task will not be terminated.
* Registers listeners which will trigger when a task is timed out or canceled. If all listeners resolve, the worker executing the given task will not be terminated.
* *Note*: If there is a blocking operation within a listener, the worker will be terminated.
* @param {() => Promise<void>} listener
*/
Expand Down Expand Up @@ -217,11 +217,11 @@ worker.cleanup = function(requestId) {
let timerId;
const timeoutPromise = new Promise((_resolve, reject) => {
timerId = setTimeout(function () {
reject(new Error('Timeout occured waiting for abort handler, killing worker'));
reject(new Error('Timeout occurred waiting for abort handler, killing worker'));
}, worker.abortListenerTimeout);
});

// Once a promise settles we need to clear the timeout to prevet fulfulling the promise twice
// Once a promise settles we need to clear the timeout to prevent fulfilling the promise twice
const settlePromise = Promise.all(promises).then(function() {
clearTimeout(timerId);
_abort();
Expand Down
34 changes: 17 additions & 17 deletions test/Pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ describe('Pool', function () {
assert(terminatedWorkers.includes('env_value2'), 'terminatedWorkers should include the value with counter = 2');
});
});

it('supports stdout/stderr capture via fork', function(done) {
var pool = createPool(__dirname + '/workers/console.js', {workerType: 'process', emitStdStreams: true});

Expand Down Expand Up @@ -1558,7 +1558,7 @@ describe('Pool', function () {
});
});


describe('abort handler', () => {
it('should not terminate worker if abort listener is defined dedicated worker with Timeout', function () {
var workerCount = 0;
Expand All @@ -1568,7 +1568,7 @@ describe('Pool', function () {
workerCount += 1;
}
});

return pool.exec('asyncTimeout', [])
.timeout(200)
.catch(function (err) {
Expand All @@ -1578,8 +1578,8 @@ describe('Pool', function () {
assert.strictEqual(stats.totalWorkers, 1);
assert.strictEqual(stats.idleWorkers, 1);
assert.strictEqual(stats.busyWorkers, 0);
}).then(function() {
return pool.exec(add, [1, 2])
}).then(function() {
return pool.exec(add, [1, 2])
}).then(function() {
var stats = pool.stats();
assert.strictEqual(workerCount, 1);
Expand All @@ -1598,12 +1598,12 @@ describe('Pool', function () {
workerCount += 1;
},
});

let task = pool.exec('asyncTimeout', [], {});

// Wrap in a new promise which waits 50ms
// in order to allow the function executing in the
// worker to
// worker to execute to termination
return new Promise(function(resolve) {
setTimeout(function() {
resolve();
Expand All @@ -1618,8 +1618,8 @@ describe('Pool', function () {
assert.strictEqual(stats.totalWorkers, 1);
assert.strictEqual(stats.idleWorkers, 1);
assert.strictEqual(stats.busyWorkers, 0);
}).then(function() {
return pool.exec(add, [1, 2])
}).then(function() {
return pool.exec(add, [1, 2])
}).then(function() {
var stats = pool.stats();
assert.strictEqual(workerCount, 1);
Expand All @@ -1645,7 +1645,7 @@ describe('Pool', function () {
return new Promise(function () {
let timeout = setTimeout(function() {
resolve();
}, 5000);
}, 5000);
me.worker.addAbortListener(function () {
return new Promise(function (resolve) {
clearTimeout(timeout);
Expand Down Expand Up @@ -1673,7 +1673,7 @@ describe('Pool', function () {
assert.strictEqual(stats.idleWorkers, 1);
assert.strictEqual(stats.busyWorkers, 0);

});
});
});
});

Expand All @@ -1691,7 +1691,7 @@ describe('Pool', function () {
return new Promise(function (_resolve, reject) {
let timeout = setTimeout(function() {
reject(new Error("should not be thrown"));
}, 5000);
}, 5000);
me.worker.addAbortListener(function () {
return new Promise(function (resolve) {
clearTimeout(timeout);
Expand Down Expand Up @@ -1723,7 +1723,7 @@ describe('Pool', function () {
assert.strictEqual(stats.idleWorkers, 1);
assert.strictEqual(stats.busyWorkers, 0);

});
});
});
});

Expand Down Expand Up @@ -1782,14 +1782,14 @@ describe('Pool', function () {
done();
}
});

const task = pool.exec('asyncAbortHandlerNeverResolves', [])

const _ = new Promise(function(resolve) {
setTimeout(function() {
resolve();
}, 50);
}).then(function() {
}).then(function() {
return task.cancel()
.catch(function (err) {
assert(err instanceof Promise.TimeoutError);
Expand Down Expand Up @@ -1818,7 +1818,7 @@ describe('Pool', function () {
var pool = createPool(__dirname + '/workers/cleanup-abort.js', {
maxWorkers: 1,
workerType: 'process',
emitStdStreams: true,
emitStdStreams: true,
workerTerminateTimeout: 1000,
});

Expand All @@ -1835,7 +1835,7 @@ describe('Pool', function () {
var pool = createPool(__dirname + '/workers/cleanup-abort.js', {
maxWorkers: 1,
workerType: 'process',
emitStdStreams: true,
emitStdStreams: true,
workerTerminateTimeout: 1000,
});

Expand Down
Loading