Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More flexibility in scheduling a trigger #255

Closed
derekantrican opened this issue Jan 28, 2022 · 1 comment · Fixed by #358
Closed

More flexibility in scheduling a trigger #255

derekantrican opened this issue Jan 28, 2022 · 1 comment · Fixed by #358
Labels
enhancement New feature or request

Comments

@derekantrican
Copy link
Owner

Currently, we use the everyMinutes function to schedule triggers, but according to the docs this only accepts 1, 5, 10, 15 or 30 as an input. We should allow a bit of greater flexibility that if you put in 60, 120, or some other multiple of 60 for howFrequent then we should use the everyHours function instead

@derekantrican derekantrican added the enhancement New feature or request label Jan 28, 2022
@Lonestarjeepin
Copy link
Collaborator

Lonestarjeepin commented Aug 18, 2023

@derekantrican , I played around with this (with some help from chatGPT because the last time I wrote Javascript was 20 years ago...) and came up with this code. I don't know how to submit a pull request in Github so this is the best version I have of sharing code back to you!

Same howFrequent variable, but I added more comments to explain acceptable inputs and how those will be manipulated.*****
var howFrequent = 5; // What interval (minutes) to run this script on to check for new events. Any integer can be used, but will be rounded up to 5, 10, 15, 30 or to the nearest hour after that.. 60, 120, etc. 1440 (24 hours) is the maximum value. Anything above that will be replaced with 1440.

Modified Install function in Code.gs:****
function install() {
// Delete any already existing triggers so we don't create excessive triggers
deleteAllTriggers();

// Schedule sync routine to explicitly repeat and schedule the initial sync
var adjustedMinutes = getValidTriggerFrequency(totalMinutes);
if (adjustedMinutes >= 60) {
ScriptApp.newTrigger("startSync")
.timeBased()
.everyHours(adjustedMinutes / 60)
.create();
} else {
ScriptApp.newTrigger("startSync")
.timeBased()
.everyMinutes(adjustedMinutes)
.create();
}
ScriptApp.newTrigger("startSync").timeBased().after(1000).create();

// Schedule sync routine to look for update once per day using everyDays
ScriptApp.newTrigger("checkForUpdate")
.timeBased()
.everyDays(1)
.create();
}

Modified getValidTriggerFrequency function in Helpers.gs*

function getValidTriggerFrequency(origFrequency) {
if (!origFrequency > 0) {
Logger.log("No valid frequency specified. Defaulting to 15 minutes.");
return 15;
}

// Limit the original frequency to 1440
origFrequency = Math.min(origFrequency, 1440);

var acceptableValues = [5, 10, 15, 30].concat(
Array.from({ length: 24 }, (_, i) => (i + 1) * 60)
); // [5, 10, 15, 30, 60, 120, ..., 1380, 1440]

// Find the smallest acceptable value greater than or equal to the original frequency
var roundedUpValue = acceptableValues.find(value => value >= origFrequency);

Logger.log(
"Intended frequency = " + origFrequency + ", Adjusted frequency = " + roundedUpValue
);
return roundedUpValue;
}

String.prototype.includes = function(phrase){
return this.indexOf(phrase) > -1;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants