Reschedule a script 24+1 hours after it was last terminated

31 views
Skip to first unread message

Andrea Bonamici

unread,
Apr 9, 2019, 6:39:33 AM4/9/19
to Google Apps Script Community
Goodmorning everyone. I need help, please.
I'm developing a script (I opened another post a few days ago) that populates Google Contacts starting from Sheet.
The problem concerns the achievement of the maximum use of the ContactsApp Service (2000 call for day).
This limit forces me to run the script every 24 hours (so that the limit is reset).
if I run the script (for example) every 23 hours I get an error
(Service called too many times a day: contacts. at createContact).

I would like the script, at the end of itself, to be rescheduled after 24 + 1 hour.
Do you have any ideas / suggestions?

For example.....
The script start at 1:00AM (via trigger) on Monday
The script finish at 4:00AM on Monday
How can reschedule the script at 5:00AM on Tuesday ?

Next time script start at 5:00AM on Tuesday an finish at 8:00AM on Tuesday
The script should be re-trigger next time at 9:00AM on Wednesday.

etc.


One solution would be....
Does the script end on Tuesday? OK .. I reschedule it Thursday.
This is technically fine but I don't want to spend too much time 
from one execution to another to prevent users from having obsolete data (in their contacts).

Thank you all for your valuable suggestions.


PS. Sorry for my english
Best regards
Andrea

ChiefChippy2 is awesome

unread,
Apr 9, 2019, 6:51:02 AM4/9/19
to Google Apps Script Community
You can use ScriptApp to interact with triggers.
OR you can save the next trigger run time in PropertiesService and you check it every hour with a trigger. If the time is before the current time, run the action.

Andrea Bonamici

unread,
Apr 9, 2019, 11:20:29 AM4/9/19
to Google Apps Script Community
Dear ChiefChippy2 

Thank you so much for the suggestion.
I didn't know Properties Service. 
Thanks.

Unfortunately I always find obstacles bigger than me.

If I haven't misunderstood you approach could be:

Script write actual Date  in Properties Service when script finish.
-this will be the date of last execution (DateLastExecution)-
Every hours a trigger read DateLastExecution and perform  a difference 
form actual Date and DateLastExecution

If difference is > 24 then....perform a function 
At end of script the DateLastExecution will be replaced with actual Date.

My problem is ...
It's possible to manage as date (and not string) the value written in Properties Service ?

Because if I try this code

//write last executio date
var LastExecutionDate = new Date();
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty('SchedulazioneDay', LastExecutionDate);

//perform difference
TODAY = new Date();
var Difference = (TODAY - LastExecutionDate);

the value of Difference (in the logger)  is NaN.

I believe because the variable LastExecutionDate is not managed as date.

Sorry..... I don't have the gift of synthesis :D

Thanks
Grazie
Andrea

Romain Vialard

unread,
Apr 9, 2019, 11:23:51 AM4/9/19
to Google Apps Script Community
You can also simply set up a time-driven trigger (like every hour, or even every X minutes) and abort if you get this specific error.

Linking that to your previous question on Exponential backoff, you could abort specifically if you have this error and retry otherwise. 
Here's again an example where I'm using the ErrorHandler library:

var contact = ErrorHandler.expBackoff(function(){return ContactsApp.createContact(firstName, lastName, emailAddress);});
if (contact instanceof Error && contact.message === ErrorHandler.NORMALIZED_ERRORS.SERVICE_INVOKED_TOO_MANY_TIMES_FOR_ONE_DAY) return;

One of the good thing here is that the library handles this error message in multiple languages:

Andrea Bonamici

unread,
Apr 9, 2019, 11:38:25 AM4/9/19
to Google Apps Script Community
Thanks for the reply. 
Unfortunately I can't do this ... 
My script starts by deleting all previously imported contacts.
Then, one to one, write  the contacts from Sheet.

If the script fails, user lost his Google Contacts.

PS. I apologize if I write inaccuracies or propose stupid problems. I don't know Google App Script and even JavaScript. Sigh.

Andrea

Romain Vialard

unread,
Apr 9, 2019, 11:42:27 AM4/9/19
to Google Apps Script Community
Wouldn't it be possible for you to update the contacts that need to be updated rather than deleting and recreating all of them ?

Andrea Bonamici

unread,
Apr 9, 2019, 11:52:56 AM4/9/19
to Google Apps Script Community

You're right Romain. I tried to think in this direction but I'm afraid it's more complicated.
If the user changes a row in the Sheet (that will be imported as contact)
how could I find the corresponding contact previously created to perform the update?

Honestly, the script I wrote (with difficulty!) works correctly if the contacts to manage are three / four hundred.

Unfortunately the contacts are around 1900.
With all these I have to solve the problem
of the execution time (30 minutes max) and the number of calls
daily to the ContactApp service.

I was so happy with my script :(

Thank you

andreabon...@gmail.com

unread,
Apr 10, 2019, 4:55:17 AM4/10/19
to Google Apps Script Community
I finally understood (I hope) how to solve my problem.
"How to re-run a function (with trigger) after 25 (or more) hours since the last execution"

I thought / tried some solutions but not the simplest of them all

My triggered function is  'ProjectSharedContacts'.
To re-run it after 25 (or more) hours I added at the end of the function...

    //Deletes all triggers in the current project.
var triggers = ScriptApp.getProjectTriggers();
 
for (var i = 0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
//Create new Trigger
    var datetime = new Date(); //This is the date on which the script ended
    datetime.setHours(datetime.getHours()+26); //I add 26 hours
    
ScriptApp.newTrigger('ProjectSharedContacts')
    .timeBased()
    .at(datetime) //Reschedule at new date
    .create();

I did a test and it worked correctly.
Thanks again for your valuable suggestions.
Best regards
Andrea

On Tuesday, 9 April 2019 17:42:27 UTC+2, Romain Vialard wrote:
Reply all
Reply to author
Forward
0 new messages