-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
WIP: t31135461 / Refine jest-worker API #6676
Closed
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
418c6f9
Restructure workers (create a base class and inherit from it)
100c49f
Move child.js to workers folder
f786cb6
Restructure base classes and make a working POC
2a1ac0e
Remove BaseWorker
59ebd73
Remove MessageChannel and cleanup super() calls
9b1f19b
Use worker threads implementation if possible
bea945f
Restructure queues
3e9ce02
Support experimental modules in jest-resolver
02dedef
Rename child.js to processChild.js
5e8347b
Remove private properties from WorkerPoolInterface
e60bc1f
Move common line outside of if-else
b7470d6
Unify interface (use workerId) and remove recursion
092d700
Remove opt-out option for worker_threads in node 10.5+
3947fcb
Alphabetical import sorting
c715f00
Unlock worker after onEnd
4614e54
Cache queue head in the getNextJob loop
e0bfb83
Elegant while loop
9958587
Remove redundand .binds
29e04d2
Clean up interfaces and responsibilites
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type {QueueChildMessage} from './types'; | ||
|
||
export default class QueueManager { | ||
_callback: Function; | ||
_last: Array<QueueChildMessage>; | ||
_locks: Array<boolean>; | ||
_numOfWorkers: number; | ||
_offset: number; | ||
_queue: Array<?QueueChildMessage>; | ||
|
||
constructor(numOfWorkers: number, callback: Function) { | ||
this._callback = callback; | ||
this._numOfWorkers = numOfWorkers; | ||
this._queue = []; | ||
this._last = []; | ||
this._locks = []; | ||
this._offset = 0; | ||
|
||
// If we exceeded the amount of retries, we will emulate an error reply | ||
// coming from the child. This avoids code duplication related with cleaning | ||
// the queue, and scheduling the next call. | ||
|
||
// if (this._retries > this._options.maxRetries) { | ||
// const error = new Error('Call retries were exceeded'); | ||
|
||
// this.onMessage([ | ||
// PARENT_MESSAGE_ERROR, | ||
// error.name, | ||
// error.message, | ||
// error.stack, | ||
// {type: 'WorkerError'}, | ||
// ]); | ||
// } | ||
} | ||
|
||
_process(workerId: number): QueueManager { | ||
if (this.isLocked(workerId)) { | ||
return this; | ||
} | ||
|
||
const job = this.getNextJob(workerId); | ||
|
||
if (!job) { | ||
return this; | ||
} | ||
|
||
const onEnd = (error: ?Error, result: mixed) => { | ||
job.onEnd(error, result); | ||
this.unlock(workerId); | ||
this._process(workerId); | ||
}; | ||
|
||
this.lock(workerId); | ||
|
||
this._callback(workerId, job.request, job.onStart, onEnd); | ||
|
||
job.request[1] = true; | ||
|
||
return this; | ||
} | ||
|
||
getNextJob(workerId: number): ?QueueChildMessage { | ||
let queueHead = this._queue[workerId]; | ||
|
||
if (!queueHead) { | ||
return null; | ||
} | ||
|
||
while (queueHead && queueHead.request[1]) { | ||
queueHead = queueHead.next; | ||
} | ||
|
||
this._queue[workerId] = queueHead; | ||
|
||
return queueHead; | ||
} | ||
|
||
enqueue(task: QueueChildMessage, workerId: number): QueueManager { | ||
if (task.request[1]) { | ||
return this; | ||
} | ||
|
||
if (this._queue[workerId]) { | ||
this._last[workerId].next = task; | ||
} else { | ||
this._queue[workerId] = task; | ||
} | ||
|
||
this._last[workerId] = task; | ||
this._process(workerId); | ||
|
||
return this; | ||
} | ||
|
||
push(task: QueueChildMessage): QueueManager { | ||
for (let i = 0; i < this._numOfWorkers; i++) { | ||
const workerIdx = (this._offset + i) % this._numOfWorkers; | ||
this.enqueue(task, workerIdx); | ||
} | ||
this._offset++; | ||
|
||
return this; | ||
} | ||
|
||
lock(workerId: number): void { | ||
this._locks[workerId] = true; | ||
} | ||
|
||
unlock(workerId: number): void { | ||
this._locks[workerId] = false; | ||
} | ||
|
||
isLocked(workerId: number): boolean { | ||
return this._locks[workerId]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import BaseWorkerPool from './base/BaseWorkerPool'; | ||
import ChildProcessWorker from './workers/ChildProcessWorker'; | ||
import NodeThreadsWorker from './workers/NodeThreadsWorker'; | ||
|
||
import type { | ||
ChildMessage, | ||
WorkerOptions, | ||
OnStart, | ||
OnEnd, | ||
WorkerPoolInterface, | ||
WorkerInterface, | ||
} from './types'; | ||
|
||
class WorkerPool extends BaseWorkerPool implements WorkerPoolInterface { | ||
send( | ||
workerId: number, | ||
request: ChildMessage, | ||
onStart: OnStart, | ||
onEnd: OnEnd, | ||
): void { | ||
this.getWorkerById(workerId).send(request, onStart, onEnd); | ||
} | ||
|
||
createWorker(workerOptions: WorkerOptions): WorkerInterface { | ||
return this._options.useWorkers | ||
? new NodeThreadsWorker(workerOptions) | ||
: new ChildProcessWorker(workerOptions); | ||
} | ||
} | ||
|
||
export default WorkerPool; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import mergeStream from 'merge-stream'; | ||
import path from 'path'; | ||
|
||
import {CHILD_MESSAGE_END} from '../types'; | ||
|
||
import type {Readable} from 'stream'; | ||
import type {WorkerPoolOptions, WorkerOptions, WorkerInterface} from '../types'; | ||
|
||
/* istanbul ignore next */ | ||
const emptyMethod = () => {}; | ||
|
||
export default class BaseWorkerPool { | ||
_stderr: Readable; | ||
_stdout: Readable; | ||
_options: WorkerPoolOptions; | ||
_workers: Array<WorkerInterface>; | ||
|
||
constructor(workerPath: string, options: WorkerPoolOptions) { | ||
this._options = options; | ||
this._workers = new Array(options.numWorkers); | ||
|
||
if (!path.isAbsolute(workerPath)) { | ||
workerPath = require.resolve(workerPath); | ||
} | ||
|
||
const stdout = mergeStream(); | ||
const stderr = mergeStream(); | ||
|
||
const {forkOptions, maxRetries} = options; | ||
|
||
for (let i = 0; i < options.numWorkers; i++) { | ||
const workerOptions: WorkerOptions = { | ||
forkOptions, | ||
maxRetries, | ||
workerId: i, | ||
workerPath, | ||
}; | ||
|
||
const worker = this.createWorker(workerOptions); | ||
const workerStdout = worker.getStdout(); | ||
const workerStderr = worker.getStderr(); | ||
|
||
if (workerStdout) { | ||
stdout.add(workerStdout); | ||
} | ||
|
||
if (workerStderr) { | ||
stderr.add(workerStderr); | ||
} | ||
|
||
this._workers[i] = worker; | ||
} | ||
|
||
this._stdout = stdout; | ||
this._stderr = stderr; | ||
} | ||
|
||
getStderr(): Readable { | ||
return this._stderr; | ||
} | ||
|
||
getStdout(): Readable { | ||
return this._stdout; | ||
} | ||
|
||
getWorkers(): Array<WorkerInterface> { | ||
return this._workers; | ||
} | ||
|
||
getWorkerById(workerId: number): WorkerInterface { | ||
return this._workers[workerId]; | ||
} | ||
|
||
createWorker(workerOptions: WorkerOptions): WorkerInterface { | ||
throw Error('Missing method createWorker in WorkerPool'); | ||
} | ||
|
||
end(): void { | ||
// We do not cache the request object here. If so, it would only be only | ||
// processed by one of the workers, and we want them all to close. | ||
for (let i = 0; i < this._workers.length; i++) { | ||
this._workers[i].send( | ||
[CHILD_MESSAGE_END, false], | ||
emptyMethod, | ||
emptyMethod, | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
18? I have to admit I don't know what these should say