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

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

Closed
wants to merge 2 commits into from

Conversation

geogolem
Copy link

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:
#164

a sourceCalendar URl that is not available.

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:
derekantrican#164

because the code changed.
@derekantrican
Copy link
Owner

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.

@geogolem
Copy link
Author

geogolem commented Oct 18, 2021

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...

@jonas0b1011001
Copy link
Collaborator

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;
}

@Lonestarjeepin
Copy link
Collaborator

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

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

Successfully merging this pull request may close these issues.

4 participants