Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 252: Allow clients to specify message expiration time. #253

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -583,17 +583,17 @@ private List<ParameterValue> getDefinedParameters(Job<?, ?> project) {
ParameterValue param = null;
if (paramDef instanceof StringParameterDefinition) {
param = new StringParameterValue(paramDef.getName(), ((StringParameterDefinition) paramDef).getDefaultValue());
}
if (paramDef instanceof TextParameterDefinition) {
} else if (paramDef instanceof TextParameterDefinition) {
param = new TextParameterValue(paramDef.getName(), ((TextParameterDefinition) paramDef).getDefaultValue());
}
if (paramDef instanceof BooleanParameterDefinition) {
} else if (paramDef instanceof BooleanParameterDefinition) {
BooleanParameterValue defaultParameterValue = ((BooleanParameterDefinition) paramDef).getDefaultParameterValue();
param = new BooleanParameterValue(paramDef.getName(), Boolean.TRUE.equals(Objects.requireNonNull(defaultParameterValue).getValue()));
}
if (paramDef instanceof ChoiceParameterDefinition) {
if (defaultParameterValue != null) {
param = new BooleanParameterValue(paramDef.getName(), Boolean.TRUE.equals(defaultParameterValue.getValue()));
}
} else if (paramDef instanceof ChoiceParameterDefinition) {
param = ((ChoiceParameterDefinition) paramDef).getDefaultParameterValue();
}

if (param != null) {
parameters.add(param);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public SendResult sendMessage(Run<?, ?> build, TaskListener listener, ProviderDa
message.setText(PluginUtils.getSubstitutedValue(pd.getMessageContent(), envVars2));


publisher.send(message);
publisher.send(message, publisher.getDeliveryMode(), publisher.getPriority(), pd.getTimeToLiveMillis());

mesgId = message.getJMSMessageID();
mesgContent = message.getText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,23 @@ public class CIMessageSenderStep extends Step {
private String messageProperties;
private String messageContent;
private boolean failOnError;
private Integer timeToLiveMinutes;

public CIMessageSenderStep(final String providerName,
final MessagingProviderOverrides overrides,
final MESSAGE_TYPE messageType,
final String messageProperties,
final String messageContent) {
this(providerName, overrides, messageType, messageProperties, messageContent, false);
this(providerName, overrides, messageType, messageProperties, messageContent, false, 0);
}

public CIMessageSenderStep(final String providerName,
final MessagingProviderOverrides overrides,
final MESSAGE_TYPE messageType,
final String messageProperties,
final String messageContent,
final Integer timeToLiveMinutes) {
this(providerName, overrides, messageType, messageProperties, messageContent, false, timeToLiveMinutes);
}

@DataBoundConstructor
Expand All @@ -82,7 +92,8 @@ public CIMessageSenderStep(final String providerName,
final MESSAGE_TYPE messageType,
final String messageProperties,
final String messageContent,
Boolean failOnError) {
Boolean failOnError,
Integer timeToLiveMinutes) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@olivergondza , that change is already in the PR, unless I am missing something.

super();
this.providerName = providerName;
this.overrides = overrides;
Expand All @@ -93,6 +104,10 @@ public CIMessageSenderStep(final String providerName,
failOnError = false;
}
this.failOnError = failOnError;
if (timeToLiveMinutes == null) {
timeToLiveMinutes = 0;
}
this.timeToLiveMinutes = timeToLiveMinutes;
}


Expand Down Expand Up @@ -144,6 +159,14 @@ public void setFailOnError(boolean failOnError) {
this.failOnError = failOnError;
}

public Integer getTimeToLiveMinutes() {
return timeToLiveMinutes;
}

public void setTimeToLiveMinutes(Integer timeToLiveMinutes) {
this.timeToLiveMinutes = timeToLiveMinutes;
}

@Override
public StepExecution start(StepContext context) {
return new CIMessageSenderStep.Execution(this, context);
Expand Down Expand Up @@ -182,6 +205,7 @@ public boolean start() throws Exception {
apd.setMessageProperties(step.getMessageProperties());
apd.setMessageContent(step.getMessageContent());
apd.setFailOnError(step.getFailOnError());
apd.setTimeToLiveMillis(step.getTimeToLiveMinutes() * 60 * 1000);
pd = apd;
} else if (p instanceof FedMsgMessagingProvider) {
FedMsgPublisherProviderData fpd = new FedMsgPublisherProviderData(step.getProviderName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class ActiveMQPublisherProviderData extends ActiveMQProviderData {
private String messageProperties;
private String messageContent;
private Boolean failOnError = false;
private Integer timeToLiveMillis = 0;

@DataBoundConstructor
public ActiveMQPublisherProviderData() {
Expand All @@ -60,12 +61,13 @@ public ActiveMQPublisherProviderData(String name, MessagingProviderOverrides ove
super(name, overrides);
}

public ActiveMQPublisherProviderData(String name, MessagingProviderOverrides overrides, MESSAGE_TYPE messageType, String messageProperties, String messageContent, Boolean failOnError) {
public ActiveMQPublisherProviderData(String name, MessagingProviderOverrides overrides, MESSAGE_TYPE messageType, String messageProperties, String messageContent, Boolean failOnError, Integer timeToLiveMillis) {
this(name, overrides);
this.messageType = messageType;
this.messageProperties = messageProperties;
this.messageContent = messageContent;
this.failOnError = failOnError;
this.timeToLiveMillis = timeToLiveMillis;
}

public MESSAGE_TYPE getMessageType() {
Expand Down Expand Up @@ -104,6 +106,15 @@ public void setFailOnError(boolean failOnError) {
this.failOnError = failOnError;
}

public Integer getTimeToLiveMillis() {
return timeToLiveMillis;
}

@DataBoundSetter
public void setTimeToLiveMillis(Integer timeToLiveMillis) {
this.timeToLiveMillis = timeToLiveMillis;
}

@Override
public Descriptor<ProviderData> getDescriptor() {
return Jenkins.get().getDescriptorByType(ActiveMQPublisherProviderDataDescriptor.class);
Expand All @@ -121,12 +132,13 @@ public boolean equals(Object that) {
Objects.equals(this.messageType, thatp.messageType) &&
Objects.equals(this.messageProperties, thatp.messageProperties) &&
Objects.equals(this.messageContent, thatp.messageContent) &&
Objects.equals(this.failOnError, thatp.failOnError);
Objects.equals(this.failOnError, thatp.failOnError) &&
Objects.equals(this.timeToLiveMillis, thatp.timeToLiveMillis);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), messageType, messageProperties, messageContent, failOnError);
return Objects.hash(super.hashCode(), messageType, messageProperties, messageContent, failOnError, timeToLiveMillis);
}

@Extension
Expand All @@ -150,7 +162,8 @@ public ActiveMQPublisherProviderData newInstance(StaplerRequest sr, JSONObject j
MESSAGE_TYPE.fromString(jo.getString("messageType")),
jo.getString("messageProperties"),
jo.getString("messageContent"),
jo.getBoolean("failOnError"));
jo.getBoolean("failOnError"),
jo.getInt("timeToLiveMinutes") * 60 * 1000);
}

public ListBoxModel doFillMessageTypeItems(@QueryParameter String messageType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class FedMsgSubscriberProviderData extends FedMsgProviderData {
private static final long serialVersionUID = -2179136605130421113L;
Expand Down Expand Up @@ -116,6 +117,11 @@ public boolean equals(Object that) {
(this.timeout != null ? this.timeout.equals(thatp.timeout): thatp.timeout == null);
}

@Override
public int hashCode() {
return Objects.hash(name, overrides, checks, variable, timeout);
}

@Extension
@Symbol("fedmsgSubscriber")
public static class FedMsgSubscriberProviderDataDescriptor extends FedMsgProviderDataDescriptor {
Expand Down
21 changes: 14 additions & 7 deletions plugin/src/main/java/com/redhat/utils/MessageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,19 @@ public static SendResult sendMessage(Run<?, ?> build, TaskListener listener, Pro
log.info(startMessage);
listener.getLogger().println(startMessage);
GlobalCIConfiguration config = GlobalCIConfiguration.get();
JMSMessagingWorker worker =
config.getProvider(pdata.getName()).createWorker(pdata, build.getParent().getName());
SendResult sendResult = worker.sendMessage(build, listener, pdata);
String completedMessage = "Sent successfully with messageId: " + sendResult.getMessageId();
log.info(completedMessage);
listener.getLogger().println(completedMessage);
return sendResult;
JMSMessagingProvider provider = config.getProvider(pdata.getName());
if (provider != null) {
JMSMessagingWorker worker = provider.createWorker(pdata, build.getParent().getName());
SendResult sendResult = worker.sendMessage(build, listener, pdata);
String completedMessage = "Sent successfully with messageId: " + sendResult.getMessageId();
log.info(completedMessage);
listener.getLogger().println(completedMessage);
return sendResult;
} else {
String errorMessage = "Unable to find provider " + pdata.getName() + ".";
log.severe(errorMessage);
listener.getLogger().println(errorMessage);
return null;
}
}
}
17 changes: 16 additions & 1 deletion plugin/src/main/java/com/redhat/utils/OrderedProperties.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.redhat.utils;

import java.util.Enumeration;
import java.util.Objects;
import java.util.Properties;
import java.util.Vector;

Expand All @@ -11,6 +12,8 @@
*/
public class OrderedProperties extends Properties {

private final Vector _names;

public OrderedProperties() {
super();

Expand All @@ -35,6 +38,18 @@ public Object remove(Object key) {
return super.remove(key);
}

private final Vector _names;
@Override
public boolean equals(Object that) {
if (!super.equals(that)) {
return false;
}

OrderedProperties thatp = (OrderedProperties) that;
return (this._names != null ? this._names.equals(thatp._names) : thatp._names == null);
}

@Override
public int hashCode() {
return Objects.hash(_names);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
<f:entry title="${%Message content}" field="messageContent">
<f:textarea name="messageContent"/>
</f:entry>
<f:entry title="${%Message time-to-live}" field="timeToLiveMinutes">
<f:number name="timeToLiveMinutes" default="0"/>
</f:entry>
<f:entry field="failOnError">
<f:checkbox title="${%Fail On Error}" name="failOnError" default="false"/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
<p>Sets the length of time in minutes from its dispatch time that a produced message
should be retained by the message system.
</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
<f:entry title="${%Message content}" field="messageContent">
<f:textarea name="messageContent"/>
</f:entry>
<f:entry title="${%Message time-to-live}" field="timeToLiveMinutes">
<f:number name="timeToLiveMinutes" default="0"/>
</f:entry>
<f:entry field="failOnError">
<f:checkbox title="${%Fail on error}" name="failOnError" default="false"/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
<p>Sets the length of time in minutes from its dispatch time that a produced message
should be retained by the message system.
</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.redhat.jenkins.plugins.ci.CIBuildTrigger;
import com.redhat.jenkins.plugins.ci.CIMessageBuilder;
import com.redhat.jenkins.plugins.ci.CIMessageNotifier;
import com.redhat.jenkins.plugins.ci.GlobalCIConfiguration;
import com.redhat.jenkins.plugins.ci.authentication.activemq.UsernameAuthenticationMethod;
import com.redhat.jenkins.plugins.ci.integration.fixtures.ActiveMQContainer;
Expand Down Expand Up @@ -58,7 +59,10 @@
import java.util.Collections;
import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class AmqMessagingPluginIntegrationTest extends SharedMessagingPluginIntegrationTest {

Expand Down Expand Up @@ -104,8 +108,12 @@ public ProviderData getSubscriberProviderData(String topic, String variableName,

@Override
public ProviderData getPublisherProviderData(String topic, MessageUtils.MESSAGE_TYPE type, String properties, String content) {
return getPublisherProviderData(topic, type, properties, content, 0);
}

public ProviderData getPublisherProviderData(String topic, MessageUtils.MESSAGE_TYPE type, String properties, String content, int ttl) {
return new ActiveMQPublisherProviderData(
DEFAULT_PROVIDER_NAME, overrideTopic(topic), type, properties, content, true
DEFAULT_PROVIDER_NAME, overrideTopic(topic), type, properties, content, true, ttl
);
}

Expand Down Expand Up @@ -489,4 +497,33 @@ public void testSimpleCIEventWithMessagePropertiesAsVariable() throws Exception
j.assertLogContains("echo TEST_PROP1 = GOT 1", lastBuild);
j.assertLogContains("echo TEST_PROP2 = GOT 2", lastBuild);
}

@Test
public void testTTL() throws Exception {
FreeStyleProject jobA = j.createFreeStyleProject();
jobA.getBuildersList().add(new Shell("echo $MESSAGE_HEADERS"));
attachTrigger(new CIBuildTrigger(false, Collections.singletonList(getSubscriberProviderData(
null, null, "CI_TYPE = 'code-quality-checks-done' and CI_STATUS = 'failed'"
))), jobA);

FreeStyleProject jobB = j.createFreeStyleProject();
jobB.getPublishersList().add(new CIMessageNotifier(getPublisherProviderData(
null, MessageUtils.MESSAGE_TYPE.CodeQualityChecksDone, "CI_STATUS = failed", null
)));

j.buildAndAssertSuccess(jobB);
waitUntilScheduledBuildCompletes();
j.assertBuildStatusSuccess(jobA.getLastBuild());
j.assertLogContains("\"JMSExpiration\":0", jobA.getLastBuild());

jobB.getPublishersList().add(new CIMessageNotifier(getPublisherProviderData(
null, MessageUtils.MESSAGE_TYPE.CodeQualityChecksDone, "CI_STATUS = failed", null, 1000
)));

j.buildAndAssertSuccess(jobB);
waitUntilScheduledBuildCompletes();
j.assertBuildStatusSuccess(jobA.getLastBuild());
j.assertLogNotContains("JMSExpiration: 0", jobA.getLastBuild());
j.assertLogContains("\"JMSExpiration\":", jobA.getLastBuild());
ggallen marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public void testSimpleCIEventTrigger() throws Exception {
MessageUtils.MESSAGE_TYPE.CodeQualityChecksDone,
"CI_STATUS = failed",
null,
true
true,
5000
)));

j.buildAndAssertSuccess(jobB);
Expand Down