2

So I'm trying to run a function every hour on the hour. The point of this function is to update a schedule to see what the current time is, and the events going on at that point in time.

I have setup Time-driven project trigger that is on an hour timer, however, it seem inconsistent when it runs, sometimes its 5 mins before the hour, other times its 10 mins after the hour.

I want the function to be run on the hour, is there a way to do that?

Alan Wells
  • 30,746
  • 15
  • 104
  • 152

2 Answers2

2

This is very possible: what you want to do is set the trigger for the next run at the end of the run.

So, you would run the function once manually and at the end insert this snippet:

ScriptApp.newTrigger("FUNCTION HERE")
           .timeBased()
           .at(new Date(new Date().setHours(new Date().getHours()+1,0,0,0))
           .create();

But, because this is, in some ways, a tenuous chain of triggers (if one ever fails, it won't continue running the next hour) you have a few options.

I would make a failsafe function that runs every day and makes sure the above function is working (if not, start the chain again). I would also wrap whatever else is in this function in a try catch block (scroll to the bottom of the page: you should be doing this anyway for all your functions, always) and add the above code to that as well so that even if your function fails, it will still run next time (or put it in the beginning of the function).

AstroCB
  • 12,337
  • 20
  • 57
  • 73
JZL003
  • 426
  • 5
  • 16
  • You're procedure is the obvious one. And I tried it as well. I found it to be not reliable enough especially if you want to run a function at short intervals. At first I tried to use it at intervals of a few seconds --> impossible. Later I tried using it at intervals of 15 minutes --> still not reliable. Only if the time of execution doesn't really matter it can be an acceptable solution. Currently I use it ONLY for updating cache every 5 h 30 (as data in cache can be stored for 6 hours) and in COMBINATION with addTimer (so if one fails the other might not fail to executed) – SoftwareTester Jul 20 '14 at 09:12
  • I don't want to be repetitive but this is for the above code where it is at a specific time not just an hourly trigger. The beauty is that, while this takes more maintenance(rerun every run) it uses a one-time trigger that can be up to a minute(I agree that this will most likely not work in sub-minute intervals but I have tested this approach and it consistently gave runs on the exact minute requested) – JZL003 Jul 21 '14 at 19:37
0

I have been struggling with triggers myself and found out they are only useable if it doesn't matter much at what time they run.

According to documentation a timer has a timeframe of 15 minutes to run. So you can''t be sure if it will run at time T or at time (T + 15 minutes) .

I also found out that using the undocumented addTimer does allow to accurately run at a specific moment. See How to implement a trigger or timer that runs after a few seconds using GAS
Have a look at the answer I provided myself on my own question for how to use addTimer

Community
  • 1
  • 1
SoftwareTester
  • 1,048
  • 1
  • 10
  • 25
  • 1
    If it helps you, look at my answer above. There is a way to do it without any undocumented methods. I have tested my answer for posterity, it usually works within seconds(if not on the exact minute itself) – JZL003 Jul 18 '14 at 19:28