private boolean isOkToSend(Notification notification) { if (notification.getCanceled() == Boolean.TRUE) { if (logger.isDebugEnabled()) { logger.debug("Notification {} canceled. Not sending.", notification.getUuid()); } return false; } if (notification.isExpired()) { if (logger.isDebugEnabled()) { logger.debug("Notification {} expired. Not sending.", notification.getUuid()); } return false; } return true; }
public void completed(Notifier notifier, Receipt receipt, UUID deviceUUID, String newProviderId) throws Exception { successes.incrementAndGet(); try { //.{year}.{month}.{day}.{HH24} possibly minute. //random date and time for format //incrementNotificationCounter( "completed" ); EntityRef deviceRef = new SimpleEntityRef(Device.ENTITY_TYPE, deviceUUID); if (receipt != null) { receipt.setSent(System.currentTimeMillis()); this.saveReceipt(notification, deviceRef, receipt,false); if (logger.isTraceEnabled()) { logger.trace("Notification {} receipt saved for device {}", notification.getUuid(), deviceUUID); } } if (newProviderId != null) { if (logger.isTraceEnabled()) { logger.trace("Notification {} replacing notifier id for device {} ", notification.getUuid(), deviceUUID); } replaceProviderId(deviceRef, notifier, newProviderId); } if (logger.isTraceEnabled()) { logger.trace("Notification {} sending completed for device {}", notification.getUuid(), deviceUUID); } } catch(Exception e) { logger.error("Unable to mark notification {} as completed due to: {}", notification.getUuid(), e); } }
public void incrementNotificationCounter(String status){ em.incrementAggregateCounters( null,null,null,"counters.notifications."+notification.getUuid()+"."+status,1 ); LocalDateTime localDateTime = LocalDateTime.now(); StringBuilder currentDate = new StringBuilder( ); currentDate.append( "counters.notifications.aggregate."+status+"." ); currentDate.append( localDateTime.getYear()+"." ); currentDate.append( localDateTime.getMonth()+"." ); currentDate.append( localDateTime.getDayOfMonth()+"." ); currentDate.append( localDateTime.getMinute() ); em.incrementAggregateCounters( null,null,null,currentDate.toString(),1 ); }
public void failed(Notifier notifier, Receipt receipt, UUID deviceUUID, Object code, String message) throws Exception { failures.incrementAndGet(); try { //incrementNotificationCounter( "failed" ); if (logger.isDebugEnabled()) { logger.debug("Notification {} for device {} got error {}", notification.getUuid(), deviceUUID, code); } if(receipt != null) { receipt.setErrorCode( code ); receipt.setErrorMessage( message ); this.saveReceipt( notification, new SimpleEntityRef( Device.ENTITY_TYPE, deviceUUID ), receipt, true ); } finishedBatch(); } catch (Exception e){ logger.error("Unable to finish marking notification {} as failed due to error: ", notification.getUuid(), e); } }
public void finishedBatch() throws Exception { long successes = this.successes.get(); long failures = this.failures.get(); // reset the counters this.successes.set(0); this.failures.set(0); // get the latest notification info notification = em.get(this.notification.getUuid(), Notification.class); notification.updateStatistics(successes, failures); notification.setModified(System.currentTimeMillis()); notification.setFinished(notification.getModified()); em.update(notification); } }
public void scheduleBatchJob(Notification notification, long delay) throws Exception { JobData jobData = new JobData(); jobData.setProperty("applicationId", sm.getApplicationId()); jobData.setProperty("notificationId", notification.getUuid()); jobData.setProperty("deliver", notification.getDeliver()); long soonestPossible = System.currentTimeMillis() + SCHEDULER_GRACE_PERIOD + delay; SchedulerService scheduler = getSchedulerService(); scheduler.createJob("notificationBatchJob", soonestPossible, jobData); if (logger.isTraceEnabled()) { logger.trace("notification {} batch scheduled for delivery", notification.getUuid()); } } public boolean scheduleQueueJob(Notification notification) throws Exception {
notification.addProperties(properties); if (logger.isTraceEnabled()) { logger.trace("ApplicationQueueMessage: notification {} properties updated in duration {} ms", notification.getUuid(), System.currentTimeMillis() - now); notificationQueueManager.queueNotification(notification, null); if (logger.isTraceEnabled()) { logger.trace("NotificationService: notification {} post queue duration {} ms ", notification.getUuid(), System.currentTimeMillis() - now);
protected void checkStatistics(Notification notification, long sent, long errors) throws Exception{ Map<String, Object> statistics = null; long timeout = System.currentTimeMillis() + 10000; while (System.currentTimeMillis() < timeout) { Thread.sleep(200); statistics = app.getEntityManager().get(notification.getUuid(), Notification.class).getStatistics(); if ((Long)statistics.get("sent")==sent && (Long)statistics.get("errors")==errors) { break; } } assertEquals(sent, ((Long)statistics.get("sent")).longValue()); assertEquals(errors, ((Long)statistics.get("errors")).longValue()); }
public boolean scheduleQueueJob(Notification notification, boolean forceSchedule) throws Exception { boolean scheduleInFuture = notification.getDeliver() != null; long scheduleAt = (notification.getDeliver() != null) ? notification.getDeliver() : 0; long soonestPossible = System.currentTimeMillis() + SCHEDULER_GRACE_PERIOD; if (scheduleAt < soonestPossible) { scheduleAt = soonestPossible; scheduleInFuture = false; } boolean scheduled = scheduleInFuture || forceSchedule; if(scheduled) { JobData jobData = new JobData(); jobData.setProperty("applicationId", sm.getApplicationId()); jobData.setProperty("notificationId", notification.getUuid()); jobData.setProperty("deliver", notification.getDeliver()); SchedulerService scheduler = getSchedulerService(); scheduler.createJob("queueJob", scheduleAt, jobData); if (logger.isTraceEnabled()) { logger.trace("notification {} scheduled for queuing", notification.getUuid()); } } return scheduled; } private SchedulerService getSchedulerService() {
final Map<String, Object> translatedPayloads = translatePayloads(payloads, notifierMap); if (logger.isTraceEnabled()) { logger.trace("sending notification for device {} for Notification: {}", deviceUUID, notification.getUuid()); new Receipt( notification.getUuid(), message.getNotifierId(), payload, deviceUUID ); tracker = new TaskTracker( providerAdapter.getNotifier(), taskManager, receipt, deviceUUID ); if (payload == null) { if (logger.isDebugEnabled()) { logger.debug("selected device {} for notification {} doesn't have a valid payload. skipping.", deviceUUID, notification.getUuid()); } finally { if (logger.isTraceEnabled()) { logger.trace("sending to device {} for Notification: {} duration {} ms", deviceUUID, notification.getUuid(), (System.currentTimeMillis() - now));
logger.debug("notification " + notification.getUuid() + " canceled"); final HashMap<Object, ProviderAdapter> notifierMap = getAdapterMap(); if (logger.isTraceEnabled()) { logger.trace("notification {} start query", notification.getUuid()); logger.trace("Notification {} started processing", notification.getUuid()); logger.trace("Provider query for notification {} device {} took {} ms", notification.getUuid(), deviceRef.getUuid(), (System.currentTimeMillis() - now)); ApplicationQueueMessage message = new ApplicationQueueMessage(appId, notification.getUuid(), deviceRef.getUuid(), notifierKey, notifierId); if (notification.getQueued() == null) { device.getUuid(), notification.getUuid()); .doOnError(throwable -> { logger.error("Error while processing devices for notification : {}, error: {}", notification.getUuid(), throwable.getMessage()); notification.setProcessingFinished(-1L); notification.setDeviceProcessedCount(deviceCount.get()); logger.warn("Partial notification. Only {} devices processed for notification {}", deviceCount.get(), notification.getUuid()); try { em.update(notification); em.update(notification); if(logger.isTraceEnabled()) { logger.trace("Notification {} finished processing {} device(s)", notification.getUuid(), deviceCount.get());
protected Notification notificationWaitForComplete(Notification notification) throws Exception { int retry = 18; // 3 mins 18 * 10 seconds while (-- retry > 0) { logger.info("notificationWaitForComplete {} retry {}", notification.getUuid(), retry); app.waitForQueueDrainAndRefreshIndex(10000); notification = app.getEntityManager().get(notification.getUuid(), Notification.class); if (notification.getFinished() != null) { return notification; } } fail("Notification failed to complete error message " + notification.getErrorMessage()); return null; }
public void verifyNotificationCounter(Notification notification,String status,Long timestamp, int expected){ Results countersResults = app.getEntityManager().getAggregateCounters( null,null,null,"counters.notifications."+notification.getUuid()+"."+status, CounterResolution.ALL,timestamp,System.currentTimeMillis(),false ) ; assertEquals( 1, countersResults.getCounters().size() ); if(expected > 0) { assertEquals( expected, countersResults.getCounters().get( 0 ).getValues().get( 0 ).getValue() ); }else if (expected == 0){ assertEquals( 0,countersResults.getCounters().get( 0 ).getValues().size()); } LocalDateTime localDateTime = LocalDateTime.now(); StringBuilder currentDate = new StringBuilder( ); currentDate.append( "counters.notifications.aggregate."+status+"." ); currentDate.append( localDateTime.getYear()+"." ); currentDate.append( localDateTime.getMonth()+"." ); currentDate.append( localDateTime.getDayOfMonth()); //+"." ); countersResults = app.getEntityManager().getAggregateCounters( null,null,null,currentDate.toString(), CounterResolution.ALL,timestamp,System.currentTimeMillis(),false ) ; //checks to see that it exists assertEquals( 1, countersResults.getCounters().size() ); if(expected > 0) { assertEquals( expected, countersResults.getCounters().get( 0 ).getValues().get( 0 ).getValue() ); } else if (expected == 0){ assertEquals( 0,countersResults.getCounters().get( 0 ).getValues().size()); } }
protected void checkReceipts(Notification notification, int expected) throws Exception { List<EntityRef> receipts = getNotificationReceipts(notification); long timeout = System.currentTimeMillis() + 10000; while (System.currentTimeMillis() < timeout) { Thread.sleep(200); receipts =getNotificationReceipts(notification); if (receipts.size()==expected) { break; } } assertEquals(expected, receipts.size()); for (EntityRef receipt : receipts) { logger.info("checkReceipts - receipt uuid: {}, notification uuid: {}", receipt.getUuid(), notification.getUuid()); Receipt r = app.getEntityManager().get(receipt, Receipt.class); assertNotNull(r.getSent()); assertNotNull(r.getPayload()); assertNotNull(r.getNotifierId()); EntityRef source = getNotificationService().getSourceNotification(r); assertEquals(source.getUuid(), notification.getUuid()); } }
notification = (Notification)results.getEntitiesMap().get(notification.getUuid()).toTypedEntity(); assertEquals(0, notification.getDeviceProcessedCount()); results = app.getEntityManager().searchCollection(app.getEntityManager().getApplicationRef(), "notifications", query); Entity entity = results.getEntitiesMap().get(notification.getUuid()); assertNotNull(entity);
notification = app.getEntityManager().get(notification.getUuid(), Notification.class); assertEquals(Notification.State.FINISHED, notification.getState()); app.testRequest(ServiceAction.DELETE, 1, "notifications", notification.getUuid()); } catch (Exception e) { fail("Delete should be successful after notification has finished.");