Skip to content

Commit

Permalink
CID-3909: Trigger thread is dying with an unhandled exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
ggallen committed Jan 16, 2019
1 parent 3099988 commit fd84ad7
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 41 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package com.redhat.jenkins.plugins.ci.messaging;

import com.redhat.utils.PluginUtils;
import org.apache.activemq.ActiveMQConnectionFactory;
import static com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingWorker.formatMessage;
import static com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingWorker.getMessageBody;

import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jms.Connection;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Topic;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingWorker.formatMessage;
import static com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingWorker.getMessageBody;
import org.apache.activemq.ActiveMQConnectionFactory;

import com.redhat.utils.PluginUtils;

/*
* The MIT License
Expand Down Expand Up @@ -47,6 +49,10 @@ public class ActiveMqMessageWatcher extends JMSMessageWatcher {
private ActiveMqMessagingProvider activeMqMessagingProvider;
private String topic;

public ActiveMqMessageWatcher(String jobname) {
super(jobname);
}

@Override
public String watch() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ public JMSMessagingWorker createWorker(ProviderData pdata, String jobname) {
}

@Override
public JMSMessageWatcher createWatcher() {
return new ActiveMqMessageWatcher();
public JMSMessageWatcher createWatcher(String jobname) {
return new ActiveMqMessageWatcher(jobname);
}

public boolean IsMigrationInProgress() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public void receive(String jobname, ProviderData pdata) {
try {
Message m = subscriber.receive(timeoutInMs); // In milliseconds!
if (m != null) {
if (provider.verify(getMessageContent(m), pd.getChecks())) {
if (provider.verify(getMessageContent(m), pd.getChecks(), jobname)) {
process(jobname, m);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public class FedMsgMessageWatcher extends JMSMessageWatcher {
private FedMsgMessagingProvider fedMsgMessagingProvider;
private boolean interrupted;

public FedMsgMessageWatcher(String jobname) {
super(jobname);
}

@Override
public String watch() {

Expand Down Expand Up @@ -91,7 +95,7 @@ public String watch() {
String json = z.getLast().toString();
FedmsgMessage data = mapper.readValue(json, FedmsgMessage.class);

if (!provider.verify(data.getBodyJson(), checks)) {
if (!provider.verify(data.getBodyJson(), checks, jobname)) {
continue;
}
return data.getBodyJson();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public JMSMessagingWorker createWorker(ProviderData pdata, String jobname) {
}

@Override
public JMSMessageWatcher createWatcher() {
return new FedMsgMessageWatcher();
public JMSMessageWatcher createWatcher(String jobname) {
return new FedMsgMessageWatcher(jobname);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void receive(String jobname, ProviderData pdata) {
//
String json = z.getLast().toString();
FedmsgMessage data = mapper.readValue(json, FedmsgMessage.class);
if (provider.verify(data.getBodyJson(), pd.getChecks())) {
if (provider.verify(data.getBodyJson(), pd.getChecks(), jobname)) {
Map<String, String> params = new HashMap<String, String>();
params.put("CI_MESSAGE", data.getBodyJson());
trigger(jobname, provider.formatMessage(data), params);
Expand Down Expand Up @@ -359,7 +359,7 @@ public String waitForMessage(Run<?, ?> build, TaskListener listener, ProviderDat
FedmsgMessage data = mapper.readValue(json, FedmsgMessage.class);
String body = data.getBodyJson();

if (!provider.verify(body, pd.getChecks())) {
if (!provider.verify(body, pd.getChecks(), jobname)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.redhat.jenkins.plugins.ci.messaging;

import com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck;
import hudson.EnvVars;
import hudson.model.TaskListener;

import java.util.List;

import com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck;

public abstract class JMSMessageWatcher {

protected String jobname;
protected int timeout;
protected MessagingProviderOverrides overrides;
protected String selector;
Expand All @@ -16,6 +18,10 @@ public abstract class JMSMessageWatcher {
protected EnvVars environment;
protected TaskListener taskListener;

public JMSMessageWatcher(String jobname) {
this.jobname = jobname;
}

public void setTimeout(int timeout) {
this.timeout = timeout;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import jenkins.model.Jenkins;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck;
Expand Down Expand Up @@ -61,39 +64,53 @@ public String getTopic() {
}

public abstract JMSMessagingWorker createWorker(ProviderData pdata, String jobname);
public abstract JMSMessageWatcher createWatcher();
public abstract JMSMessageWatcher createWatcher(String jobname);

public boolean verify(String json, List<MsgCheck> checks) {
if (checks != null && checks.size() > 0) {
if (StringUtils.isBlank(json)) {
// There are checks, but the json is empty. Must be false.
return false;
}
public boolean verify(String json, List<MsgCheck> checks, String jobname) {
if (checks == null || checks.size() == 0) {
return true;
} else if (StringUtils.isBlank(json)) {
// There are checks, but the json is empty. Must be false.
return false;
}

DocumentContext context = JsonPath.parse(json);
for (MsgCheck check: checks) {
if (!verify(context, check)) {
log.fine("msg check: " + check.toString() + " failed against: " + json);
return false;
try {
DocumentContext context = null;
try {
context = JsonPath.parse(json);
} catch (InvalidJsonException ije) {
log.severe(jobname + ": Unable to parse JSON.\n" + ExceptionUtils.getStackTrace(ije));
}
if (context != null) {
for (MsgCheck check: checks) {
if (!verify(context, check, jobname)) {
log.fine(jobname + ": msg check: " + check.toString() + " failed against JSON:\n" + json);
return false;
}
}
log.fine(jobname + ": All msg checks have passed.");
return true;
} else {
log.warning(jobname + ": DocumentContext is null.");
}
log.fine("All msg checks have passed.");
} catch (Exception e) {
log.severe(jobname + ": Unexpected exception raised in verify.\n" + ExceptionUtils.getStackTrace(e));
}
return true;
return false;
}

private boolean verify(DocumentContext context, MsgCheck check) {
String aVal = "";

String field = StringUtils.prependIfMissing(check.getField(), "$.");
private boolean verify(DocumentContext context, MsgCheck check, String jobname) {
try {
aVal = context.read(field).toString();
String field = StringUtils.prependIfMissing(check.getField(), "$.");
String aVal = Objects.toString(context.read(field), "");
String eVal = StringUtils.defaultString(check.getExpectedValue());
return Pattern.compile(eVal).matcher(aVal).find();
} catch (PathNotFoundException pnfe) {
log.fine(pnfe.getMessage());
return false;
log.fine(jobname + ": " + pnfe.getMessage());
} catch (Exception e) {
log.severe(jobname + ": Unexpected exception raised in verify.\n" + ExceptionUtils.getStackTrace(e));
}
String eVal = StringUtils.defaultString(check.getExpectedValue());
return Pattern.compile(eVal).matcher(aVal).find();
return false;
}

@Override
Expand Down

0 comments on commit fd84ad7

Please sign in to comment.