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

[7.x] [cli-dev-mode] complete shutdown once devServer stops gracefully (#95822) #95878

Merged
merged 1 commit into from
Mar 31, 2021
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
3 changes: 0 additions & 3 deletions packages/kbn-cli-dev-mode/src/base_path_proxy_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ export class BasePathProxyServer {
}

public async start(options: BasePathProxyServerOptions) {
this.log.write('starting basepath proxy server');

const serverOptions = getServerOptions(this.httpConfig);
const listenerOptions = getListenerOptions(this.httpConfig);
this.server = createServer(serverOptions, listenerOptions);
Expand Down Expand Up @@ -101,7 +99,6 @@ export class BasePathProxyServer {
return;
}

this.log.write('stopping basepath proxy server');
await this.server.stop();
this.server = undefined;

Expand Down
46 changes: 43 additions & 3 deletions packages/kbn-cli-dev-mode/src/cli_dev_mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

import Path from 'path';
import { EventEmitter } from 'events';

import * as Rx from 'rxjs';
import {
map,
Expand All @@ -17,6 +19,7 @@ import {
distinctUntilChanged,
switchMap,
concatMap,
takeUntil,
} from 'rxjs/operators';
import { CliArgs } from '@kbn/config';
import { REPO_ROOT, CiStatsReporter } from '@kbn/dev-utils';
Expand All @@ -30,6 +33,16 @@ import { shouldRedirectFromOldBasePath } from './should_redirect_from_old_base_p
import { getServerWatchPaths } from './get_server_watch_paths';
import { CliDevConfig } from './config';

// signal that emits undefined once a termination signal has been sent
const exitSignal$ = new Rx.ReplaySubject<undefined>(1);
Rx.merge(
Rx.fromEvent(process as EventEmitter, 'exit'),
Rx.fromEvent(process as EventEmitter, 'SIGINT'),
Rx.fromEvent(process as EventEmitter, 'SIGTERM')
)
.pipe(mapTo(undefined), take(1))
.subscribe(exitSignal$);

// timeout where the server is allowed to exit gracefully
const GRACEFUL_TIMEOUT = 5000;

Expand Down Expand Up @@ -216,9 +229,36 @@ export class CliDevMode {
this.log.warn('no-base-path', '='.repeat(100));
}

this.subscription.add(this.optimizer.run$.subscribe(this.observer('@kbn/optimizer')));
this.subscription.add(this.watcher.run$.subscribe(this.observer('watcher')));
this.subscription.add(this.devServer.run$.subscribe(this.observer('dev server')));
this.subscription.add(
this.optimizer.run$
.pipe(
// stop the optimizer as soon as we get an exit signal
takeUntil(exitSignal$)
)
.subscribe(this.observer('@kbn/optimizer'))
);

this.subscription.add(
this.watcher.run$
.pipe(
// stop the watcher as soon as we get an exit signal
takeUntil(exitSignal$)
)
.subscribe(this.observer('watcher'))
);

this.subscription.add(
this.devServer.run$
.pipe(
tap({
complete: () => {
// when the devServer gracefully exits because of an exit signal stop the cli dev mode to trigger full shutdown
this.stop();
},
})
)
.subscribe(this.observer('dev server'))
);
}

private reportTimings(reporter: CiStatsReporter) {
Expand Down