Search in sources :

Example 1 with DefaultAlertChangeEventContent

use of com.thinkbiganalytics.alerts.spi.DefaultAlertChangeEventContent in project kylo by Teradata.

the class JpaBatchJobExecutionProvider method notifyFailure.

@Override
public void notifyFailure(BatchJobExecution jobExecution, OpsManagerFeed feed, boolean isStream, String status) {
    jobExecutionChangedNotifier.notifyOperationStatusEvent(jobExecution, feed, FeedOperation.State.FAILURE, status);
    Alert alert = null;
    // see if the feed has an unhandled alert already.
    String feedId = feed.getId().toString();
    String alertId = jobExecution.getJobExecutionContextAsMap().get(BatchJobExecutionProvider.KYLO_ALERT_ID_PROPERTY);
    String message = "Failed Job " + jobExecution.getJobExecutionId() + " for feed " + feed != null ? feed.getName() : null;
    if (StringUtils.isNotBlank(alertId)) {
        alert = provider.getAlertAsServiceAccount(provider.resolve(alertId)).orElse(null);
    }
    if (alert == null) {
        alert = alertManager.createEntityAlert(OperationalAlerts.JOB_FALURE_ALERT_TYPE, Alert.Level.FATAL, message, alertManager.createEntityIdentificationAlertContent(feedId, SecurityRole.ENTITY_TYPE.FEED, jobExecution.getJobExecutionId()));
        Alert.ID providerAlertId = provider.resolve(alert.getId(), alert.getSource());
        JpaBatchJobExecutionContextValue executionContext = new JpaBatchJobExecutionContextValue(jobExecution, KYLO_ALERT_ID_PROPERTY);
        executionContext.setStringVal(providerAlertId.toString());
        ((JpaBatchJobExecution) jobExecution).addJobExecutionContext(executionContext);
        save(jobExecution);
    } else {
        // if streaming feed with unhandled alerts attempt to update alert content
        DefaultAlertChangeEventContent alertContent = null;
        if (isStream && alert.getState().equals(Alert.State.UNHANDLED)) {
            if (alert.getEvents() != null && alert.getEvents().get(0) != null) {
                alertContent = alert.getEvents().get(0).getContent();
                if (alertContent == null) {
                    alertContent = new DefaultAlertChangeEventContent();
                    alertContent.getContent().put("failedCount", 1);
                    alertContent.getContent().put("stream", true);
                } else {
                    Integer count = (Integer) alertContent.getContent().putIfAbsent("failedCount", 0);
                    count++;
                    alertContent.getContent().put("failedCount", count);
                }
                final DefaultAlertChangeEventContent content = alertContent;
                provider.respondTo(alert.getId(), (alert1, response) -> response.updateAlertChange(message, content));
            } else {
                if (alertContent == null) {
                    alertContent = new DefaultAlertChangeEventContent();
                    alertContent.getContent().put("failedCount", 1);
                    alertContent.getContent().put("stream", true);
                }
                final DefaultAlertChangeEventContent content = alertContent;
                provider.respondTo(alert.getId(), (alert1, response) -> response.unhandle(message, content));
            }
        } else {
            alertContent = new DefaultAlertChangeEventContent();
            alertContent.getContent().put("failedCount", 1);
            if (isStream) {
                alertContent.getContent().put("stream", true);
            }
            final DefaultAlertChangeEventContent content = alertContent;
            provider.respondTo(alert.getId(), (alert1, response) -> response.unhandle(message, content));
        }
    }
}
Also used : BigInteger(java.math.BigInteger) DefaultAlertChangeEventContent(com.thinkbiganalytics.alerts.spi.DefaultAlertChangeEventContent) Alert(com.thinkbiganalytics.alerts.api.Alert)

Aggregations

Alert (com.thinkbiganalytics.alerts.api.Alert)1 DefaultAlertChangeEventContent (com.thinkbiganalytics.alerts.spi.DefaultAlertChangeEventContent)1 BigInteger (java.math.BigInteger)1