Reputation: 263
I have (amongst others) the following four functions.
fallback()
newSubmission()
installSubmissionTrigger()
uninstallSubmissionTrigger()
I have a trigger that:
fallback()
that posts something to the Spreadsheet for review.fallback
calls installSubmissionTrigger()
.installSubmissionTrigger
creates a time-based trigger running every minute.newSubmission()
.newSubmission
does something I want and calls uninstallSubmissionTrigger()
.uninstallSubmissionTrigger
removes the time-based trigger.All of this works fine using Rhino but when I enable V8 the time-based trigger becomes disabled for unknown reasons when it is supposed to run.
Also when using V8, if I run installSubmissionTrigger()
manually, the trigger does fire.
If I run fallback()
manually, the trigger also does fire.
What could be the unknown reason the trigger becomes disabled?
function fallback(event) {
...
installSubmissionTrigger();
...
}
function newSubmission() {
...
uninstallSubmissionTrigger();
...
}
function installSubmissionTrigger() {
var properties = PropertiesService.getScriptProperties();
if(!properties.getProperty("triggerID")) {
var trigger = ScriptApp.newTrigger('newSubmission').timeBased().everyMinutes(1).create();
properties.setProperty("triggerID", trigger.getUniqueId());
Logger.log("Creating newSubmission trigger: " + trigger.getUniqueId());
}
}
function uninstallSubmissionTrigger() {
var properties = PropertiesService.getScriptProperties();
properties.deleteProperty("triggerID");
// Loop over all triggers.
var allTriggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < allTriggers.length; i++) {
// If the current trigger is the correct one, delete it.
if (allTriggers[i].getHandlerFunction() === 'newSubmission') {
ScriptApp.deleteTrigger(allTriggers[i]);
}
}
}
Use-case example:
Upvotes: 6
Views: 2010
Reputation: 644
Here is how I overcame this issue. I added a doGet(e) function in my script and made my script a web app. Now if you setup a trigger from within doGet or doPost, your trigger won't get disabled. Atleast it did not get disabled for me as I tested.
Lastly, I used a free cronjob service and simply sent a GET request to my webapp [DEPLOYMENT] URL every Nth day of the week. Not an ideal solution but for a last resort, it is not bad.
Now, If you wish to setup a trigger from your own script after you have finished working then simply make a get request to webapp URL of your own script and it should work the same way.
Upvotes: 1
Reputation: 3340
This issue you're having has been reported and it's related with V8 runtime [1]. You could work with DEPRECATED_ES5
runtime version which is working as expected.
[1] https://issuetracker.google.com/issues/150756612
Upvotes: 4