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

[procrunner] avoid waiting for processes forever #65909

Merged
merged 4 commits into from
May 11, 2020
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
26 changes: 21 additions & 5 deletions packages/kbn-dev-utils/src/proc_runner/proc_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@
*/

import moment from 'moment';
import { filter, first, catchError } from 'rxjs/operators';
import * as Rx from 'rxjs';
import { filter, first, catchError, map } from 'rxjs/operators';
import exitHook from 'exit-hook';

import { ToolingLog } from '../tooling_log';
import { createCliError } from './errors';
import { Proc, ProcOptions, startProc } from './proc';

const SECOND = 1000;
const MINUTE = 60 * SECOND;

const noop = () => {};

interface RunOptions extends ProcOptions {
wait: true | RegExp;
waitTimeout?: number | false;
}

/**
Expand Down Expand Up @@ -71,6 +76,7 @@ export class ProcRunner {
cwd = process.cwd(),
stdin = undefined,
wait = false,
waitTimeout = 15 * MINUTE,
env = process.env,
} = options;

Expand All @@ -97,8 +103,8 @@ export class ProcRunner {
try {
if (wait instanceof RegExp) {
// wait for process to log matching line
await proc.lines$
.pipe(
await Rx.race(
proc.lines$.pipe(
filter(line => wait.test(line)),
first(),
catchError(err => {
Expand All @@ -108,8 +114,18 @@ export class ProcRunner {
throw err;
}
})
)
.toPromise();
),
waitTimeout === false
? Rx.NEVER
: Rx.timer(waitTimeout).pipe(
map(() => {
const sec = waitTimeout / SECOND;
throw createCliError(
`[${name}] failed to match pattern within ${sec} seconds [pattern=${wait}]`
);
})
)
).toPromise();
}

if (wait === true) {
Expand Down
Loading