GAS-ICS-Sync icon indicating copy to clipboard operation
GAS-ICS-Sync copied to clipboard

Currently the script fails execution if it encounters a sourceCalendar URl that is not available.

Open geogolem opened this issue 3 years ago • 3 comments

This commit logs the fact that the url is available and that the syncing of the targetcalendar will be skipped and execution is allowed to continue for syncing subsequent calendars etc.

This commit is sort of a modified implementation of: https://github.com/derekantrican/GAS-ICS-Sync/pull/164

geogolem avatar Oct 17 '21 23:10 geogolem

So the method is skip all syncing to that Google Calendar if any of the source calendars are unavailable? I'm not sure I like that solution - what if I have 3 ical sources for 1 Google calendar, but 1 is unavailable? Then I would stop getting updates from those other calendars.

derekantrican avatar Oct 18 '21 16:10 derekantrican

So the method is skip all syncing to that Google Calendar if any of the source calendars are unavailable? I'm not sure I like that solution - what if I have 3 ical sources for 1 Google calendar, but 1 is unavailable? Then I would stop getting updates from those other calendars.

We can probably change the functionality to handle different cases --> right now, the scirpt just dies at that point the moment it finds a url unavailable. My little change makes it keep going since other url's correspoinding to other targets might be available.

For my use case --> i only have 1 source per target --- but multiple pairs... So I just wanted to keep going if one of the pairs fails..

but I can look into augmenting it for cases where there are multiple sources for a target and 1 or some of those sources are unavailable...

geogolem avatar Oct 18 '21 17:10 geogolem

right now, the scirpt just dies at that point the moment it finds a url unavailable

That is actually a bug in the current code. This should work:

function fetchSourceCalendars(sourceCalendarURLs){
  var result = []
  for (var url of sourceCalendarURLs){
    url = url.replace("webcal://", "https://");
    
    callWithBackoff(function() {
      var urlResponse = UrlFetchApp.fetch(url, { 'validateHttpsCertificates' : false, 'muteHttpExceptions' : true });
      if (urlResponse.getResponseCode() == 200){
        var urlContent = urlResponse.getContentText();
        if(!urlContent.includes("BEGIN:VCALENDAR")){
          Logger.log("[ERROR] Incorrect ics/ical URL: " + url);
          return;
        }
        else{
          result.push(urlContent);
          Logger.log("Result: " + result.length);
          return;
        }     
      }
      else{ //Throw here to make callWithBackoff run again
        throw "Error: Encountered " + urlResponse.getResponseCode() + " when accessing " + url; 
      }
    }, 5);
  }
  
  return result;
}

and

function callWithBackoff(func, maxRetries) {
  var tries = 0;
  var result;
  while ( tries <= maxRetries ) {
    tries++;
    try{
      result = func();
      return result;
    }
    catch(err){
      if ( err.message && (err.message.includes("is not a function")  || !backoffRecoverableErrors.some(function(e){
              return err.message.toLowerCase().includes(e);
            }) )) {
        throw err;
      } else if ( err.message && err.message.includes("Forbidden") ) {
        return null;
      } else if ( tries > maxRetries) {
        Logger.log(`Error, giving up after trying ${maxRetries} times [${err}]`);
        return null;
      } else {
        Logger.log( "Error, Retrying... [" + err  +"]");
        Utilities.sleep (Math.pow(2,tries)*100) + 
                            (Math.round(Math.random() * 100));
      }
    }
  }
  return null;
}

jonas0b1011001 avatar Oct 18 '21 19:10 jonas0b1011001

Closing per conversation with Jonas. This fix was pushed to master via #245 .

Lonestarjeepin avatar Dec 16 '23 18:12 Lonestarjeepin