Search in sources :

Example 1 with ProviderData

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;
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) Date(java.util.Date) SendResult(com.redhat.jenkins.plugins.ci.messaging.data.SendResult) ZonedDateTime(java.time.ZonedDateTime) RabbitMQSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.RabbitMQSubscriberProviderData) HashMap(java.util.HashMap) Connection(com.rabbitmq.client.Connection) StringUtils(org.apache.commons.lang3.StringUtils) Level(java.util.logging.Level) CancelCallback(com.rabbitmq.client.CancelCallback) Map(java.util.Map) EnvVars(hudson.EnvVars) TaskListener(hudson.model.TaskListener) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) RabbitMQPublisherProviderData(com.redhat.jenkins.plugins.ci.provider.data.RabbitMQPublisherProviderData) Jenkins(jenkins.model.Jenkins) ExceptionUtils(org.apache.commons.lang.exception.ExceptionUtils) RabbitMQMessage(com.redhat.jenkins.plugins.ci.messaging.data.RabbitMQMessage) MsgCheck(com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck) PluginUtils(com.redhat.utils.PluginUtils) IOException(java.io.IOException) UUID(java.util.UUID) Logger(java.util.logging.Logger) StandardCharsets(java.nio.charset.StandardCharsets) Run(hudson.model.Run) TimeUnit(java.util.concurrent.TimeUnit) Result(hudson.model.Result) Channel(com.rabbitmq.client.Channel) CIEnvironmentContributingAction(com.redhat.jenkins.plugins.ci.CIEnvironmentContributingAction) ProviderData(com.redhat.jenkins.plugins.ci.provider.data.ProviderData) AMQP(com.rabbitmq.client.AMQP) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) DeliverCallback(com.rabbitmq.client.DeliverCallback) MsgCheck(com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck) IOException(java.io.IOException) RabbitMQMessage(com.redhat.jenkins.plugins.ci.messaging.data.RabbitMQMessage) Date(java.util.Date) EnvVars(hudson.EnvVars) RabbitMQSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.RabbitMQSubscriberProviderData) CancelCallback(com.rabbitmq.client.CancelCallback) CIEnvironmentContributingAction(com.redhat.jenkins.plugins.ci.CIEnvironmentContributingAction)

Example 2 with ProviderData

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;
}
Also used : FedMsgSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData) ProviderData(com.redhat.jenkins.plugins.ci.provider.data.ProviderData) ActiveMQSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData) ArrayList(java.util.ArrayList) DataBoundSetter(org.kohsuke.stapler.DataBoundSetter)

Example 3 with ProviderData

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);
    }
}
Also used : FedMsgSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData) ProviderData(com.redhat.jenkins.plugins.ci.provider.data.ProviderData) ActiveMQSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData) CITriggerThread(com.redhat.jenkins.plugins.ci.threads.CITriggerThread) JMSMessagingProvider(com.redhat.jenkins.plugins.ci.messaging.JMSMessagingProvider) IOException(java.io.IOException) AbstractProject(hudson.model.AbstractProject) IOException(java.io.IOException)

Example 4 with ProviderData

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;
}
Also used : FedMsgSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData) ProviderData(com.redhat.jenkins.plugins.ci.provider.data.ProviderData) ActiveMQSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData) CITriggerThread(com.redhat.jenkins.plugins.ci.threads.CITriggerThread) ArrayList(java.util.ArrayList) JMSMessagingProvider(com.redhat.jenkins.plugins.ci.messaging.JMSMessagingProvider)

Aggregations

ProviderData (com.redhat.jenkins.plugins.ci.provider.data.ProviderData)4 ActiveMQSubscriberProviderData (com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData)3 FedMsgSubscriberProviderData (com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData)3 JMSMessagingProvider (com.redhat.jenkins.plugins.ci.messaging.JMSMessagingProvider)2 CITriggerThread (com.redhat.jenkins.plugins.ci.threads.CITriggerThread)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AMQP (com.rabbitmq.client.AMQP)1 CancelCallback (com.rabbitmq.client.CancelCallback)1 Channel (com.rabbitmq.client.Channel)1 Connection (com.rabbitmq.client.Connection)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 DeliverCallback (com.rabbitmq.client.DeliverCallback)1 CIEnvironmentContributingAction (com.redhat.jenkins.plugins.ci.CIEnvironmentContributingAction)1 MsgCheck (com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck)1 RabbitMQMessage (com.redhat.jenkins.plugins.ci.messaging.data.RabbitMQMessage)1 SendResult (com.redhat.jenkins.plugins.ci.messaging.data.SendResult)1 RabbitMQPublisherProviderData (com.redhat.jenkins.plugins.ci.provider.data.RabbitMQPublisherProviderData)1 RabbitMQSubscriberProviderData (com.redhat.jenkins.plugins.ci.provider.data.RabbitMQSubscriberProviderData)1 PluginUtils (com.redhat.utils.PluginUtils)1