@Override public void doRun() { if (!indexerSetupService.isRunning()) { LOG.error("Indexer is not running, not checking streams for alerts."); return; } LOG.debug("Running alert checks."); final List<Stream> alertedStreams = streamService.loadAllWithConfiguredAlertConditions(); LOG.debug("There are {} streams with configured alert conditions.", alertedStreams.size()); // Load all streams that have configured alert conditions. for (Stream stream : alertedStreams) { LOG.debug( "Stream [{}] has [{}] configured alert conditions.", stream, streamService.getAlertConditions(stream).size()); if (stream.isPaused()) { LOG.debug("Stream [{}] has been paused. Skipping alert check.", stream); continue; } // Check if a threshold is reached. for (AlertCondition alertCondition : streamService.getAlertConditions(stream)) { try { final AlertCondition.CheckResult result = alertService.triggered(alertCondition); if (result.isTriggered()) { // Alert is triggered! LOG.debug("Alert condition [{}] is triggered. Sending alerts.", alertCondition); // Persist alert. final Alert alert = alertService.factory(result); alertService.save(alert); final List<AlarmCallbackConfiguration> callConfigurations = alarmCallbackConfigurationService.getForStream(stream); // Checking if alarm callbacks have been defined if (callConfigurations.size() > 0) for (AlarmCallbackConfiguration configuration : callConfigurations) { AlarmCallbackHistory alarmCallbackHistory; AlarmCallback alarmCallback = null; try { alarmCallback = alarmCallbackFactory.create(configuration); alarmCallback.call(stream, result); alarmCallbackHistory = alarmCallbackHistoryService.success(configuration, alert, alertCondition); } catch (Exception e) { if (alarmCallback != null) { LOG.warn( "Alarm callback <" + alarmCallback.getName() + "> failed. Skipping.", e); } else { LOG.warn( "Alarm callback with id " + configuration.getId() + " failed. Skipping.", e); } alarmCallbackHistory = alarmCallbackHistoryService.error( configuration, alert, alertCondition, e.getMessage()); } try { alarmCallbackHistoryService.save(alarmCallbackHistory); } catch (Exception e) { LOG.warn("Unable to save history of alarm callback run: ", e); } } else { /* Using e-mail alarm callback per default if there are no alarm callbacks configured explicitly. This way we are supporting users who have upgraded from an old version where alarm callbacks were non-existent. It also helps for users who forgot to set up alarm callbacks for newly created alert conditions. */ emailAlarmCallback.call(stream, result); } } else { // Alert not triggered. LOG.debug("Alert condition [{}] is not triggered.", alertCondition); } } catch (Exception e) { LOG.error("Skipping alert check that threw an exception.", e); } } } }