Skip to content

Commit

Permalink
Add processor name pattern to MailNotificationProcessor to narrow dow…
Browse files Browse the repository at this point in the history
…n notifications on failure
  • Loading branch information
jmendeza committed Dec 18, 2024
1 parent 18540a0 commit 7ea1c5d
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.net.UnknownHostException;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.craftercms.commons.config.ConfigUtils.*;

Expand Down Expand Up @@ -93,6 +95,7 @@ public enum StatusCondition {
public static final String END_MODEL_KEY = "end";
public static final String STATUS_MODEL_KEY = "status";
public static final String OUTPUT_ATTACHED_MODEL_KEY = "outputAttached";
public static final String PROCESSOR_MATCH_PATTERNS_CONFIG_KEY = "failedProcessors";

protected String defaultTemplateName;
protected String defaultFrom;
Expand All @@ -112,6 +115,7 @@ public enum StatusCondition {
protected boolean html;
protected String serverName;
protected StatusCondition statusCondition;
protected Pattern failedProcessorsPattern;
protected DateTimeFormatter dateTimeFormatter;

/**
Expand Down Expand Up @@ -179,6 +183,11 @@ public void doInit(Configuration config) throws ConfigurationException, Deployer
serverName = getStringProperty(config, SERVER_NAME_CONFIG_KEY);
statusCondition = StatusCondition.valueOf(getStringProperty(config, STATUS_CONDITION_KEY, defaultStatusCondition));

String processorsMatchRegex = getStringProperty(config, PROCESSOR_MATCH_PATTERNS_CONFIG_KEY);
if (processorsMatchRegex != null) {
failedProcessorsPattern = Pattern.compile(processorsMatchRegex);
}

if (StringUtils.isEmpty(serverName)) {
try {
serverName = InetAddress.getLocalHost().getHostName();
Expand Down Expand Up @@ -206,6 +215,10 @@ protected ChangeSet doPostProcess(Deployment deployment, ChangeSet filteredChang
status, statusCondition);
return null;
}
if (!matchFailedProcessors(deployment)) {
logger.info("Skipping notification because failed processors do not match the configured patterns");
return null;
}

Map<String, Object> templateModel = new HashMap<>();
templateModel.put(SERVER_NAME_MODEL_KEY, serverName);
Expand Down Expand Up @@ -252,6 +265,18 @@ protected ChangeSet doPostProcess(Deployment deployment, ChangeSet filteredChang
return null;
}

/**
* Indicates if any failed deployment processor matches the configured patterns.
*/
private boolean matchFailedProcessors(final Deployment deployment) {
return failedProcessorsPattern == null ||
deployment.getProcessorExecutions().stream()
.filter(ex -> ex.getStatus() == Deployment.Status.FAILURE)
.map(ProcessorExecution::getProcessorName)
.map(failedProcessorsPattern::matcher)
.anyMatch(Matcher::matches);
}

protected boolean hasExecutionsFailures(Deployment deployment) {
for (ProcessorExecution execution : deployment.getProcessorExecutions()) {
if (execution.getStatus() == Deployment.Status.FAILURE) {
Expand Down

0 comments on commit 7ea1c5d

Please sign in to comment.