Bypass script execution timeout, programmatically
Does your script exceed maximum execution time? Here's a solution to automatically bypass that.
an app script junkie's worst nightmare:
script "exceeded maximum execution time"
as per google's quotas for its apps script services, the script runtime can vary either between 6 mins. per execution - if you're on an unpaid account - or 30 mins., if on paid. normally, 6 mins. is sufficient to run typical automations for everyday life but at times, there are instances where you may want the script to run a longer time, depending on the tasks it is executing (i.e. if the count of tasks are high) and/or the amount of information its processing.
either way, the only non-tech solution to this was to re-run the script over and again, until such time that all tasks are completed; however, this brings up a number of challenges, a few of what i've experienced are as follows -
- starts from beginning: obviously, this depends heavily on the structure of the code but nonetheless, there is currently no way that the script would automatically know where it last stopped (owing to an execution timeout) and to having commenced from there on a re-run.
- overwrites or repeats data: again, depending on the architecture of the code, if you're appending data to either a doc, sheet or someplace else, you'd be required to maintain a marker or "run a check" to assess what-was-last, before writing data to said system.
- manual: notwithstanding my immense respect towards the divine human intervention 🙄, there's but always a chance of human error in manually having to first check where to begin and reassess what has to be done.
after scouring through the internet for its various solutions on the matter, i've combined my solution that reflects 2 key aspects -
- check elapsed time: create a seperate function which returns the difference between now (as in, when the script calls this function) and from when the script started to execute, owing to the original function
- thanks for an article on digital inspiration
- break before hitting threshold: this is the key; in order to successfully exit the script before it hits it's mandated timeout limit, you'd need to set a threshold that's well before when the script ought to terminate automatically
- spawn a time-based trigger before killing (break;) the script
- kill all triggers after the end of final execution
note that doing so will create time based repetitions (sort of a cron-job, if you will) that you could monitor via the apps script developer hub.