From 9049f86c5eb075b38add1e41b3271ee9263f8dc5 Mon Sep 17 00:00:00 2001 From: Sam Saccone Date: Sat, 13 May 2017 15:32:02 -0700 Subject: [PATCH] Handle sigint and handle connecting to existing. --- chrome-launcher/chrome-launcher.ts | 30 +++++++++++++++++++-- lighthouse-cli/bin.ts | 42 +++++++----------------------- 2 files changed, 37 insertions(+), 35 deletions(-) mode change 100755 => 100644 lighthouse-cli/bin.ts diff --git a/chrome-launcher/chrome-launcher.ts b/chrome-launcher/chrome-launcher.ts index 8d7579cce8ef..cf8386f6b3c1 100644 --- a/chrome-launcher/chrome-launcher.ts +++ b/chrome-launcher/chrome-launcher.ts @@ -22,13 +22,14 @@ import * as fs from 'fs'; import * as chromeFinder from './chrome-finder'; import {DEFAULT_FLAGS} from './flags'; import {defaults, delay, makeUnixTmpDir, makeWin32TmpDir} from './utils'; - import * as net from 'net'; const rimraf = require('rimraf'); const log = require('../lighthouse-core/lib/log'); const spawn = childProcess.spawn; const execSync = childProcess.execSync; const isWindows = process.platform === 'win32'; +const _SIGINT = 'SIGINT'; +const _SIGINT_EXIT_CODE = 130; type SupportedPlatforms = 'darwin'|'linux'|'win32'; @@ -37,10 +38,24 @@ export interface Options { chromeFlags?: Array; autoSelectChrome?: boolean; port?: number; + handleSIGINT?: boolean; } -export async function launch(opts?: Options) { +export interface LaunchedChrome { kill: () => Promise<{}>; } + +export async function launch(opts: Options = {}): Promise { + opts.handleSIGINT = defaults(opts.handleSIGINT, true); + const instance = new ChromeLauncher(opts); + + // Kill spawned Chrome process in case of ctrl-C. + if (opts.handleSIGINT) { + process.on(_SIGINT, async () => { + await instance.kill(); + process.exit(_SIGINT_EXIT_CODE); + }); + } + await instance.launch(); return {kill: instance.kill}; @@ -114,6 +129,17 @@ class ChromeLauncher { } async launch() { + if (this.port !== 0) { + // If an explict port is passed first look for an open connection... + try { + return await this.isDebuggerReady(); + } catch (err) { + log.log( + 'ChromeLauncher', + `No debugging port found on port ${this.port}, launching a new Chrome.`); + } + } + if (!this.prepared) { this.prepare(); } diff --git a/lighthouse-cli/bin.ts b/lighthouse-cli/bin.ts old mode 100755 new mode 100644 index aa4f6a5a6db1..f45afc92c3d4 --- a/lighthouse-cli/bin.ts +++ b/lighthouse-cli/bin.ts @@ -16,14 +16,12 @@ */ 'use strict'; -const _SIGINT = 'SIGINT'; -const _SIGINT_EXIT_CODE = 130; const _RUNTIME_ERROR_CODE = 1; const _PROTOCOL_TIMEOUT_EXIT_CODE = 67; const assetSaver = require('../lighthouse-core/lib/asset-saver.js'); const getFilenamePrefix = require('../lighthouse-core/lib/file-namer.js').getFilenamePrefix; -import {ChromeLauncher} from '../chrome-launcher/chrome-launcher'; +import {launch, LaunchedChrome} from '../chrome-launcher/chrome-launcher'; import * as Commands from './commands/commands'; import {getFlags, Flags} from './cli-flags'; const lighthouse = require('../lighthouse-core'); @@ -104,31 +102,9 @@ function initPort(flags: Flags): Promise { * port. If none is found and the `skipAutolaunch` flag is not true, launches * a debuggable instance. */ -function getDebuggableChrome(flags: Flags): Promise { - const chromeLauncher = new ChromeLauncher({ - port: flags.port, - chromeFlags: flags.chromeFlags.split(' '), - autoSelectChrome: !flags.selectChrome, - }); - - // Kill spawned Chrome process in case of ctrl-C. - process.on(_SIGINT, () => { - chromeLauncher.kill().then(() => process.exit(_SIGINT_EXIT_CODE), handleError); - }); - - return chromeLauncher - // Check if there is an existing instance of Chrome ready to talk. - .isDebuggerReady() - .catch(() => { - if (flags.skipAutolaunch) { - return; - } - - // If not, create one. - log.log('Lighthouse CLI', 'Launching Chrome...'); - return chromeLauncher.run(); - }) - .then(() => chromeLauncher); +async function getDebuggableChrome(flags: Flags) { + return await launch( + {port: flags.port, chromeFlags: flags.chromeFlags.split(' '), handleSIGINT: true}); } function showConnectionError() { @@ -217,11 +193,11 @@ function saveResults(results: Results, artifacts: Object, flags: Flags) { export async function runLighthouse( url: string, flags: Flags, config: Object|null): Promise<{}|void> { - let chromeLauncher: ChromeLauncher|undefined = undefined; + let launchedChrome: LaunchedChrome|undefined; try { await initPort(flags); - const chromeLauncher = await getDebuggableChrome(flags); + launchedChrome = await getDebuggableChrome(flags); const results = await lighthouse(url, flags, config); const artifacts = results.artifacts; @@ -232,10 +208,10 @@ export async function runLighthouse( await performanceXServer.hostExperiment({url, flags, config}, results); } - return await chromeLauncher.kill(); + return await launchedChrome.kill(); } catch (err) { - if (typeof chromeLauncher !== 'undefined') { - await chromeLauncher!.kill(); + if (typeof launchedChrome !== 'undefined') { + await launchedChrome!.kill(); } return handleError(err);