Files
flipper/src/utils/adbClient.js
John Knox 3e336d2349 Print error when adb client creation fails
Summary:
It looks like the shell method of creating an adb client is failing almost all of the time: https://fburl.com/scuba/6ac2zhxn

Thankfully we have the adbkit fallback, but we don't have the error message to see why it's failing: https://our.intern.facebook.com/intern/logview/details/infinity_javascript/dab8d34562f0cdb835132f8825317942/

Reviewed By: danielbuechele

Differential Revision: D14260406

fbshipit-source-id: 6b21b5d9a4806780daa7ce692770788067364d12
2019-03-01 04:26:46 -08:00

74 lines
2.1 KiB
JavaScript

/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
import {reportPlatformFailures} from './metrics';
import {promisify} from 'util';
import child_process from 'child_process';
import promiseRetry from 'promise-retry';
import adbConfig from '../utils/adbConfig';
import adbkit from 'adbkit-fb';
let instance;
export function getAdbClient(): Promise<any> {
if (!instance) {
instance = reportPlatformFailures(createClient(), 'createADBClient');
}
return instance;
}
/* Adbkit will attempt to start the adb server if it's not already running,
however, it sometimes fails with ENOENT errors. So instead, we start it
manually before requesting a client. */
function createClient() {
const adbPath = process.env.ANDROID_HOME
? `${process.env.ANDROID_HOME}/platform-tools/adb`
: 'adb';
return reportPlatformFailures(
promisify(child_process.exec)(`${adbPath} start-server`)
.then(result => {
if (result.error) {
throw new Error(
`Failed to start adb server: ${result.stderr.toString()}`,
);
}
})
.then(() => adbkit.createClient(adbConfig())),
'createADBClient.shell',
).catch(err => {
console.error(
'Failed to create adb client using shell adb command. Trying with adbkit.\n' +
err.toString(),
);
/* In the event that starting adb with the above method fails, fallback
to using adbkit, though its known to be unreliable. */
const unsafeClient = adbkit.createClient(adbConfig());
return reportPlatformFailures(
promiseRetry(
(retry, number) => {
return unsafeClient
.listDevices()
.then(() => {
return unsafeClient;
})
.catch(e => {
console.warn(
`Failed to start adb client. Retrying. ${e.message}`,
);
retry(e);
});
},
{
minTimeout: 200,
retries: 5,
},
),
'createADBClient.adbkit',
);
});
}