How to speed up a Slow running Google Apps Script updating CalendarApp guestlists?

Is there anyway that this semi-trivial script can be sped up? I have it running on 3 separate timer triggers to counter that it will always fail on timeout, but this is eating into my total allotted trigger time.

If it helps, the background is that we have a fortnightly office-wide event which is just a social. Sometimes this gets bumped, but will always be on a Friday. This is a recurring event. If you invite a Google Group list, this will be accurate the first time you add it, but it will not reflect changes to the membership of that group over time. This script removes ex-staff and adds new comers. If a new event is made, then it adds all the current staff.

The actual logjam is in the two setting blocks which has to make a CalendarApp API call for every name in a list (typically <300) In fact because the CalendarApp experiences a lot of failures, each call is repeated in a double try...catch block (only adding time when errors occur - often)

// specifiedDate passed in from a non-tirgger manual override function
function updateOfficeDrinksEvent(specifiedDate) { 

  var today, daysTilFriday, 
      millisecondDay, newTime, nextFridayDate, events, drinks, 
      dGuests, names, missingNames, extraNames, m, e;

      specifiedDate = (specifiedDate.hasOwnProperty('timezone')) 
                   ? specifiedDate : null;

  today = new Date();
  daysTilFriday = (5 - today.getUTCDay());
  millisecondDay = (1000 * 60 * 60 * 24);      
  nextFridayDate = ((daysTilFriday === 0) ? today.getTime() : (daysTilFriday < 0) ? (today.getTime() + (5 + Math.abs(daysTilFriday)) * millisecondDay) : today.getTime() + daysTilFriday * millisecondDay);
  nextFriday = specifiedDate || new Date(nextFridayDate);

  events = CalendarApp.getCalendarById(
             ScriptProperties.getProperty('officeEventsCalendar'))
               .getEventsForDay(nextFriday);  

  drinks = events.filter(function(event) { return event.getTitle() == 'Office Drinks';})[0];
           // There can be only one!

  if (drinks && drinks.getTitle().indexOf('Office Drinks') > -1) {

    // Filter staff group list and event guest list to find new and removed staff
    allNames = Utilities.jsonParse(Utilities.jsonStringify(GroupsApp.getGroupByEmail(ScriptProperties.getProperty('allStaffGroup')).getUsers())); // All Staff until a better method is available for capturing subGroups by OU
    dGuests = drinks.getGuestList(false).map(function(guest) { return guest.getEmail(); });
    missingNames = allNames.filter(function(name) { return (dGuests.indexOf(name) < 0); }).sort(function (a,b) {return (a < b) ? 1:(a > b) ? -1 : 0; });
    extraNames = dGuests.filter(function(name) { return (allNames.indexOf(name) < 0 && name.indexOf('resource.calendar.google.com') < 0 && name.indexOf(ScriptProperties.getProperty('domain'))); });


/**** SLOW BLOCK 1 - removes anyone in the invite list no longer a member of staff ****/
    for (e = 0; e < extraNames.length; e +=1) {
      try {
        drinks.removeGuest(extraNames[e]);
      } catch(err) {
        try {
        // second attempt
          drinks.removeGuest(extraNames[e]);
        } catch(er) {
          // I give up.
          Logger.log("can't remove missing name: " + extraNames[e]);
          Logger.log(er.stack);
        }
      }
    }

/**** SLOW BLOCK 2 - for a new event adds everyone missing from the invite in the staff list ****/
    for (m = 0; m < missingNames.length; m +=1) {
      try {
        drinks.addGuest(missingNames[m]);
      } catch(err) {
        // second attempt
        try {
          drinks.addGuest(missingNames[m]);
        } catch(er) {
          // I give up.
          Logger.log("can't add missing name: " + missingNames[m]);
          Logger.log(er.stack);
        }
      }
    }        
  }
}

Of course any general code comments are welcome, but principally i'm interested in the operation speed. There is no batch add or remove functions - I think I'll add a feature request in the issue tracker.

Answers


Need Your Help

Block certain IP from accesing you AWS EC

php security amazon-web-services

my question is regarding Amazon web services.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.