const SCHEDULE_HANDLER_ = 'runScheduledRequestUpdate'; /** * Installs one hourly trigger for the request-status runner. * Stops on visible conflicts; preserves one existing clock trigger without changing its cadence. * * @returns {void} */ function installRequestStatusTrigger() { const lock = LockService.getScriptLock(); if (!lock.tryLock(5000)) { throw new Error('Could not acquire the trigger installation lock. Try again shortly.'); } try { readAndValidateRequests_(getRequestSheet_()); const existing = ScriptApp.getProjectTriggers().filter(function (trigger) { return trigger.getHandlerFunction() === SCHEDULE_HANDLER_; }); if (existing.some(function (trigger) { return trigger.getEventType() !== ScriptApp.EventType.CLOCK || trigger.getTriggerSource() !== ScriptApp.TriggerSource.CLOCK; })) { throw new Error('A non-clock trigger uses runScheduledRequestUpdate. Review it in Triggers before installing.'); } if (existing.length > 1) { throw new Error('Multiple request-status clock triggers exist. Review them, then run removeRequestStatusTriggers before installing again.'); } if (existing.length === 1) { Logger.log('Trigger already installed: ' + existing[0].getUniqueId()); Logger.log('Confirm its hourly interval in Triggers. This installer does not change an existing schedule.'); return; } const trigger = ScriptApp.newTrigger(SCHEDULE_HANDLER_).timeBased().everyHours(1).create(); Logger.log('Installed trigger: ' + trigger.getUniqueId()); } finally { lock.releaseLock(); } } /** * Removes all visible time-driven triggers for the request-status runner. * A different account's installable triggers are not visible or removable here. * * @returns {void} */ function removeRequestStatusTriggers() { const lock = LockService.getScriptLock(); if (!lock.tryLock(5000)) { throw new Error('Could not acquire the trigger removal lock. Try again shortly.'); } try { const triggers = ScriptApp.getProjectTriggers().filter(function (trigger) { return trigger.getHandlerFunction() === SCHEDULE_HANDLER_ && trigger.getEventType() === ScriptApp.EventType.CLOCK && trigger.getTriggerSource() === ScriptApp.TriggerSource.CLOCK; }); triggers.forEach(function (trigger) { ScriptApp.deleteTrigger(trigger); }); Logger.log('Removed ' + triggers.length + ' request-status trigger(s).'); } finally { lock.releaseLock(); } }