You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cordova.apache.org by dblotsky <gi...@git.apache.org> on 2015/06/09 02:20:04 UTC

[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

GitHub user dblotsky opened a pull request:

    https://github.com/apache/cordova-android/pull/180

    CB-9119 Fixing intermittent 'adb install' hanging issue

    Adding lib/retry.js for retrying promise-returning functions. Retrying 'adb install' in emulator.js to work around it hanging sometimes.
    
    Related Android bug: https://code.google.com/p/android/issues/detail?id=10255

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/MSOpenTech/cordova-android CB-9119

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/cordova-android/pull/180.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #180
    
----
commit 912f011bc7edc42b388a59c6fbce3229cd1209bb
Author: Dmitry Blotsky <dm...@gmail.com>
Date:   2015-06-09T00:17:12Z

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

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r32027248
  
    --- Diff: bin/templates/cordova/lib/emulator.js ---
    @@ -298,37 +306,67 @@ module.exports.resolveTarget = function(target) {
      * If no started emulators are found, error out.
      * Returns a promise.
      */
    -module.exports.install = function(target, buildResults) {
    -    return Q().then(function() {
    -        if (target && typeof target == 'object') {
    -            return target;
    +module.exports.install = function(givenTarget, buildResults) {
    +
    +    var target;
    +
    +    // resolve the target emulator
    +    return Q().then(function () {
    +        if (givenTarget && typeof givenTarget == 'object') {
    +            return givenTarget;
    +        } else {
    +            return module.exports.resolveTarget(givenTarget);
             }
    -        return module.exports.resolveTarget(target);
    -    }).then(function(resolvedTarget) {
    -        var apk_path = build.findBestApkForArchitecture(buildResults, resolvedTarget.arch);
    +
    +    // set the resolved target
    +    }).then(function (resolvedTarget) {
    +        target = resolvedTarget;
    +
    +    // install the app
    +    }).then(function () {
    +
    +        var apk_path    = build.findBestApkForArchitecture(buildResults, target.arch);
    +        var execOptions = {
    +            timeout:    INSTALL_COMMAND_TIMEOUT, // in milliseconds
    +            killSignal: EXEC_KILL_SIGNAL
    +        };
    +
             console.log('Installing app on emulator...');
             console.log('Using apk: ' + apk_path);
    -        return exec('adb -s ' + resolvedTarget.target + ' install -r -d "' + apk_path + '"', os.tmpdir())
    -        .then(function(output) {
    +
    +        var retriedInstall = retry.retryPromise(
    +            NUM_INSTALL_RETRIES,
    +            exec, 'adb -s ' + target.target + ' install -r -d "' + apk_path + '"', os.tmpdir(), execOptions
    +        );
    +
    +        return retriedInstall.then(function (output) {
                 if (output.match(/Failure/)) {
                     return Q.reject('Failed to install apk to emulator: ' + output);
    +            } else {
    +                console.log('INSTALL SUCCESS');
                 }
    -            return Q();
    -        }, function(err) {
    +        }, function (err) {
                 return Q.reject('Failed to install apk to emulator: ' + err);
    -        }).then(function() {
    -            //unlock screen
    -            return exec('adb -s ' + resolvedTarget.target + ' shell input keyevent 82', os.tmpdir());
    -        }).then(function() {
    -            // launch the application
    -            console.log('Launching application...');
    -            var launchName = appinfo.getActivityName();
    -            var cmd = 'adb -s ' + resolvedTarget.target + ' shell am start -W -a android.intent.action.MAIN -n ' + launchName;
    -            return exec(cmd, os.tmpdir());
    -        }).then(function(output) {
    -            console.log('LAUNCH SUCCESS');
    -        }, function(err) {
    -            return Q.reject('Failed to launch app on emulator: ' + err);
             });
    +
    +    // unlock screen
    +    }).then(function () {
    +
    +        console.log('Unlocking screen...');
    +        return exec('adb -s ' + target.target + ' shell input keyevent 82', os.tmpdir());
    +
    +    // launch the application
    --- End diff --
    
    The comment should be added when the console log gets removed in that case. Following the DRY principle - Don't Repeat Yourself.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by dblotsky <gi...@git.apache.org>.
Github user dblotsky commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r32090933
  
    --- Diff: bin/templates/cordova/lib/retry.js ---
    @@ -0,0 +1,63 @@
    +#!/usr/bin/env node
    +
    +/*
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +      http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +/* jshint node: true */
    +
    +"use strict";
    +
    +/*
    + * Retry a promise-returning function attemts_left times, propagating its
    + * results on success or throwing its error on a failed final attempt.
    + *
    + * Takes a number of attempts, a promise-returning function, and all its
    + * arguments (as trailing arguments).
    + */
    +module.exports.retryPromise = function (attemts_left, promiseFunction) {
    +
    +    // NOTE:
    +    //      get all trailing arguments, by skipping the first two (attemts_left and
    +    //      promiseFunction) because they shouldn't get passed to promiseFunction
    +    var promiseFunctionArguments = Array.prototype.slice.call(arguments, 2);
    +
    +    return promiseFunction.apply(undefined, promiseFunctionArguments).then(
    +
    +        // on success pass results through
    +        function onFulfilled(value) {
    +            return value;
    +        },
    +
    +        // on rejection either retry, or throw the error
    +        function onRejected(error) {
    +
    +            attemts_left -= 1;
    +
    +            if (attemts_left < 1) {
    +                throw error;
    +            }
    +
    +            console.log("call failed; retrying " + attemts_left + " more time(s)");
    --- End diff --
    
    Added capitals and periods.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r31975924
  
    --- Diff: bin/templates/cordova/lib/exec.js ---
    @@ -19,16 +19,42 @@
            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 (options === undefined) {
    +        options = {};
    --- End diff --
    
    Is this changing the behavior of providing max buffer when no options are specified?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r31976074
  
    --- Diff: bin/templates/cordova/lib/retry.js ---
    @@ -0,0 +1,63 @@
    +#!/usr/bin/env node
    +
    +/*
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +      http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +/* jshint node: true */
    +
    +"use strict";
    +
    +/*
    + * Retry a promise-returning function attemts_left times, propagating its
    + * results on success or throwing its error on a failed final attempt.
    + *
    + * Takes a number of attempts, a promise-returning function, and all its
    + * arguments (as trailing arguments).
    + */
    +module.exports.retryPromise = function (attemts_left, promiseFunction) {
    --- End diff --
    
    Consider writing unit tests for this one as it is fairly easy and isolated to write for this function.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by dblotsky <gi...@git.apache.org>.
Github user dblotsky commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r31977783
  
    --- Diff: bin/templates/cordova/lib/emulator.js ---
    @@ -298,37 +306,67 @@ module.exports.resolveTarget = function(target) {
      * If no started emulators are found, error out.
      * Returns a promise.
      */
    -module.exports.install = function(target, buildResults) {
    -    return Q().then(function() {
    -        if (target && typeof target == 'object') {
    -            return target;
    +module.exports.install = function(givenTarget, buildResults) {
    +
    +    var target;
    +
    +    // resolve the target emulator
    +    return Q().then(function () {
    +        if (givenTarget && typeof givenTarget == 'object') {
    +            return givenTarget;
    +        } else {
    +            return module.exports.resolveTarget(givenTarget);
             }
    -        return module.exports.resolveTarget(target);
    -    }).then(function(resolvedTarget) {
    -        var apk_path = build.findBestApkForArchitecture(buildResults, resolvedTarget.arch);
    +
    +    // set the resolved target
    +    }).then(function (resolvedTarget) {
    +        target = resolvedTarget;
    +
    +    // install the app
    +    }).then(function () {
    +
    +        var apk_path    = build.findBestApkForArchitecture(buildResults, target.arch);
    +        var execOptions = {
    +            timeout:    INSTALL_COMMAND_TIMEOUT, // in milliseconds
    +            killSignal: EXEC_KILL_SIGNAL
    +        };
    +
             console.log('Installing app on emulator...');
             console.log('Using apk: ' + apk_path);
    -        return exec('adb -s ' + resolvedTarget.target + ' install -r -d "' + apk_path + '"', os.tmpdir())
    -        .then(function(output) {
    +
    +        var retriedInstall = retry.retryPromise(
    +            NUM_INSTALL_RETRIES,
    +            exec, 'adb -s ' + target.target + ' install -r -d "' + apk_path + '"', os.tmpdir(), execOptions
    +        );
    +
    +        return retriedInstall.then(function (output) {
                 if (output.match(/Failure/)) {
                     return Q.reject('Failed to install apk to emulator: ' + output);
    +            } else {
    +                console.log('INSTALL SUCCESS');
                 }
    -            return Q();
    -        }, function(err) {
    +        }, function (err) {
                 return Q.reject('Failed to install apk to emulator: ' + err);
    -        }).then(function() {
    -            //unlock screen
    -            return exec('adb -s ' + resolvedTarget.target + ' shell input keyevent 82', os.tmpdir());
    -        }).then(function() {
    -            // launch the application
    -            console.log('Launching application...');
    -            var launchName = appinfo.getActivityName();
    -            var cmd = 'adb -s ' + resolvedTarget.target + ' shell am start -W -a android.intent.action.MAIN -n ' + launchName;
    -            return exec(cmd, os.tmpdir());
    -        }).then(function(output) {
    -            console.log('LAUNCH SUCCESS');
    -        }, function(err) {
    -            return Q.reject('Failed to launch app on emulator: ' + err);
             });
    +
    +    // unlock screen
    +    }).then(function () {
    +
    +        console.log('Unlocking screen...');
    +        return exec('adb -s ' + target.target + ' shell input keyevent 82', os.tmpdir());
    +
    +    // launch the application
    --- End diff --
    
    If the console logs get removed (for example, to make the function less verbose), the comments will remain.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/cordova-android/pull/180


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r31976044
  
    --- Diff: bin/templates/cordova/lib/emulator.js ---
    @@ -298,37 +306,67 @@ module.exports.resolveTarget = function(target) {
      * If no started emulators are found, error out.
      * Returns a promise.
      */
    -module.exports.install = function(target, buildResults) {
    -    return Q().then(function() {
    -        if (target && typeof target == 'object') {
    -            return target;
    +module.exports.install = function(givenTarget, buildResults) {
    +
    +    var target;
    +
    +    // resolve the target emulator
    +    return Q().then(function () {
    +        if (givenTarget && typeof givenTarget == 'object') {
    +            return givenTarget;
    +        } else {
    +            return module.exports.resolveTarget(givenTarget);
             }
    -        return module.exports.resolveTarget(target);
    -    }).then(function(resolvedTarget) {
    -        var apk_path = build.findBestApkForArchitecture(buildResults, resolvedTarget.arch);
    +
    +    // set the resolved target
    +    }).then(function (resolvedTarget) {
    +        target = resolvedTarget;
    +
    +    // install the app
    +    }).then(function () {
    +
    +        var apk_path    = build.findBestApkForArchitecture(buildResults, target.arch);
    +        var execOptions = {
    +            timeout:    INSTALL_COMMAND_TIMEOUT, // in milliseconds
    +            killSignal: EXEC_KILL_SIGNAL
    +        };
    +
             console.log('Installing app on emulator...');
             console.log('Using apk: ' + apk_path);
    -        return exec('adb -s ' + resolvedTarget.target + ' install -r -d "' + apk_path + '"', os.tmpdir())
    -        .then(function(output) {
    +
    +        var retriedInstall = retry.retryPromise(
    +            NUM_INSTALL_RETRIES,
    +            exec, 'adb -s ' + target.target + ' install -r -d "' + apk_path + '"', os.tmpdir(), execOptions
    +        );
    +
    +        return retriedInstall.then(function (output) {
                 if (output.match(/Failure/)) {
                     return Q.reject('Failed to install apk to emulator: ' + output);
    +            } else {
    +                console.log('INSTALL SUCCESS');
                 }
    -            return Q();
    -        }, function(err) {
    +        }, function (err) {
                 return Q.reject('Failed to install apk to emulator: ' + err);
    -        }).then(function() {
    -            //unlock screen
    -            return exec('adb -s ' + resolvedTarget.target + ' shell input keyevent 82', os.tmpdir());
    -        }).then(function() {
    -            // launch the application
    -            console.log('Launching application...');
    -            var launchName = appinfo.getActivityName();
    -            var cmd = 'adb -s ' + resolvedTarget.target + ' shell am start -W -a android.intent.action.MAIN -n ' + launchName;
    -            return exec(cmd, os.tmpdir());
    -        }).then(function(output) {
    -            console.log('LAUNCH SUCCESS');
    -        }, function(err) {
    -            return Q.reject('Failed to launch app on emulator: ' + err);
             });
    +
    +    // unlock screen
    +    }).then(function () {
    +
    +        console.log('Unlocking screen...');
    +        return exec('adb -s ' + target.target + ' shell input keyevent 82', os.tmpdir());
    +
    +    // launch the application
    --- End diff --
    
    Nit: I liked the placement of comments earlier - though - given we are emitting console log messages with the same text  - these comments are somewhat redundant.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by dblotsky <gi...@git.apache.org>.
Github user dblotsky commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r31977946
  
    --- Diff: bin/templates/cordova/lib/retry.js ---
    @@ -0,0 +1,63 @@
    +#!/usr/bin/env node
    +
    +/*
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +      http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +/* jshint node: true */
    +
    +"use strict";
    +
    +/*
    + * Retry a promise-returning function attemts_left times, propagating its
    + * results on success or throwing its error on a failed final attempt.
    + *
    + * Takes a number of attempts, a promise-returning function, and all its
    + * arguments (as trailing arguments).
    + */
    +module.exports.retryPromise = function (attemts_left, promiseFunction) {
    +
    +    // NOTE:
    +    //      get all trailing arguments, by skipping the first two (attemts_left and
    +    //      promiseFunction) because they shouldn't get passed to promiseFunction
    +    var promiseFunctionArguments = Array.prototype.slice.call(arguments, 2);
    +
    +    return promiseFunction.apply(undefined, promiseFunctionArguments).then(
    +
    +        // on success pass results through
    +        function onFulfilled(value) {
    +            return value;
    +        },
    +
    +        // on rejection either retry, or throw the error
    +        function onRejected(error) {
    +
    +            attemts_left -= 1;
    +
    +            if (attemts_left < 1) {
    +                throw error;
    +            }
    +
    +            console.log("call failed; retrying " + attemts_left + " more time(s)");
    --- End diff --
    
    I'm conflicted on whether this log message should exist. It's there now because I chose to be more verbose rather than less verbose, but I'm not sure that it belongs here in general. If this function gets used in other places, these messages will quickly get out of hand. What's Cordova's way to print verbose logs only when asked?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r32178275
  
    --- Diff: bin/templates/cordova/lib/emulator.js ---
    @@ -298,37 +306,67 @@ module.exports.resolveTarget = function(target) {
      * If no started emulators are found, error out.
      * Returns a promise.
      */
    -module.exports.install = function(target, buildResults) {
    -    return Q().then(function() {
    -        if (target && typeof target == 'object') {
    -            return target;
    +module.exports.install = function(givenTarget, buildResults) {
    +
    +    var target;
    +
    +    // resolve the target emulator
    +    return Q().then(function () {
    +        if (givenTarget && typeof givenTarget == 'object') {
    +            return givenTarget;
    +        } else {
    +            return module.exports.resolveTarget(givenTarget);
             }
    -        return module.exports.resolveTarget(target);
    -    }).then(function(resolvedTarget) {
    -        var apk_path = build.findBestApkForArchitecture(buildResults, resolvedTarget.arch);
    +
    +    // set the resolved target
    +    }).then(function (resolvedTarget) {
    +        target = resolvedTarget;
    +
    +    // install the app
    +    }).then(function () {
    +
    +        var apk_path    = build.findBestApkForArchitecture(buildResults, target.arch);
    +        var execOptions = {
    +            timeout:    INSTALL_COMMAND_TIMEOUT, // in milliseconds
    +            killSignal: EXEC_KILL_SIGNAL
    +        };
    +
             console.log('Installing app on emulator...');
             console.log('Using apk: ' + apk_path);
    -        return exec('adb -s ' + resolvedTarget.target + ' install -r -d "' + apk_path + '"', os.tmpdir())
    -        .then(function(output) {
    +
    +        var retriedInstall = retry.retryPromise(
    +            NUM_INSTALL_RETRIES,
    +            exec, 'adb -s ' + target.target + ' install -r -d "' + apk_path + '"', os.tmpdir(), execOptions
    +        );
    +
    +        return retriedInstall.then(function (output) {
                 if (output.match(/Failure/)) {
                     return Q.reject('Failed to install apk to emulator: ' + output);
    +            } else {
    +                console.log('INSTALL SUCCESS');
                 }
    -            return Q();
    -        }, function(err) {
    +        }, function (err) {
                 return Q.reject('Failed to install apk to emulator: ' + err);
    -        }).then(function() {
    -            //unlock screen
    -            return exec('adb -s ' + resolvedTarget.target + ' shell input keyevent 82', os.tmpdir());
    -        }).then(function() {
    -            // launch the application
    -            console.log('Launching application...');
    -            var launchName = appinfo.getActivityName();
    -            var cmd = 'adb -s ' + resolvedTarget.target + ' shell am start -W -a android.intent.action.MAIN -n ' + launchName;
    -            return exec(cmd, os.tmpdir());
    -        }).then(function(output) {
    -            console.log('LAUNCH SUCCESS');
    -        }, function(err) {
    -            return Q.reject('Failed to launch app on emulator: ' + err);
             });
    +
    +    // unlock screen
    +    }).then(function () {
    +
    +        console.log('Unlocking screen...');
    +        return exec('adb -s ' + target.target + ' shell input keyevent 82', os.tmpdir());
    +
    +    // launch the application
    --- End diff --
    
    It applies two any n things you need to keep in sync - be it code/comments/documentation etc. Anyway, this duplication is too trivial to make any measurable difference and it's best to move forward.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r32027602
  
    --- Diff: bin/templates/cordova/lib/retry.js ---
    @@ -0,0 +1,63 @@
    +#!/usr/bin/env node
    +
    +/*
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +      http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +/* jshint node: true */
    +
    +"use strict";
    +
    +/*
    + * Retry a promise-returning function attemts_left times, propagating its
    + * results on success or throwing its error on a failed final attempt.
    + *
    + * Takes a number of attempts, a promise-returning function, and all its
    + * arguments (as trailing arguments).
    + */
    +module.exports.retryPromise = function (attemts_left, promiseFunction) {
    +
    +    // NOTE:
    +    //      get all trailing arguments, by skipping the first two (attemts_left and
    +    //      promiseFunction) because they shouldn't get passed to promiseFunction
    +    var promiseFunctionArguments = Array.prototype.slice.call(arguments, 2);
    +
    +    return promiseFunction.apply(undefined, promiseFunctionArguments).then(
    +
    +        // on success pass results through
    +        function onFulfilled(value) {
    +            return value;
    +        },
    +
    +        // on rejection either retry, or throw the error
    +        function onRejected(error) {
    +
    +            attemts_left -= 1;
    +
    +            if (attemts_left < 1) {
    +                throw error;
    +            }
    +
    +            console.log("call failed; retrying " + attemts_left + " more time(s)");
    --- End diff --
    
    There is no unified way to do verbose logging - every platform & cordova-lib does it a little bit differently. I have not seen any code in Android platform doing it altogether. If you decide to keep these messages, consider reformatting.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r31976180
  
    --- Diff: bin/templates/cordova/lib/retry.js ---
    @@ -0,0 +1,63 @@
    +#!/usr/bin/env node
    +
    +/*
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +      http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +/* jshint node: true */
    +
    +"use strict";
    +
    +/*
    + * Retry a promise-returning function attemts_left times, propagating its
    + * results on success or throwing its error on a failed final attempt.
    + *
    + * Takes a number of attempts, a promise-returning function, and all its
    + * arguments (as trailing arguments).
    + */
    +module.exports.retryPromise = function (attemts_left, promiseFunction) {
    +
    +    // NOTE:
    +    //      get all trailing arguments, by skipping the first two (attemts_left and
    +    //      promiseFunction) because they shouldn't get passed to promiseFunction
    +    var promiseFunctionArguments = Array.prototype.slice.call(arguments, 2);
    +
    +    return promiseFunction.apply(undefined, promiseFunctionArguments).then(
    +
    +        // on success pass results through
    +        function onFulfilled(value) {
    +            return value;
    +        },
    +
    +        // on rejection either retry, or throw the error
    +        function onRejected(error) {
    +
    +            attemts_left -= 1;
    +
    +            if (attemts_left < 1) {
    +                throw error;
    +            }
    +
    +            console.log("call failed; retrying " + attemts_left + " more time(s)");
    --- End diff --
    
    Nit. use full stop instead of semicolon. This is not code but message to users and start the message with a capital letter.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by dblotsky <gi...@git.apache.org>.
Github user dblotsky commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r32090785
  
    --- Diff: bin/templates/cordova/lib/emulator.js ---
    @@ -298,37 +306,67 @@ module.exports.resolveTarget = function(target) {
      * If no started emulators are found, error out.
      * Returns a promise.
      */
    -module.exports.install = function(target, buildResults) {
    -    return Q().then(function() {
    -        if (target && typeof target == 'object') {
    -            return target;
    +module.exports.install = function(givenTarget, buildResults) {
    +
    +    var target;
    +
    +    // resolve the target emulator
    +    return Q().then(function () {
    +        if (givenTarget && typeof givenTarget == 'object') {
    +            return givenTarget;
    +        } else {
    +            return module.exports.resolveTarget(givenTarget);
             }
    -        return module.exports.resolveTarget(target);
    -    }).then(function(resolvedTarget) {
    -        var apk_path = build.findBestApkForArchitecture(buildResults, resolvedTarget.arch);
    +
    +    // set the resolved target
    +    }).then(function (resolvedTarget) {
    +        target = resolvedTarget;
    +
    +    // install the app
    +    }).then(function () {
    +
    +        var apk_path    = build.findBestApkForArchitecture(buildResults, target.arch);
    +        var execOptions = {
    +            timeout:    INSTALL_COMMAND_TIMEOUT, // in milliseconds
    +            killSignal: EXEC_KILL_SIGNAL
    +        };
    +
             console.log('Installing app on emulator...');
             console.log('Using apk: ' + apk_path);
    -        return exec('adb -s ' + resolvedTarget.target + ' install -r -d "' + apk_path + '"', os.tmpdir())
    -        .then(function(output) {
    +
    +        var retriedInstall = retry.retryPromise(
    +            NUM_INSTALL_RETRIES,
    +            exec, 'adb -s ' + target.target + ' install -r -d "' + apk_path + '"', os.tmpdir(), execOptions
    +        );
    +
    +        return retriedInstall.then(function (output) {
                 if (output.match(/Failure/)) {
                     return Q.reject('Failed to install apk to emulator: ' + output);
    +            } else {
    +                console.log('INSTALL SUCCESS');
                 }
    -            return Q();
    -        }, function(err) {
    +        }, function (err) {
                 return Q.reject('Failed to install apk to emulator: ' + err);
    -        }).then(function() {
    -            //unlock screen
    -            return exec('adb -s ' + resolvedTarget.target + ' shell input keyevent 82', os.tmpdir());
    -        }).then(function() {
    -            // launch the application
    -            console.log('Launching application...');
    -            var launchName = appinfo.getActivityName();
    -            var cmd = 'adb -s ' + resolvedTarget.target + ' shell am start -W -a android.intent.action.MAIN -n ' + launchName;
    -            return exec(cmd, os.tmpdir());
    -        }).then(function(output) {
    -            console.log('LAUNCH SUCCESS');
    -        }, function(err) {
    -            return Q.reject('Failed to launch app on emulator: ' + err);
             });
    +
    +    // unlock screen
    +    }).then(function () {
    +
    +        console.log('Unlocking screen...');
    +        return exec('adb -s ' + target.target + ' shell input keyevent 82', os.tmpdir());
    +
    +    // launch the application
    --- End diff --
    
    Don't Repeat Yourself applies to repeated code or logic, not to comments. This comment improves readability because you can clearly see what every `then` block does by looking only at the comment above it, and it is therefore useful.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by dblotsky <gi...@git.apache.org>.
Github user dblotsky commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r32090950
  
    --- Diff: bin/templates/cordova/lib/exec.js ---
    @@ -19,16 +19,42 @@
            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 (options === undefined) {
    +        options = {};
    +    }
    +
    +    // override cwd to preserve old opt_cwd behavior
    +    options.cwd = opt_cwd;
    +
    +    // set maxBuffer
    +    if (options.maxBuffer === undefined) {
    --- End diff --
    
    Now using `typeof options.maxBuffer === "undefined"` instead.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r32027163
  
    --- Diff: bin/templates/cordova/lib/exec.js ---
    @@ -19,16 +19,42 @@
            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 (options === undefined) {
    +        options = {};
    +    }
    +
    +    // override cwd to preserve old opt_cwd behavior
    +    options.cwd = opt_cwd;
    +
    +    // set maxBuffer
    +    if (options.maxBuffer === undefined) {
    --- End diff --
    
    Comparing to undefined is a bad practice: http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by dblotsky <gi...@git.apache.org>.
Github user dblotsky commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r31978041
  
    --- Diff: bin/templates/cordova/lib/exec.js ---
    @@ -19,16 +19,42 @@
            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 (options === undefined) {
    +        options = {};
    --- End diff --
    
    Previously, a hard-coded `maxBuffer` was always passed to `child_process.exec`. Now, it's passed as a default when it's not specified in a given `options.maxBuffer`. Otherwise, the given value is respected.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by dblotsky <gi...@git.apache.org>.
Github user dblotsky commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r31977846
  
    --- Diff: bin/templates/cordova/lib/retry.js ---
    @@ -0,0 +1,63 @@
    +#!/usr/bin/env node
    +
    +/*
    +    Licensed to the Apache Software Foundation (ASF) under one
    +    or more contributor license agreements.  See the NOTICE file
    +    distributed with this work for additional information
    +    regarding copyright ownership.  The ASF licenses this file
    +    to you under the Apache License, Version 2.0 (the
    +    "License"); you may not use this file except in compliance
    +    with the License.  You may obtain a copy of the License at
    +
    +      http://www.apache.org/licenses/LICENSE-2.0
    +
    +    Unless required by applicable law or agreed to in writing,
    +    software distributed under the License is distributed on an
    +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    +    KIND, either express or implied.  See the License for the
    +    specific language governing permissions and limitations
    +    under the License.
    +*/
    +
    +/* jshint node: true */
    +
    +"use strict";
    +
    +/*
    + * Retry a promise-returning function attemts_left times, propagating its
    + * results on success or throwing its error on a failed final attempt.
    + *
    + * Takes a number of attempts, a promise-returning function, and all its
    + * arguments (as trailing arguments).
    + */
    +module.exports.retryPromise = function (attemts_left, promiseFunction) {
    --- End diff --
    
    Good idea. I created a JIRA task for this.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on the pull request:

    https://github.com/apache/cordova-android/pull/180#issuecomment-110188933
  
    Mostly looks good - some stylistic comments


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org


[GitHub] cordova-android pull request: CB-9119 Fixing intermittent 'adb ins...

Posted by nikhilkh <gi...@git.apache.org>.
Github user nikhilkh commented on a diff in the pull request:

    https://github.com/apache/cordova-android/pull/180#discussion_r32027291
  
    --- Diff: bin/templates/cordova/lib/exec.js ---
    @@ -19,16 +19,42 @@
            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 (options === undefined) {
    +        options = {};
    --- End diff --
    
    Makes sense. I see that now.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org
For additional commands, e-mail: dev-help@cordova.apache.org