Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save schlos/b837c70b689a5317c346b1caebb5b1b7 to your computer and use it in GitHub Desktop.
Save schlos/b837c70b689a5317c346b1caebb5b1b7 to your computer and use it in GitHub Desktop.

Revisions

  1. @peterherrmann peterherrmann revised this gist Sep 22, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,6 @@
    * Retries with delays of approximately 1, 2, 4, 8 then 16 seconds for a total of
    * about 32 seconds before it gives up and rethrows the last error.
    * See: https://developers.google.com/google-apps/documents-list/#implementing_exponential_backoff
    * <br>Author: [email protected] (Peter Herrmann)
    <h3>Examples:</h3>
    <pre>//Calls an anonymous function that concatenates a greeting with the current Apps user's email
    var example1 = GASRetry.call(function(){return "Hello, " + Session.getActiveUser().getEmail();});
  2. @peterherrmann peterherrmann revised this gist Jul 26, 2012. No changes.
  3. @peterherrmann peterherrmann revised this gist Jul 26, 2012. 1 changed file with 23 additions and 17 deletions.
    40 changes: 23 additions & 17 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,29 +1,35 @@
    /**
    * Invokes a function, performing up to 5 retries with exponential backoff
    * It will retry with delays of approximately 1, 2, 4, 8 then 16 seconds for a total of
    * Invokes a function, performing up to 5 retries with exponential backoff.
    * Retries with delays of approximately 1, 2, 4, 8 then 16 seconds for a total of
    * about 32 seconds before it gives up and rethrows the last error.
    * @see http://googleappsdeveloper.blogspot.com.au/2011/12/documents-list-api-best-practices.html
    * Example:
    *
    * <pre>
    * var test = gasCall(function(){return "Hello, " + Session.getActiveUser().getEmail();});
    * </pre>
    * See: https://developers.google.com/google-apps/documents-list/#implementing_exponential_backoff
    * <br>Author: [email protected] (Peter Herrmann)
    <h3>Examples:</h3>
    <pre>//Calls an anonymous function that concatenates a greeting with the current Apps user's email
    var example1 = GASRetry.call(function(){return "Hello, " + Session.getActiveUser().getEmail();});
    </pre><pre>//Calls an existing function
    var example2 = GASRetry.call(myFunction);
    </pre><pre>//Calls an anonymous function that calls an existing function with an argument
    var example3 = GASRetry.call(function(){myFunction("something")});
    </pre><pre>//Calls an anonymous function that invokes DocsList.setTrashed on myFile and logs retries with the Logger.log function.
    var example4 = GASRetry.call(function(){myFile.setTrashed(true)}, Logger.log);
    </pre>
    *
    * @param {Function} function The anonymous or named function to call
    *
    * @returns The results of the called function
    *
    * @author [email protected] (Peter Herrmann)
    * @param {Function} func The anonymous or named function to call.
    * @param {Function} optLoggerFunction Optionally, you can pass a function that will be used to log
    to in the case of a retry. For example, Logger.log (no parentheses) will work.
    * @return {*} The value returned by the called function.
    */
    function gasCall(f) {
    function call(func, optLoggerFunction) {
    for (var n=0; n<6; n++) {
    try {
    return f();
    return func();
    } catch(e) {
    if (optLoggerFunction) {optLoggerFunction("GASRetry " + n + ": " + e)}
    if (n == 5) {
    throw e;
    }
    }
    Utilities.sleep((Math.pow(2,n)*1000) + (Math.round(Math.random() * 1000)));
    }
    }
    }
    }
  4. @peterherrmann peterherrmann revised this gist Jun 1, 2012. 1 changed file with 23 additions and 21 deletions.
    44 changes: 23 additions & 21 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,29 @@
    /*
    Here's my Google Apps Script implementation of the exponential backoff pattern presented
    in the Google Developer Blog [1] and covered in the Google Apps developer docs [2].
    It will retry with delays of approximately 1, 2, 4, 8 then 16 seconds for a total of
    about 32 seconds before gives up and rethrows the last error.
    [1] http://googleappsdeveloper.blogspot.com.au/2011/12/documents-list-api-best-practices.html
    [2] https://developers.google.com/google-apps/documents-list/#implementing_exponential_backoff
    /**
    * Invokes a function, performing up to 5 retries with exponential backoff
    * It will retry with delays of approximately 1, 2, 4, 8 then 16 seconds for a total of
    * about 32 seconds before it gives up and rethrows the last error.
    * @see http://googleappsdeveloper.blogspot.com.au/2011/12/documents-list-api-best-practices.html
    * Example:
    *
    * <pre>
    * var test = gasCall(function(){return "Hello, " + Session.getActiveUser().getEmail();});
    * </pre>
    *
    * @param {Function} function The anonymous or named function to call
    *
    * @returns The results of the called function
    *
    * @author [email protected] (Peter Herrmann)
    */

    //You can use like this:
    var files = apiCallWithExponentialBackoff("DocsList.getFiles(300,100);");
    if (files){
    // do stuff
    }

    function apiCallWithExponentialBackoff(callString) {
    for (var n=0; n < 6; n++) {
    function gasCall(f) {
    for (var n=0; n<6; n++) {
    try {
    return eval(callString);
    return f();
    } catch(e) {
    if (n === 5) {
    throw e; //rethrow after 5th retry
    }
    if (n == 5) {
    throw e;
    }
    Utilities.sleep((Math.pow(2,n)*1000) + (Math.round(Math.random() * 1000)));
    }
    }
    }
    }
  5. @peterherrmann peterherrmann created this gist May 15, 2012.
    27 changes: 27 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    /*
    Here's my Google Apps Script implementation of the exponential backoff pattern presented
    in the Google Developer Blog [1] and covered in the Google Apps developer docs [2].
    It will retry with delays of approximately 1, 2, 4, 8 then 16 seconds for a total of
    about 32 seconds before gives up and rethrows the last error.
    [1] http://googleappsdeveloper.blogspot.com.au/2011/12/documents-list-api-best-practices.html
    [2] https://developers.google.com/google-apps/documents-list/#implementing_exponential_backoff
    */

    //You can use like this:
    var files = apiCallWithExponentialBackoff("DocsList.getFiles(300,100);");
    if (files){
    // do stuff
    }

    function apiCallWithExponentialBackoff(callString) {
    for (var n=0; n < 6; n++) {
    try {
    return eval(callString);
    } catch(e) {
    if (n === 5) {
    throw e; //rethrow after 5th retry
    }
    Utilities.sleep((Math.pow(2,n)*1000) + (Math.round(Math.random() * 1000)));
    }
    }
    }