Permalink
Browse files

Bug fix for refreshing console geofences

  • Loading branch information...
richturner committed Nov 6, 2018
1 parent 61e219d commit 5d63fafd3440502d5a1fa66bd7bd52d4e7f8c566
@@ -287,7 +287,6 @@ public void configure() throws Exception {
SentNotification sentNotification = new SentNotification()
.setName(notification.getName())
.setType(notification.getMessage().getType())
.setMessage(notification.getMessage().toValue())
.setSource(source)
.setSourceId(sourceId.get())
.setTarget(targets.getType())
@@ -311,8 +310,10 @@ public void configure() throws Exception {
} else {
LOG.warning("Notification failed '" + id + "': " + targets.getType() + ":" + targetId + ", reason=" + result.getMessage());
sentNotification.setError(TextUtil.isNullOrEmpty(result.getMessage()) ? "Unknown error" : result.getMessage());
em.merge(sentNotification);
}
// Merge the sent notification again with the message included just in case the handler modified the message
sentNotification.setMessage(notification.getMessage().toValue());
em.merge(sentNotification);
} catch (Exception e) {
LOG.log(Level.SEVERE,
"Notification handler threw an exception whilst sending notification '" + id + "'",
@@ -74,6 +74,7 @@
private static final Logger LOG = SyslogCategory.getLogger(RULES, ORConsoleGeofenceAssetAdapter.class.getName());
public static final String NAME = "ORConsole";
public static int NOTIFY_ASSETS_DEBOUNCE_MILLIS = 60000;
public static int NOTIFY_ASSETS_BATCH_MILLIS = 10000;
protected Map<String, RulesEngine.AssetStateLocationPredicates> assetLocationPredicatesMap = new HashMap<>();
protected NotificationService notificationService;
protected AssetStorageService assetStorageService;
@@ -264,16 +265,19 @@ protected void notifyAssetGeofencesChanged(Set<String> assetIds) {
List<String> ids = new ArrayList<>(assetIds);
ObjectValue data = Values.createObject();
data.put("action", "GEOFENCE_REFRESH");
Notification notification = new Notification("GeofenceRefresh", new PushNotificationMessage().setData(data), null);
// Break into batches of 10 sent every 10s to avoid consoles bombarding the backend
int rows = 1 + ids.size() / 10;
IntStream.range(0, rows)
.forEach(i -> {
List<String> subIds = ids.subList(10 * i, Math.min(10 + (10 * i), ids.size()));
final List<String> subIds = ids.subList(10 * i, Math.min(10 + (10 * i), ids.size()));
final Notification notification = new Notification("GeofenceRefresh", new PushNotificationMessage().setData(data), null);
notification.setTargets(new Notification.Targets(Notification.TargetType.ASSET, subIds));
LOG.info("Notifiying consoles that geofences have changed: " + subIds);
executorService.schedule(() -> notificationService.sendNotification(notification), i * 10000);
executorService.schedule(() -> {
LOG.info("Notifiying consoles that geofences have changed: " + notification.getTargets());
notificationService.sendNotification(notification);
}, i * NOTIFY_ASSETS_BATCH_MILLIS);
});
}
Oops, something went wrong.

0 comments on commit 5d63faf

Please sign in to comment.