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 support for stdout and stderr options #186

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion src/Pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ function Pool(script, options) {
this.forkOpts = options.forkOpts || {};
this.debugPortStart = (options.debugPortStart || 43210);
this.nodeWorker = options.nodeWorker;
this.workerType = options.workerType || options.nodeWorker || 'auto'
this.workerOpts = {
stdout: options.stdout,
stderr: options.stderr,
};
this.workerType = options.workerType || options.nodeWorker || 'auto';
this.maxQueueSize = options.maxQueueSize || Infinity;

// configuration
Expand Down Expand Up @@ -355,6 +359,7 @@ Pool.prototype._createWorkerHandler = function () {
forkArgs: this.forkArgs,
forkOpts: this.forkOpts,
debugPort: DEBUG_PORT_ALLOCATOR.nextAvailableStartingAt(this.debugPortStart),
workerOpts: this.workerOpts,
workerType: this.workerType
});
}
Expand Down
9 changes: 5 additions & 4 deletions src/WorkerHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function setupWorker(script, options) {
return setupBrowserWorker(script, Worker);
} else if (options.workerType === 'thread') { // node.js only
WorkerThreads = ensureWorkerThreads();
return setupWorkerThreadWorker(script, WorkerThreads);
return setupWorkerThreadWorker(script, options, WorkerThreads);
} else if (options.workerType === 'process' || !options.workerType) { // node.js only
return setupProcessWorker(script, resolveForkOptions(options), requireFoolWebpack('child_process'));
} else { // options.workerType === 'auto' or undefined
Expand All @@ -72,7 +72,7 @@ function setupWorker(script, options) {
else { // environment.platform === 'node'
var WorkerThreads = tryRequireWorkerThreads();
if (WorkerThreads) {
return setupWorkerThreadWorker(script, WorkerThreads);
return setupWorkerThreadWorker(script, options, WorkerThreads);
} else {
return setupProcessWorker(script, resolveForkOptions(options), requireFoolWebpack('child_process'));
}
Expand All @@ -97,10 +97,11 @@ function setupBrowserWorker(script, Worker) {
return worker;
}

function setupWorkerThreadWorker(script, WorkerThreads) {
function setupWorkerThreadWorker(script, options, WorkerThreads) {
var worker = new WorkerThreads.Worker(script, {
stdout: false, // automatically pipe worker.STDOUT to process.STDOUT
stderr: false // automatically pipe worker.STDERR to process.STDERR
stderr: false, // automatically pipe worker.STDERR to process.STDERR
...options.workerOpts,
});
worker.isWorkerThread = true;
// make the worker mimic a child_process
Expand Down
2 changes: 2 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* @property {number} [maxWorkers]
* @property {number} [maxQueueSize]
* @property {'auto' | 'web' | 'process' | 'thread'} [workerType]
* @property {boolean} [stdout]
* @property {boolean} [stderr]
* @property {*} [forkArgs]
* @property {*} [forkOpts]
* @property {number} [debugPortStart]
Expand Down
3 changes: 2 additions & 1 deletion test/WorkerHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ describe('WorkerHandler', function () {
describe('setupWorkerThreadWorker', function() {
it('works', function() {
var SCRIPT = 'the script';
const OPTIONS = {};
var postMessage;
var addEventListener;
var terminate = 0;
Expand All @@ -417,7 +418,7 @@ describe('WorkerHandler', function () {
terminate++;
}

var worker = WorkerHandler._setupWorkerThreadWorker(SCRIPT, { Worker: Worker });
var worker = WorkerHandler._setupWorkerThreadWorker(SCRIPT, OPTIONS, { Worker: Worker });

assert.ok(worker instanceof Worker);

Expand Down