[kbn/optimizer] use execa to fork workers (#67730)

Co-authored-by: spalger <spalger@users.noreply.github.com>
This commit is contained in:
Spencer 2020-05-29 10:34:58 -07:00 committed by GitHub
parent 6b7b0cbc44
commit 6288096f62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 27 deletions

View file

@ -28,6 +28,7 @@
"cpy": "^8.0.0",
"css-loader": "^3.4.2",
"del": "^5.1.0",
"execa": "^4.0.2",
"file-loader": "^4.2.0",
"istanbul-instrumenter-loader": "^3.0.1",
"jest-diff": "^25.1.0",

View file

@ -17,10 +17,10 @@
* under the License.
*/
import { fork, ChildProcess } from 'child_process';
import { Readable } from 'stream';
import { inspect } from 'util';
import execa from 'execa';
import * as Rx from 'rxjs';
import { map, takeUntil } from 'rxjs/operators';
@ -42,7 +42,7 @@ export interface WorkerStarted {
export type WorkerStatus = WorkerStdio | WorkerStarted;
interface ProcResource extends Rx.Unsubscribable {
proc: ChildProcess;
proc: execa.ExecaChildProcess;
}
const isNumeric = (input: any) => String(input).match(/^[0-9]+$/);
@ -70,20 +70,22 @@ function usingWorkerProc<T>(
config: OptimizerConfig,
workerConfig: WorkerConfig,
bundles: Bundle[],
fn: (proc: ChildProcess) => Rx.Observable<T>
fn: (proc: execa.ExecaChildProcess) => Rx.Observable<T>
) {
return Rx.using(
(): ProcResource => {
const args = [JSON.stringify(workerConfig), JSON.stringify(bundles.map((b) => b.toSpec()))];
const proc = fork(require.resolve('../worker/run_worker'), args, {
stdio: ['ignore', 'pipe', 'pipe', 'ipc'],
execArgv: [
const proc = execa.node(require.resolve('../worker/run_worker'), args, {
nodeOptions: [
...(inspectFlag && config.inspectWorkers
? [`${inspectFlag}=${inspectPortCounter++}`]
: []),
...(config.maxWorkerCount <= 3 ? ['--max-old-space-size=2048'] : []),
],
buffer: false,
stderr: 'pipe',
stdout: 'pipe',
});
return {

View file

@ -34812,10 +34812,11 @@ const makeError = ({
const prefix = getErrorPrefix({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled});
const execaMessage = `Command ${prefix}: ${command}`;
const shortMessage = error instanceof Error ? `${execaMessage}\n${error.message}` : execaMessage;
const isError = Object.prototype.toString.call(error) === '[object Error]';
const shortMessage = isError ? `${execaMessage}\n${error.message}` : execaMessage;
const message = [shortMessage, stderr, stdout].filter(Boolean).join('\n');
if (error instanceof Error) {
if (isError) {
error.originalMessage = error.message;
error.message = message;
} else {
@ -36263,25 +36264,24 @@ module.exports = function (/*streams...*/) {
"use strict";
const mergePromiseProperty = (spawned, promise, property) => {
// Starting the main `promise` is deferred to avoid consuming streams
const value = typeof promise === 'function' ?
(...args) => promise()[property](...args) :
promise[property].bind(promise);
Object.defineProperty(spawned, property, {
value,
writable: true,
enumerable: false,
configurable: true
});
};
const nativePromisePrototype = (async () => {})().constructor.prototype;
const descriptors = ['then', 'catch', 'finally'].map(property => [
property,
Reflect.getOwnPropertyDescriptor(nativePromisePrototype, property)
]);
// The return value is a mixin of `childProcess` and `Promise`
const mergePromise = (spawned, promise) => {
mergePromiseProperty(spawned, promise, 'then');
mergePromiseProperty(spawned, promise, 'catch');
mergePromiseProperty(spawned, promise, 'finally');
for (const [property, descriptor] of descriptors) {
// Starting the main `promise` is deferred to avoid consuming streams
const value = typeof promise === 'function' ?
(...args) => Reflect.apply(descriptor.value, promise(), args) :
descriptor.value.bind(promise);
Reflect.defineProperty(spawned, property, {...descriptor, value});
}
return spawned;
};

View file

@ -13092,10 +13092,10 @@ execa@^0.7.0:
signal-exit "^3.0.0"
strip-eof "^1.0.0"
execa@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-4.0.0.tgz#7f37d6ec17f09e6b8fc53288611695b6d12b9daf"
integrity sha512-JbDUxwV3BoT5ZVXQrSVbAiaXhXUkIwvbhPIwZ0N13kX+5yCzOhUNdocxB/UQRuYOHRYYwAxKYwJYc0T4D12pDA==
execa@^4.0.0, execa@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/execa/-/execa-4.0.2.tgz#ad87fb7b2d9d564f70d2b62d511bee41d5cbb240"
integrity sha512-QI2zLa6CjGWdiQsmSkZoGtDx2N+cQIGb3yNolGTdjSQzydzLgYYf8LRuagp7S7fPimjcrzUDSUFd/MgzELMi4Q==
dependencies:
cross-spawn "^7.0.0"
get-stream "^5.0.0"