Hi there,
Possibly related to TFS #65614 "Bug in Users code causes excessive (and expensive) calls to google api", we ran into an issue with Live Integration for users being really slow. We were able to relate this to Dynamicweb.Admin reaching out to Google to update a user's address. Here's the sequence of events:
1. A user updates an address in the Customer Center
2. Our Live Integration picks up the change and syncs the user with the ERP. In our code, we also call User.Save()
3. Calling User.Save() triggers the UserOnBeforeSave notification
4. That notification is picked up by Dynamicweb.Admin.Maps.UserNotificationSubscriber which then tries to update the Geo location by calling Google. It's here where the delays occur.
I think the code in there could be improved a bit by updating the user's GeoLocationHashCode even when there's no valid address. It currently looks like this:
public static GeoLocationStatus SetGeolocationFromGoogleMaps(User user, bool force = false) { if (!force && user.IsGeolocationCustom) { return GeoLocationStatus.IsCustom; } string getLocationAddress = Maps.GetGetLocationAddress(user, " "); if (string.IsNullOrEmpty(getLocationAddress)) { return GeoLocationStatus.NoAddress; } string geoLocationHash = Maps.GetGeoLocationHash(getLocationAddress); if (!force && geoLocationHash == user.GeolocationHashCode) { return GeoLocationStatus.NotChanged; } GeoLocation location = Maps.GetLocation(getLocationAddress); if (location == null) { return GeoLocationStatus.None; } user.GeolocationLatitude = location.Latitude; user.GeolocationLongitude = location.Longitude; user.GeolocationHashCode = geoLocationHash; return GeoLocationStatus.Updated; }
I think that by updating the GeolocationHashCode even when no address is returned would fix the issue:
GeoLocation location = Maps.GetLocation(getLocationAddress); if (location == null) { user.GeolocationHashCode = geoLocationHash; return GeoLocationStatus.None; }
That way, when a user has a faulty address that cannot be mapped, the hash is updated anyway and on the next save, no further attempt is made to update the address.
That still leaves the issue for when Google cannot be reached successfully (I think that's what's happening in our case; Google now requires an API key and we don't have one on our site). Would it make sense now that the key is required to update SetGeolocationFromGoogleMaps or Maps.GetLocation to not do anything when there is no key? Or otherwise add a checkbox in settings to disable the behavior altogether?
Thanks!
Imar