1

Can anyone help me understand why my triggers are often executing too early?

Here is a snippet of my code, where next is a variable, in seconds, informing when my trigger should next run:

ScriptApp.newTrigger('passiveCheck')
.timeBased()
.after(next * 1000)
.create();

Logger.log('Target time: ' + targetTime(next));

I have tried calculating then logging the timestamp as to when the trigger should run, and my trigger very frequently runs early, sometimes by up to ~45 seconds. My understanding from the documentation is that the delay clock trigger should never run early, but might run late?

I've included targetTime for context:

function targetTime(next) {
  //Get the current date
  var now = new Date();
  //Convert into a timestamp
  now = now.getTime();
  //Convert seconds to milliseconds
  next = next * 1000;
  //Determine the target timestamp for execution
  var target = now + next;
  target = new Date(target);
  //Format timestamp for logging
  target = Utilities.formatDate(target, Session.getScriptTimeZone(), 'yyyy-mm-dd HH:mm:ss');
  return target;
}

Thanks in advance!

Chris
  • 23
  • 3
  • You might wish to post this as an issue. Perhaps they can explain this problem – Cooper Mar 16 '22 at 10:23
  • Related: https://stackoverflow.com/q/31749729/1595451, https://stackoverflow.com/q/24172944/1595451 – Rubén Mar 16 '22 at 15:26
  • Thanks, @Rubén but these are unfortunately unrelated. My trigger always fires and the function always works, the problem is purely that the trigger fires too early. – Chris Mar 17 '22 at 07:47

1 Answers1

2
  • I’ve tried reproducing this behavior on my end but I was unable to.
  • Here is a simple code I’ve used to compare the timestamps:

Sample code:

function main(){
  var next = 10; // 10 seconds
  next = next * 1000; //converting to Milliseconds
  var now = new Date(); //Time now
  var end = new Date(now.getTime() + (next)); //instantiating a Date with (milliseconds_now + milliseconds_next)

  console.log(`now ==> ${now.toISOString()}`); // Now complete Datetime in UTC timezone
  console.log(`end ==> ${end.toISOString()}`); // End complete Datetime in UTC timezone
  console.log(`next ==> ${next}`); // next variable stores the "countdown" time for the trigger

  ScriptApp.newTrigger("triggerFunction").timeBased().after(next).create();
}

function triggerFunction(){
  var now = new Date(); //Time now
  console.log(`now ==> ${now.toISOString()}`); // Now complete Datetime in UTC timezone
}
  • On my tests, the function is not triggered exactly at the time I’ve set in the variable next but in none of my tests it was triggered earlier than the expected time.

  • If that is the case for you, I agree with the user Cooper and I’d recommend to gather evidence (to show the expected time and the real execution time) and submit it as an issue here.

Gustavo
  • 639
  • 2
  • 4
  • Thanks, @Gustavo. I was able to replicate the issue using your code. Next just needed to be longer - in my example between 900 and 1200 seconds. This is also where the code continues to call the next iteration of the trigger, once the triggered function is complete. – Chris Mar 17 '22 at 18:44
  • I've [logged](https://issuetracker.google.com/issues/225186466) an issue accordingly. – Chris Mar 17 '22 at 19:08
  • @Chris No problem! I'd just like to add that I've tried with the duration you've mentioned (`(900 and 1200 seconds`) but even so, the function always fires after the expected time on my attempts. Cheers! – Gustavo Mar 18 '22 at 15:55