From db693a17bd1fa218a941c07af3e5202ccfd137bd Mon Sep 17 00:00:00 2001 From: Spencer Date: Tue, 30 Mar 2021 15:57:10 -0700 Subject: [PATCH] [cli-dev-mode] complete shutdown once devServer stops gracefully (#95822) Co-authored-by: spalger Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../src/base_path_proxy_server.ts | 3 -- packages/kbn-cli-dev-mode/src/cli_dev_mode.ts | 46 +++++++++++++++++-- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/packages/kbn-cli-dev-mode/src/base_path_proxy_server.ts b/packages/kbn-cli-dev-mode/src/base_path_proxy_server.ts index 40841c8327cc2..6d12d5d05f07c 100644 --- a/packages/kbn-cli-dev-mode/src/base_path_proxy_server.ts +++ b/packages/kbn-cli-dev-mode/src/base_path_proxy_server.ts @@ -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); @@ -101,7 +99,6 @@ export class BasePathProxyServer { return; } - this.log.write('stopping basepath proxy server'); await this.server.stop(); this.server = undefined; diff --git a/packages/kbn-cli-dev-mode/src/cli_dev_mode.ts b/packages/kbn-cli-dev-mode/src/cli_dev_mode.ts index 94dbcb9654e8a..58e0a3e87a143 100644 --- a/packages/kbn-cli-dev-mode/src/cli_dev_mode.ts +++ b/packages/kbn-cli-dev-mode/src/cli_dev_mode.ts @@ -7,6 +7,8 @@ */ import Path from 'path'; +import { EventEmitter } from 'events'; + import * as Rx from 'rxjs'; import { map, @@ -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'; @@ -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(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; @@ -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) {