简体   繁体   中英

Execution Timeout for Google Apps Script

I'm currently writing a script that copies a folder, and all of its sub-folders and files of a very large directory from one location in Google Drive to another. This is being used for archiving purposes for the company which I am employed by.

My issue is that the size and quantity of the file that I am trying to archive is WAY too large to handle in the 5 minute execution time given by Google. I am trying to keep this script as a standalone web app, however I am happy to extend on it as needed.

My second issue is that I need the script to run over and over again until the folders have been copied but once it is finished I need it to stop. I was originally going to use triggers, however time-based triggers are inappropriate for the task I am trying to fulfil.

In short, I need my script to run until the task is completed, automatically restarting itself, and avoiding execution time errors.

The full code is included below.

//Global Variables
var classroomFolderName = "Classroom";
var archiveFolderName = "Archive";

function doGet(){
  var classroomFolder = DriveApp.getFoldersByName(classroomFolderName).next();
  var archiveFolder = DriveApp.getFoldersByName(archiveFolderName).next();

  start(archiveFolder);
}

function getFolder(folderName){
  var foldercount = 0;
  //Selects all folders named exactally as parameter is given
  var folder = DriveApp.getFoldersByName(folderName);
  while (folder.hasNext()) {
    var folders = folder.next();
    foldercount++;
  }

  //Throws errors if number of folders != 1
  if (foldercount < 1){
    throw 1;
  }

  else if (foldercount > 1){
    throw 2;
  }

  else{
    return folder;
  }
}

function start(archiveFolder) {

  var sourceFolder = classroomFolderName;
  var targetFolder = "New Archive";

  var source = DriveApp.getFoldersByName(sourceFolder);
  var target = archiveFolder.createFolder(targetFolder);

  if (source.hasNext()) {
    copyFolder(source.next(), target);
  }

}

function copyFolder(source, target) {

  var folders = source.getFolders();
  var files   = source.getFiles();

  while(files.hasNext()) {
    var file = files.next();
    file.makeCopy(file.getName(), target);
  }

  while(folders.hasNext()) {
    var subFolder = folders.next();
    var folderName = subFolder.getName();
    var targetFolder = target.createFolder(folderName);
    copyFolder(subFolder, targetFolder);
  }  

}

This is something I came across- you may find some value in it-

https://ctrlq.org/code/20016-maximum-execution-time-limit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM