Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I identify which time-driven trigger has called a function?

I have a Google App Script function in a Google Sheets spreadsheet I need to call once a day using a time-driven trigger.

This function often takes longer to run than the maximum time allowed for scripts, currently 6 minutes, so I've written it to do its work over several calls. If the function hasn't finished I would like to create a temporary time-driven trigger to run the function again in one minute and delete the temporary trigger when the function is called, but leave the daily trigger active. Pseudocode probably explains it better...

function run_job_via_trigger(trigger) {
  if(trigger === temporary trigger) {
    // If this is a 'temporary' trigger that was created to
    // run the job after the first call then delete it.
    // This must not delete the daily trigger that makes the
    // first call to the function.
    // If I check the UID of the trigger here I still
    // would need to know which trigger is the daily trigger
    // and which is a temporary trigger!
    ScriptApp.deleteTrigger(trigger)
  }

  const job_finished = job_that_takes_several_calls_to_complete();

  if(job_finished === false) {
    // Create a temporary time-driven trigger to call this
    // function again in 1 minute.
    ScriptApp.newTrigger('run_job_via_trigger').timeBased().everyMinutes(1).create();
  }

}

function job_that_takes_several_calls_to_complete() {
  // This function often takes more time to complete than
  // the maximum time allowed for scripts to run. It keeps
  // track of its execution time and returns true if it has
  // finished doing what it needs to do or false if it
  // needs more time and should be called again.
  return finished ? true : false;
}

How can I detect which time-driven trigger has called the run_job_via_trigger function so I can delete the temporary trigger(s) but not the daily trigger?

The spreadsheet has several other time-driven triggers so simply deleting all triggers at the end and creating a new daily trigger is not, as far as I can tell, an acceptable solution.

like image 995
x-x Avatar asked Mar 02 '23 11:03

x-x


1 Answers

When your function is called with a trigger, it receives a trigger event as its parameter. You can check the trigger UID for example, like so:

function doWhatever(e) {
  if(e.triggerUid === 'trigger_id') {
    // do something
  } else {
    // do something else
  }
}

UPDATE

There are a couple of ways to know which triggers are running.

The best-case scenario, when you created a trigger, you stored its ID somewhere, like user properties and then you always know when it's running. But I guess you haven't done that.

In your case you might want to do some manual work. Go to the triggers page, find your recurring trigger, click on the three dots on the right and select "Executions". You will then see the trigger ID in the filter:

enter image description here

Now you can use that in your code to check whether it's your recurring trigger or your temporary trigger.

like image 117
Dmitry Kostyuk Avatar answered Mar 09 '23 05:03

Dmitry Kostyuk