How to control background execution of Apps Script Custom Functions? - google-apps-script

The list of the Apps Script add-on executions shows that the custom functions are executed in the background every 20 minutes. How to control/avoid executions of custom functions in the background? The background executions of custom functions is a waste of resources. I would like to control the execution of my custom functions.

Check out Triggers
I'm not sure what kind of triggers you have installed, but it sounds like a time-based trigger.
If you go into the menu pictured above, you will be able to manage any triggers you have installed. Including changing the time period of your 20 minute one.
Quotas
One execution every 20 minutes is not something to worry about in terms of quotas, as you have the ability to run many many more executions. Though of course, if running it every 20 minutes is useless to you, by all means, change the period or delete it!

Related

Is there a way to have exact control over google apps script trigger timing?

For understandable reasons Google seems to only be allowing script triggers with 15 minute tolerance on timing (when specifying an absolute time - they allow relative times precise to the millisecond). I'm wondering if I've missed something though. Is there a way of saying I want something to run at exactly 00:01 in a particular time zone every day?
As far as the documentation goes, you are right on the 15 minutes approximation. If you want a script to run exactly at a given time, you should run it using the Apps Script API from a web server of yours or even your desktop. To set it up, you can use this example as a reference.

Service using too much computer time for one day

This indicates that the script exceeded the total allowable execution time for one day. It most commonly occurs for scripts that run on a trigger, which have a lower daily limit than scripts executed manually. The limit as far as I am aware is 6 hours per day. None of my scripts run for that long.
Everything was fine earlier. Suddenly, over the last 3-4 days, all the scripts have started failing. I have no clue what's happening. My only hunch is that one of my scripts is going into an infinite loop. But still, if that happens, it will fail soon and collectively, all scripts would not run for 6 hours.
Please help to resolve this issue. I have a G suite id but Google mentioned that I get help at stackoverflow and that they cannot help.
The exception message should also tell you a lot about what is happening. If you are not sure which script is causing the issues you can try to go to the Google Apps Script Manager. In the menu bar on the left you will see a section called My Executions. If you click that you can see a list of scripts that ran and how long they ran for. This page also allows you to sort by duration and filter the results to hopefully help you figure out what is going on.
It possible that you have reached your quota. Having a Google Suite account allows you up to 6 hrs/day of Trigger Total Runtime. Also make sure you are not going over the limit on single script runtime as outlined here further down on the Quota For Google Services page.
For Google Suite Accounts the quota is:
Script runtime: 6 min/execution
Custom function runtime: 30 sec/execution
I had a similar problem. It's a little maddening because getting an error for the script leads one to believe it is a possible syntax problem. More likely, the script never ran in the first place, which is why you likely don't have a failed execution status for the trigger. What may have happened instead is that you have a script or scripts that are still firing for documents that no longer exist.
Navigate to "My Triggers" in the G Suite Developer Hub: https://script.google.com/home/triggers
Click on any time-based events and select "executions."
Search the "status" column for the words "Timed out," and check the duration.
Delete the trigger(s) that is timing out, or otherwise resolve why it is timing out (such as restoring the deleted document).
In my case, I had deleted several old spreadsheets, but one of my add-ons (Supermetrics) was still firing triggers. I had incorrectly assumed that when I deleted my spreadsheets the triggers would have gone with them. Instead they were firing and timing out after about six minutes, and then four hours later they would try to execute again. I added up the duration for all of these triggers and, sure enough, it exceeded my quota.
Delete those old triggers, and you'll likely be back within your quota limits the next day when it resets. This is what worked for me, but I imagine it's not a one-size fits all solution.

Updating HTML interface from Server Script

I have written a standard google web app script with server code and a html interface. The web interface prompts the user for a month and year then creates a google document (by copying a template document) for every day in the month selected.
I have got the code working but I would like to provide some sort of status as the code is running. It takes about 4 mins. I put out a message before calling the server code and a message when it completes but I would like to provide progress updates. i.e. "Docs Created 1", "Docs created 2" etc.
I guess what I am really asking is can google server code update something on the web page while it is running?
Thanks.
Will
No, it cannot (AFAIK).
But since it is taking 4 minutes (which is dangerously close to the 6 minute limit), I suggest you create only one document on your function, then return to the client-side. The client-side will then update the status on the screen and call your server-side function again, which will do the second document, return and so on, until you finish.
By doing this you not only updates your client-side but also avoid getting close to the maximum execution limit. The downside, of course, is that you'll add up some seconds to your total execution time. But for long tasks I don't much problem, actually informing the user of your script progress will make it feel faster in comparison.

Google App Script trigger is not firing

I am a Google App Script user since last 2 years. I see here in India during peak time i.e during evening the 1 minute trigger is not firing at proper time. Is there any server side issue or there is some other issue. Saying about the worse case scenarios, today my script trigger has not been fired since past 2 hours. I have correctly done logging. No log is printed means it does not even go to that method. Can anybody tell me what might be the exact reason behind it.
It might be beneficial to show us the code behind your script, given that there is a method that is potentially being skipped over. Have there been no errors thrown to you via the Google Apps Script notifications?
It seems, perhaps, your issue may fall into place with this currently reported error: https://code.google.com/p/google-apps-script-issues/issues/detail?id=2708
We're likely not going to be able to give you a precise answer to your problem if we can't examine what you're currently working with. I wish you luck in your endeavor, though.

Multithreading with google apps script

Is there any way to run many functions (or the same function with different parameters) at the same time?
Thanks
Yes, by triggering a function at 1 minute intervals where the time to run is > 1 minute (but under the 6 minute maximum) you'll certainly have multiple instances running. It can be hard to manage though: make sure you use the LockService if you're changing/writing to any shared resources and you need also to watch out for hitting 24hr compute time quotas.
There is an other better way: Use ServerHandlers... Unfortunately, concerning my script (Sorting gmail by size) short time limits don't allow me to go faster.