How do I debug intermittent errors with CalendarApp? - google-apps-script

I have a Google Apps Script which uses a JSON web API to get a list of StarCraft2 tournaments from a site (using UrlFetchApp) and create corresponding Google Calendar events in a variety of Calendars (one Calendar per region/league combination).
I have a trigger set to every 5 minutes, that emails me immediately on errors, and if the script completes normally and has added any events or run into any errors I caught, it emails me as well.
Most of the time, there are no changes necessary (I cache the JSON info using ScriptDb to avoid calling out to CalendarApp unless I need to) and the script is silent. However, overnight I received several emails which contained the messages:
The resource you requested could not be located. (line 154)
Service error: CalendarApp: Error body {font-family: arial,sans-serif}
G o o g l e Error Cannot access the calendar you requested (line 155)
TypeError: Cannot call method "getEvents" of null. (line 155)
Lines 154, 155 and 156 are:
var calendar = CalendarApp.getCalendarById(calendars[region][league]);
var events = calendar.getEvents(new Date(1900, 1, 1), new Date());
events.forEach(function (event) { event.deleteEvent(); });
At this point in the script I've already attempted to add any new events and I'm about to query each of my Calendars to delete any events which occurred in the past.
These errors appear to be an issue with the availability of CalendarApp, but the Dashboard does not indicate any issues with the service.
My project key is: MWhWmFSrL1dK3RJui6Aphb12cA4KNgw7R

I have many scripts that use CalendarApp and all of them have occasional errors in their execution, wether in manual mode or time triggered, it seems (but I know this is an empirical feeling) that this service is subject to errors when the traffic is too high or the internet connection is too slow (a few month ago my office had a very poor internet connection and the script used to generate a lot of errors -while it didn't when executed from home-, now that the situation is better the same script runs smoothly everywhere).
Anyway, whatever the real cause might be (and only a Google engineer could answer), the solution I used was to put the Calendar calls in a try-catch loop that tries the connection a few times and since then I have no error notification anymore.
On another script that I didn't change yet, I keep getting errors from time to time but since it doesn't concern critical information I don't really mind... note that I just checked right now and I haven't receive any notification for 5 days ! (must be a good season for calendars ;-)

Related

How to get a notification if Google Script's "My Executions" shows that a script hasn't run in specified time

I have a Google Script running every 5 minutes. It works except randomly there are sudden crashes and the script/trigger won't survive that. So, for many different reasons, the script stops running after some weeks of continuing runtime. At this point, I would need a notification.
How can I get a notification if a function (script) hasn't run in say 2 hours? Doesn't matter what the technique/notification is, but probably email would be great for the notification. I don't think the solution can be in the script code itself, because the script randomly (every few weeks) crashes at Google's side and there's nothing I can do about that.
Google Apps Script installable triggers automatically set an email notification. You could edit when it should be sent (i.e. send immediately) and you could add more notifications. NOTE: This can only be done manually.
The above will work for "normal" failures but it there is a service outage or other platforms failures the notification might not be sent.
One option is to log the executions somewhere then set a second trigger to check that the first trigger ran every time that it should do it as expected. NOTE: The failure that prevented that the first trigger run might also prevent that his second trigger run i.e. a service outage so you might want to set other monitoring measures accordingly the the severity/priority of this failure and your project budget.

Your script, Drive Migrator, has recently failed to finish successfully

I have this email from google.
Your script, Drive Migrator, has recently failed to finish successfully. A summary of the failure(s) is shown below. To configure the triggers for this script, or change your setting for receiving future failure notifications, click here.
There are about 200 messages of the format:
Start 1/1/19 4:53 PM
Function processFilesAndFolders
Error Message Authorization is required to perform that action.
Trigger time-based
End 1/1/19 4:53 PM
When i click there i fund there is an empty trigger and no further information.
Project -
Last run -
Deployment -
Event Time-based
Function -
Error rate -
Its very strange given nobody was on the system at this time, and I cannot see any details about the trigger.
How do I see what is happening here? Is someone on my system, trying to move my files around? How is is possible to have a trigger that is void of information like this? It looks somewhat hacky. Can I centrally disable this "drive migrator"?
I found an old question about this: Drive Migrator Fail Message
But, perhaps it does not have enough information to get a proper answer.
I also contacted google support directly, but they said that asking here is the only way to get technical support on this service. I understand this is not the best "stackoverflow type question", but I'm at a loss on where to get further information about this.
The failure notifications likely occur when a Google Script is deleted from your Drive without de-authorization or when the permissions get revoked for some reason.
Go here and you'll find a list of all triggers running under your Google account. Delete the one associated with your Failure emails.
This guide explains how you can unsubscribe from the failure emails and also properly de-authorize the associated script.

Google Script Quotas - GmailApp.Search

I've spliced together a script which runs on a 1-minute interval and does a look-up on a Google label:
var mailthreads = GmailApp.search('label:MYLABEL');
Initially, the script ran fine, however within a few days the script began to throw a variety of errors, such as:
Service invoked too many times for one day: gmail
Service using too much computer time for one day
Service invoked too many times in a short time: gmail rateMax. Try Utilities.sleep(1000) between calls.
I have checked Quotas for Google Services with no real explination on what could be causing the script to fail - it ran initially day-to-day without fail, and as the cycle for quotas is 24-hours I would imagine if one 24-hour cycle worked, why didn't the rest?
One theory I have is that the GmailApp.Search query counts each returned item under the label as a single read, and now that the label has grown in size I am reaching a quota limit on reads. Is this how it works, and if not is there anything else I can check?

HTML Loading Messages

Is there a way to pass a value back to a HTML page while a server side function is running?
In the past when I was using the UI service, early in the function I would just create a label in the UI like:
app.createLabel('Working, hang on!');
And it would appear while the script was running, letting them know the script was running. When it got to certain parts of the script I would then be able to update/create a new message like:
app.createLabel('Halfway there, hold on.');
Or
app.createLabel('Fetched values, processing them');
etc., to tell them where we are if the script was running for a long time. An added bonus was that it could also update them on what direction the script was taking, so for example if a room booking script found that the first room was unavailable, it could pass a message back such as:
app.createLabel('First room was busy, checking other rooms');
And so on, and so forth.
Is there a way to do this with the HTML service? It seems my only options are success and failure handlers updating the HTML page when the function is complete, which don't seem to be what I want, as it's not a 'Real Time' message.
You can still use ui.alert() in sheets and docs, and sheets also has toast().
To keep the message within a sidebar or dialog that you've created using the HTML Service, you can set up a separate poller function on the client, to retrieve messages from the server every once in a while. Your long-running function could queue up a message via a function call on the server, which would be picked up by the poller on its next cycle, and displayed to the user.
See How to poll a Google Doc from an add-on for an example of a poller.

Google Apps Script Triggers not firing - but only sometimes

I have a few scripts that I want to run overnight so I've set up script triggers to execute these. Its all pretty straightforward stuff and they run fine. But occasionally, say once a month, one of them fails to execute. No error messages are sent to me by Google or anything like that.
Has anyone else experienced this? Has anyone got any ideas I can check for this error? Obviously if its the actual trigger that's not firing in the first place then the code won't actually run and therefore it won't be able to run any checks.
I'll also flag this with Google themselves in google-apps-script-issues forum.
I have a trigger set to fire every five minutes and a few times a month it will fail. Often it will say something about the function not being found.
You can be notified of an execution failure by setting up an "execution failure notice". In Resources > Current Script's Triggers, after each trigger will be a "notifications" link. Clicking it will open the "Execution failure notifications" window.
You have some choices about timing of the email notification of the failure.
Maybe: I suspect that, after I edit my code, sometimes the triggers miss fire. But I have never double checked this.
Suggestions:
1) add some logging and at the end of the script, grab the log and email it back to yourself.
2) Add a try catch around your code and email any errors that you catch back to yourself.
Google scripts are throttled in various ways and your script may be hitting up against one of these limits. There are limits on the number of emails you can send and limits on execution time of the script in addition to others.