CB-9119 Adding lib/retry.js for retrying promise-returning functions. Retrying 'adb install' in emulator.js because it sometimes hangs.

This commit is contained in:
Dmitry Blotsky
2015-06-08 17:17:12 -07:00
parent eb70f05168
commit c0312f9b50
3 changed files with 167 additions and 36 deletions
+33 -6
View File
@@ -19,23 +19,50 @@
under the License.
*/
var child_process = require('child_process'),
Q = require('q');
var child_process = require("child_process");
var Q = require("q");
// constants
var DEFAULT_MAX_BUFFER = 1024000;
// Takes a command and optional current working directory.
// Returns a promise that either resolves with the stdout, or
// rejects with an error message and the stderr.
module.exports = function(cmd, opt_cwd) {
//
// WARNING:
// opt_cwd is an artifact of an old design, and must
// be removed in the future; the correct solution is
// to pass the options object the same way that
// child_process.exec expects
//
// NOTE:
// exec documented here - https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
module.exports = function(cmd, opt_cwd, options) {
var d = Q.defer();
if (typeof options === "undefined") {
options = {};
}
// override cwd to preserve old opt_cwd behavior
options.cwd = opt_cwd;
// set maxBuffer
if (typeof options.maxBuffer === "undefined") {
options.maxBuffer = DEFAULT_MAX_BUFFER;
}
try {
child_process.exec(cmd, {cwd: opt_cwd, maxBuffer: 1024000}, function(err, stdout, stderr) {
if (err) d.reject('Error executing "' + cmd + '": ' + stderr);
child_process.exec(cmd, options, function(err, stdout, stderr) {
if (err) d.reject("Error executing \"" + cmd + "\": " + stderr);
else d.resolve(stdout);
});
} catch(e) {
console.error('error caught: ' + e);
console.error("error caught: " + e);
d.reject(e);
}
return d.promise;
};