[go: nahoru, domu]

Skip to content

Commit

Permalink
Handle sigint and handle connecting to existing.
Browse files Browse the repository at this point in the history
  • Loading branch information
samccone committed May 15, 2017
1 parent b89d7c5 commit 9049f86
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
30 changes: 28 additions & 2 deletions chrome-launcher/chrome-launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -37,10 +38,24 @@ export interface Options {
chromeFlags?: Array<string>;
autoSelectChrome?: boolean;
port?: number;
handleSIGINT?: boolean;
}

export async function launch(opts?: Options) {
export interface LaunchedChrome { kill: () => Promise<{}>; }

export async function launch(opts: Options = {}): Promise<LaunchedChrome> {
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};
Expand Down Expand Up @@ -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();
}
Expand Down
42 changes: 9 additions & 33 deletions lighthouse-cli/bin.ts
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -104,31 +102,9 @@ function initPort(flags: Flags): Promise<undefined> {
* port. If none is found and the `skipAutolaunch` flag is not true, launches
* a debuggable instance.
*/
function getDebuggableChrome(flags: Flags): Promise<ChromeLauncher> {
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() {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 9049f86

Please sign in to comment.