Last active
July 6, 2024 10:27
-
-
Save peterherrmann/2700284 to your computer and use it in GitHub Desktop.
GASRetry - Exponential backoff JavaScript implementation for Google Apps Script (Library project key: "MGJu3PS2ZYnANtJ9kyn2vnlLDhaBgl_dE")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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: https://developers.google.com/google-apps/documents-list/#implementing_exponential_backoff | |
<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} 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 call(func, optLoggerFunction) { | |
for (var n=0; n<6; n++) { | |
try { | |
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))); | |
} | |
} | |
} |
Using this library makes autocomplete to dissapear.. Any idea how to get it back ?
Ex:
var ss = call(function(){return SpreadsheetApp.getActiveSpreadsheet()})
var sh = ss.// no autocomplete available here
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I'm triyng to write this retry loop into the code below but I can't seem to get it working. Please see below. Any help would be greatly appreciated as I've beeb wrapping my head around this for days now with no luck. I've left it clean without any changes to show the original script. The error I keep getting is '(TypeError: Cannot call method "getChild" of null.' and I want to retry the function to reduce the error:
function update_(trigger) {
try {
var response,
image,
count = 0,
last = false,
data = productSheet.getDataRange().getValues();
} catch (e) {
Logger.log(e.toString());
}
}
function call_(func) {
for (var n = 0; n < 6; n++) {
try {
return func();
} catch (e) {
if (n == 5) {
throw e;
}
Utilities.sleep(Math.pow(2, n) * 1000 + Math.round(Math.random() * 1000));
}
}
}