Iterators built with different continuation tokens are producing the same results in Google Apps -
i programming google apps script within spreadsheet. utilize case includes iterating on big set of folders children of given one. problem processing takes longer maximum google allows (6 minutes), had programme script able resume later. creating trigger resume task, not part of problem (at least, not more of import 1 @ moment).
my code looks (reduced minimum illustrate problem):
function launchprocess() { var scriptproperties = propertiesservice.getscriptproperties(); scriptproperties.setproperty(source_parent_folder_key, source_parent_folder_id); scriptproperties.deleteproperty(continuation_token_key); continueprocess(); } function continueprocess() { seek { var starttime = (new date()).gettime(); var scriptproperties = propertiesservice.getscriptproperties(); var srcparentfolderid = scriptproperties.getproperty(source_parent_folder_key); var continuationtoken = scriptproperties.getproperty(continuation_token_key); var iterator = continuationtoken == null ? driveapp.getfolderbyid(srcparentfolderid).getfolders() : driveapp.continuefolderiterator(continuationtoken); var timelimitisnear = false; var currtime; while (iterator.hasnext() && !timelimitisnear) { var folder = iterator.next(); processfolder_(folder); currtime = (new date()).gettime(); timelimitisnear = (currtime - starttime >= max_running_time); } if (!iterator.hasnext()) { scriptproperties.deleteproperty(continuation_token_key); } else { var conttoken = iterator.getcontinuationtoken(); scriptproperties.setproperty(continuation_token_key, conttoken); } } grab (e) { //sends mail service error } } when launchprocess invoked, prepares programme other method, continueprocess, iterates on set of folders. iterator obtained using continuation token, when nowadays (it not there in first invocation). when time limit near, continueprocess obtains continuation token, saves in property , waits next invocation.
the problem have iterator returning same set of folders although has been built different tokens (i have printed them, know different).
any thought doing wrong?
thank in advance.
it appears loop not built correctly. (edit : actually, issue how break while loop, see thoughts in comments)
note there no special reason utilize try/catch in context since see no reason hasnext() method homecoming error (but if think can add together it)
here illustration works, added trigger creation / delete lines implement test.
edit : code updated logs , countervar source_parent_folder_id = '0b3qsfd3iike3ms0ymzu4yjq4nc04njqxltqyymetytexnc1lmwvhntzimjlhmmi' var max_running_time = 5*35*6; function launchprocessfolder() { var scriptproperties = propertiesservice.getscriptproperties(); scriptproperties.setproperty('source_parent_folder_key', source_parent_folder_id); scriptproperties.setproperty('counter', 0); scriptproperties.deleteproperty('continuation_token_key'); scriptapp.newtrigger('continueprocess').timebased().everyminutes(10).create(); continueprocessfolder(); } function continueprocessfolder() { var starttime = (new date()).gettime(); var scriptproperties = propertiesservice.getscriptproperties(); var srcparentfolderid = scriptproperties.getproperty('source_parent_folder_key'); var continuationtoken = scriptproperties.getproperty('continuation_token_key'); var iterator = continuationtoken == null ? driveapp.getfolderbyid(srcparentfolderid).getfolders() : driveapp.continuefolderiterator(continuationtoken); var timelimitisnear = false; var currtime; var counter = number(scriptproperties.getproperty('counter')); while (iterator.hasnext() && !timelimitisnear) { var folder = iterator.next(); counter++; logger.log(counter+' - '+folder.getname()); currtime = (new date()).gettime(); timelimitisnear = (currtime - starttime >= max_running_time); if (!iterator.hasnext()) { scriptproperties.deleteproperty('continuation_token_key'); scriptapp.deletetrigger(scriptapp.getprojecttriggers()[0]); logger.log('******************no more folders**************'); break; } } if(timelimitisnear){ var conttoken = iterator.getcontinuationtoken(); scriptproperties.setproperty('continuation_token_key', conttoken); scriptproperties.setproperty('counter', counter); logger.log('write scriptproperties'); } } edit 2 : (see lastly comment)
here test script modified files in folder. different tests appears operation fast , needed set quite short timeout limit create happen before reaching end of list.
i added couple of logger.log() , counter see happening , know sure interrupting while loop.
with current values can see works expected, first (and second) break happens time limitation , logger confirms token written. on 3rd run can see files have been dumped.
var source_parent_folder_id = '0b3qsfd3iike3ms0ymzu4yjq4nc04njqxltqyymetytexnc1lmwvhntzimjlhmmi' var max_running_time = 5*35*6; function launchprocess() { var scriptproperties = propertiesservice.getscriptproperties(); scriptproperties.setproperty('source_parent_folder_key', source_parent_folder_id); scriptproperties.setproperty('counter', 0); scriptproperties.deleteproperty('continuation_token_key'); scriptapp.newtrigger('continueprocess').timebased().everyminutes(10).create(); continueprocess(); } function continueprocess() { var starttime = (new date()).gettime(); var scriptproperties = propertiesservice.getscriptproperties(); var srcparentfolderid = scriptproperties.getproperty('source_parent_folder_key'); var continuationtoken = scriptproperties.getproperty('continuation_token_key'); var iterator = continuationtoken == null ? driveapp.getfolderbyid(srcparentfolderid).getfiles() : driveapp.continuefileiterator(continuationtoken); var timelimitisnear = false; var currtime; var counter = number(scriptproperties.getproperty('counter')); while (iterator.hasnext() && !timelimitisnear) { var file = iterator.next(); counter++; logger.log(counter+' - '+file.getname()); currtime = (new date()).gettime(); timelimitisnear = (currtime - starttime >= max_running_time); if (!iterator.hasnext()) { scriptproperties.deleteproperty('continuation_token_key'); scriptapp.deletetrigger(scriptapp.getprojecttriggers()[0]); logger.log('******************no more files**************'); break; } } if(timelimitisnear){ var conttoken = iterator.getcontinuationtoken(); scriptproperties.setproperty('continuation_token_key', conttoken); scriptproperties.setproperty('counter', counter); logger.log('write scriptproperties'); } } google-apps-script google-spreadsheet google-drive-sdk
No comments:
Post a Comment