use of com.redhat.jenkins.plugins.ci.provider.data.ProviderData in project jms-messaging-plugin by jenkinsci.
the class RabbitMQMessagingWorker method waitForMessage.
@Override
public String waitForMessage(Run<?, ?> build, TaskListener listener, ProviderData pdata) {
RabbitMQSubscriberProviderData pd = (RabbitMQSubscriberProviderData) pdata;
try {
if (connection == null || !connection.isOpen()) {
connect();
}
if (channel == null || !channel.isOpen()) {
this.channel = connection.createChannel();
}
channel.exchangeDeclarePassive(exchangeName);
channel.queueBind(getQueue(provider), exchangeName, this.topic);
} catch (Exception ex) {
log.severe("Connection to broker can't be established!");
log.severe(ExceptionUtils.getStackTrace(ex));
listener.error("Connection to broker can't be established!");
listener.error(ExceptionUtils.getStackTrace(ex));
return null;
}
log.info("Waiting for message.");
listener.getLogger().println("Waiting for message.");
for (MsgCheck msgCheck : pd.getChecks()) {
log.info(" with check: " + msgCheck.toString());
listener.getLogger().println(" with check: " + msgCheck);
}
Integer timeout = (pd.getTimeout() != null ? pd.getTimeout() : RabbitMQSubscriberProviderData.DEFAULT_TIMEOUT_IN_MINUTES);
log.info(" with timeout: " + timeout + " minutes");
listener.getLogger().println(" with timeout: " + timeout + " minutes");
// Create deliver callback to listen for messages
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String json = new String(delivery.getBody(), StandardCharsets.UTF_8);
listener.getLogger().println("Received '" + delivery.getEnvelope().getRoutingKey() + "':\n" + "Message id: '" + delivery.getProperties().getMessageId() + "'\n'" + json + "'");
log.info("Received '" + delivery.getEnvelope().getRoutingKey() + "':\n" + "Message id: '" + delivery.getProperties().getMessageId() + "'\n'" + json + "'");
RabbitMQMessage message = new RabbitMQMessage(delivery.getEnvelope().getRoutingKey(), json, delivery.getProperties().getMessageId());
message.setTimestamp(new Date().getTime());
message.setDeliveryTag(delivery.getEnvelope().getDeliveryTag());
messageQueue.add(message);
};
String consumerTag = null;
long startTime = new Date().getTime();
int timeoutInMs = timeout * 60 * 1000;
try {
consumerTag = channel.basicConsume(getQueue(provider), deliverCallback, (CancelCallback) null);
while ((new Date().getTime() - startTime) < timeoutInMs) {
if (!messageQueue.isEmpty()) {
RabbitMQMessage message = messageQueue.poll();
log.info("Obtained message from queue: " + message.toJson());
if (!provider.verify(message.getBodyJson(), pd.getChecks(), jobname)) {
channel.basicAck(message.getDeliveryTag(), false);
continue;
}
listener.getLogger().println("Message: '" + message.getMsgId() + "' was succesfully checked.");
if (build != null) {
if (StringUtils.isNotEmpty(pd.getVariable())) {
EnvVars vars = new EnvVars();
vars.put(pd.getVariable(), message.getBodyJson());
build.addAction(new CIEnvironmentContributingAction(vars));
}
}
channel.basicAck(message.getDeliveryTag(), false);
return message.getBodyJson();
}
if (interrupt) {
return null;
}
TimeUnit.MILLISECONDS.sleep(500);
}
log.severe("Timed out waiting for message!");
listener.getLogger().println("Timed out waiting for message!");
} catch (Exception e) {
// for more info
if (e.getClass() == InterruptedException.class) {
Thread.currentThread().interrupt();
}
log.log(Level.SEVERE, "Unhandled exception waiting for message.", e);
} finally {
try {
if (consumerTag != null) {
channel.basicCancel(consumerTag);
}
channel.close();
} catch (Exception e) {
listener.getLogger().println("exception in finally");
}
}
return null;
}
use of com.redhat.jenkins.plugins.ci.provider.data.ProviderData in project jms-messaging-plugin by jenkinsci.
the class CIBuildTrigger method setProviders.
@DataBoundSetter
public void setProviders(List<ProviderDataEnvelope> envelopes) {
ArrayList<ProviderData> providers = new ArrayList<>();
for (ProviderDataEnvelope envelope : envelopes) {
ProviderData providerData = envelope.providerData;
if (providerData == null) {
log.warning("Empty provider submitted");
continue;
}
providers.add(providerData);
}
this.providers = providers;
}
use of com.redhat.jenkins.plugins.ci.provider.data.ProviderData in project jms-messaging-plugin by jenkinsci.
the class CIBuildTrigger method startTriggerThreads.
private void startTriggerThreads() {
if (job == null)
return;
if (providerUpdated) {
log.info("Saving job since messaging provider was migrated...");
try {
job.save();
} catch (IOException e) {
log.warning("Exception while trying to save job: " + e.getMessage());
}
}
if (job instanceof AbstractProject) {
AbstractProject<?, ?> aJob = (AbstractProject<?, ?>) job;
if (aJob.isDisabled()) {
log.info("Job '" + job.getFullName() + "' is disabled, not subscribing.");
return;
}
}
try {
synchronized (locks.computeIfAbsent(job.getFullName(), o -> new ArrayList<>())) {
if (stopTriggerThreads(job.getFullName()) == null && providers != null) {
List<CITriggerThread> threads = locks.get(job.getFullName());
int instance = 1;
for (ProviderData pd : providers) {
JMSMessagingProvider provider = GlobalCIConfiguration.get().getProvider(pd.getName());
if (provider == null) {
log.log(Level.SEVERE, "Failed to locate JMSMessagingProvider with name " + pd.getName() + ". You must update the job configuration. Trigger not started.");
return;
}
CITriggerThread thread = CITriggerThreadFactory.createCITriggerThread(provider, pd, job.getFullName(), this, instance);
log.info("Starting thread (" + thread.getId() + ") for '" + job.getFullName() + "'.");
thread.start();
threads.add(thread);
instance++;
}
}
}
} catch (Exception e) {
log.log(Level.SEVERE, "Unhandled exception in trigger start.", e);
}
}
use of com.redhat.jenkins.plugins.ci.provider.data.ProviderData in project jms-messaging-plugin by jenkinsci.
the class CIBuildTrigger method getComparisonThreads.
private List<CITriggerThread> getComparisonThreads() {
if (providers != null && job != null) {
List<CITriggerThread> threads = new ArrayList<>();
int instance = 1;
for (ProviderData pd : providers) {
JMSMessagingProvider provider = GlobalCIConfiguration.get().getProvider(pd.getName());
// The thread is never started.
if (provider == null)
throw new NullPointerException("No such provider configured for name: '" + pd.getName() + "' on job named '" + job.getFullName() + "'");
CITriggerThread thread = new CITriggerThread(provider, pd, job.getFullName(), null, instance);
threads.add(thread);
instance++;
}
return threads;
}
return null;
}
Aggregations