Skip to content

Commit

Permalink
[cli-dev-mode] complete shutdown once devServer stops gracefully (#95822
Browse files Browse the repository at this point in the history
)

Co-authored-by: spalger <[email protected]>
Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
3 people committed Mar 30, 2021
1 parent 631c151 commit db693a1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
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

0 comments on commit db693a1

Please sign in to comment.