Skip to content

Instantly share code, notes, and snippets.

@schlos
Forked from peterherrmann/outerLoop.js
Created April 30, 2020 19:53
Show Gist options
  • Save schlos/3d0a9c489c3f233439d02f56f321a747 to your computer and use it in GitHub Desktop.
Save schlos/3d0a9c489c3f233439d02f56f321a747 to your computer and use it in GitHub Desktop.

Revisions

  1. @peterherrmann peterherrmann revised this gist Feb 1, 2017. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions outerLoop.js
    Original file line number Diff line number Diff line change
    @@ -46,9 +46,7 @@ function outerLoop() {
    Logger.info('Completed processing all %d things with the "%s" function', thingies.length, arguments.callee.name);
    }
    } catch (e) {
    e = (typeof e === 'string') ? new Error(e) : e;
    Logger.severe('%s: %s (line %s, file "%s"). Stack: "%s" . While processing %s.',e.name||'',
    e.message||'', e.lineNumber||'', e.fileName||'', e.stack||'', processingMessage||'');
    Logger.severe('%s. While processing %s', JSON.stringify(e, null, 2), processingMessage);
    throw e;
    }
    }
  2. @peterherrmann peterherrmann revised this gist Oct 20, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion outerLoop.js
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ function outerLoop() {
    var start = parseInt(PropertiesService.getScriptProperties().getProperty(arguments.callee.name + "-start")) || 0;

    var thingies = ['stuff to process', 'in an Array',,,,]; //
    for (var i = start ; i < i.length; i++) {
    for (var i = start ; i < thingies.length; i++) {
    if (Math.round((new Date() - startTime)/1000) > 300) { //360 seconds is Google Apps Script max run time
    //We've hit max runtime.
    isOverMaxRuntime = true;
  3. @peterherrmann peterherrmann revised this gist Oct 20, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion outerLoop.js
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ function outerLoop() {
    var start = parseInt(PropertiesService.getScriptProperties().getProperty(arguments.callee.name + "-start")) || 0;

    var thingies = ['stuff to process', 'in an Array',,,,]; //
    for (var i = start ; i < 5; i++) {
    for (var i = start ; i < i.length; i++) {
    if (Math.round((new Date() - startTime)/1000) > 300) { //360 seconds is Google Apps Script max run time
    //We've hit max runtime.
    isOverMaxRuntime = true;
  4. @peterherrmann peterherrmann revised this gist Oct 20, 2014. 1 changed file with 41 additions and 21 deletions.
    62 changes: 41 additions & 21 deletions outerLoop.js
    Original file line number Diff line number Diff line change
    @@ -1,38 +1,58 @@
    //load configuration details and start logging - creates and sets up sheets the first time they are run
    var CONFIG_SPREADSHEET_KEY = '<ssid_goes_here>';
    var Config = SettingsManager.load(CONFIG_SPREADSHEET_KEY); //Add Mafviu9bMfg9xVu21LGfpWnHAGDwXQ1CH in Resources > Libraries
    Logger = BetterLog.useSpreadsheet(Config['logSpreadsheetId'].value);//Add MYB7yzedMbnJaMKECt6Sm7FLDhaBgl_dE in Resources > Libraries

    // trigger this function
    function outerLoop() {
    try {
    var MAX_RUNTIME = 300; //360 is Google max run time so we use a little less than that so we can save state
    isOverMaxRuntime = false,
    startTime = new Date(); // to calc elapsed time
    // to calc elapsed time
    var isOverMaxRuntime = false,
    startTime = new Date();

    //we want to process this stuff - your code goes here
    thingies = getAnArrayOfStuffToProcess();
    // Deletes all occurrences of the Repeating trigger we don't end up with undeleted time based triggers all over the place
    ScriptApp.getProjectTriggers().forEach(function(i) {
    if (i.getHandlerFunction()==='outerLoopRepeating') {ScriptApp.deleteTrigger(i);}
    });

    // Get the start index in the case where we hit the max run time last time we ran
    var i = parseInt(ScriptProperties.getProperty(arguments.callee.name + "-start")) || 0;
    for (i; i < thingies.length; i++) {
    if (Math.round((new Date() - startTime)/1000) > MAX_RUNTIME) {
    //Logger.finer('Entering the "%s" function', arguments.callee.name);

    // Handle max execution times in our outer loop
    // Get start index if we hit max execution time last run
    var start = parseInt(PropertiesService.getScriptProperties().getProperty(arguments.callee.name + "-start")) || 0;

    var thingies = ['stuff to process', 'in an Array',,,,]; //
    for (var i = start ; i < 5; i++) {
    if (Math.round((new Date() - startTime)/1000) > 300) { //360 seconds is Google Apps Script max run time
    //We've hit max runtime.
    isOverMaxRuntime = true;
    break;
    }
    //do our work here - your code goes here
    Logger.log('Inside the for loop that does the xyz work. i is currently: %s', i);
    //do our work here
    Logger.finest('Inside the for loop that does the xyz work. i is currently: %d', i);
    var processingMessage = Utilities.formatString('%d of %d thingies: %s <%s>', i+1, thingies.length, thingyName, thingyId);
    //do our work above this line

    //do our work above here
    }
    if (isOverMaxRuntime) {
    //save state in script/user properties
    ScriptProperties.setProperty(arguments.callee.name + '-start', i); //the function name plus -start
    Logger.log('Hit max run time - last iteration completed was i=%s', i-1);
    //save state in user/project prop if required
    PropertiesService.getScriptProperties().setProperty(arguments.callee.name + '-start', i);
    //create another trigger
    ScriptApp.newTrigger('outerLoopRepeating').timeBased().everyMinutes(10).create();
    Logger.info('Hit max run time - last iteration completed was i=%d', i-1);
    } else {
    Logger.log('Done all the work and all iterations');
    ScriptProperties.deleteProperty(arguments.callee.name + '-start');
    Logger.log('Completed processing all %s things with the "%s" function', thingies.length, arguments.callee.name);
    Logger.fine('Done all the work and all iterations');
    PropertiesService.getScriptProperties().deleteProperty(arguments.callee.name + '-start');
    Logger.info('Completed processing all %d things with the "%s" function', thingies.length, arguments.callee.name);
    }
    } catch (e) { //with stack tracing if your exceptions bubble up to here
    } catch (e) {
    e = (typeof e === 'string') ? new Error(e) : e;
    Logger.log('%s: %s (line %s, file "%s"). Stack: "%s" . While processing %s.',e.name||'',
    Logger.severe('%s: %s (line %s, file "%s"). Stack: "%s" . While processing %s.',e.name||'',
    e.message||'', e.lineNumber||'', e.fileName||'', e.stack||'', processingMessage||'');
    throw e;
    }
    }
    }
    //automatically invoked from outerLoop()'s creation of a new trigger if required to get work done
    function outerLoopRepeating() {
    outerLoop();
    }
  5. @peterherrmann peterherrmann revised this gist Jan 11, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion outerLoop.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ function outerLoop() {

    // Get the start index in the case where we hit the max run time last time we ran
    var i = parseInt(ScriptProperties.getProperty(arguments.callee.name + "-start")) || 0;
    for (var i = 0; i < thingies.length; i++) {
    for (i; i < thingies.length; i++) {
    if (Math.round((new Date() - startTime)/1000) > MAX_RUNTIME) {
    //We've hit max runtime.
    isOverMaxRuntime = true;
  6. @peterherrmann peterherrmann revised this gist Jun 2, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion outerLoop.js
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ function outerLoop() {
    } catch (e) { //with stack tracing if your exceptions bubble up to here
    e = (typeof e === 'string') ? new Error(e) : e;
    Logger.log('%s: %s (line %s, file "%s"). Stack: "%s" . While processing %s.',e.name||'',
    e.message||'', e.lineNumber||'', e.fileName||'', e.stack||'', processingMessage);
    e.message||'', e.lineNumber||'', e.fileName||'', e.stack||'', processingMessage||'');
    throw e;
    }
    }
  7. @peterherrmann peterherrmann created this gist Jun 1, 2013.
    38 changes: 38 additions & 0 deletions outerLoop.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    function outerLoop() {
    try {
    var MAX_RUNTIME = 300; //360 is Google max run time so we use a little less than that so we can save state
    isOverMaxRuntime = false,
    startTime = new Date(); // to calc elapsed time

    //we want to process this stuff - your code goes here
    thingies = getAnArrayOfStuffToProcess();

    // Get the start index in the case where we hit the max run time last time we ran
    var i = parseInt(ScriptProperties.getProperty(arguments.callee.name + "-start")) || 0;
    for (var i = 0; i < thingies.length; i++) {
    if (Math.round((new Date() - startTime)/1000) > MAX_RUNTIME) {
    //We've hit max runtime.
    isOverMaxRuntime = true;
    break;
    }
    //do our work here - your code goes here
    Logger.log('Inside the for loop that does the xyz work. i is currently: %s', i);
    var processingMessage = Utilities.formatString('%d of %d thingies: %s <%s>', i+1, thingies.length, thingyName, thingyId);
    //do our work above this line
    }
    if (isOverMaxRuntime) {
    //save state in script/user properties
    ScriptProperties.setProperty(arguments.callee.name + '-start', i); //the function name plus -start
    Logger.log('Hit max run time - last iteration completed was i=%s', i-1);
    } else {
    Logger.log('Done all the work and all iterations');
    ScriptProperties.deleteProperty(arguments.callee.name + '-start');
    Logger.log('Completed processing all %s things with the "%s" function', thingies.length, arguments.callee.name);
    }
    } catch (e) { //with stack tracing if your exceptions bubble up to here
    e = (typeof e === 'string') ? new Error(e) : e;
    Logger.log('%s: %s (line %s, file "%s"). Stack: "%s" . While processing %s.',e.name||'',
    e.message||'', e.lineNumber||'', e.fileName||'', e.stack||'', processingMessage);
    throw e;
    }
    }