Multithreading with google apps script - 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.

Related

How to control background execution of Apps Script Custom Functions?

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!

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.

How to view total triggered run-time today

I am writing code which is running on triggers. I know that I have 1 hour of triggered run time per day. Can I access how much of this hour I have used? Or can I log how much I used at the end of the day so I can know if I am in danger of going over this quota?
Thank you,
Eric
Now you can make use of Apps Script REST API to achieve this. Specifically, the REST Resource: processes one.
Please refer to the duration field as displayed here -

catching Exceeded maximum execution time?

Is there anyway of catching this GAS error: "Exceeded maximum execution time"
I mean catching with try ... catch(e) // so far it's not working for me.
Thanks
As written in the comments to your question, thats not possible. But, however, you can set a flag in scriptDB or properties when execution starts and clear that flag when execution comes to a normal end, so you can find out during the next run wether your script came to a regular end when it was run last time and try to take corrective actions if not.
The answer above is correct; it's not possible. An easy alternative to the workaround that pbhd mentioned would be to simply track the runtime of your script (e.g. comparing results of new Date().getTime() at regular intervals) and run whatever you'd include under your catch statement right before you hit the maximum execution time. The maximum is 6 minutes (reference).
That way, you don't have to catch the error -- you can preempt it.
During normal testing, it is possible to accidentally create an infinite (or very long running) loop that consumes the daily execution time limit 100%.
Even if you realize what you have dome wrong immediately, you cannot immediately re-try with Google scripts for another 24 hours - thus slowing down ongoing development significantly and maybe forcing the developer to do some other work , taking his focus/"attention stream" away from the current problem. This is almost always bad.
My product ("IBM OLIVER CICS test/debug" - see Wikipedia article) solved this problem - and many others - around 37 years ago - by having a time limit on any particular transaction and intercepting the resulting time out, allowing options of:-
continuation or
examine/modify variables
"manual" re-try (for the same time) or
abort.
Google could implement this approach just as easily - by "pausing" if execution time is looking too heavy. I had a similar solution to other resources in OLIVER - such as excessive API calls ("possible macro loop") and excessive memory usage.
It seems it takes an "old timer" like me to provide solutions to problems that have existed "since the beginning of time" (and certainly before PC's were thought of).
Googles current "solution" (i.e. absolute limits) only helps Google keep its own servers from being swamped. It would be easy for them to do what OLIVER did all those years ago. By the way there should be no "IBM" prefix on the Wikipedia article - it was my own product and some clown Wikipedia editor altered it to include the prefix.
(By the way, Google do not prevent other scripts on same s/s running - that maybe only use minimal amounts of extra time ( i.e. Scripts on the same spreadsheet still work) . I tried renaming the original script as an experiment but it was stopped after a very short time with "exceeded execution time" error.
GIZ-A-JOB Google - you know its worth it!