Search in sources :

Example 1 with RabbitMQMessage

use of com.redhat.jenkins.plugins.ci.messaging.data.RabbitMQMessage in project jms-messaging-plugin by jenkinsci.

the class RabbitMQMessagingWorker method sendMessage.

@Override
public SendResult sendMessage(Run<?, ?> build, TaskListener listener, ProviderData pdata) {
    RabbitMQPublisherProviderData pd = (RabbitMQPublisherProviderData) pdata;
    try {
        if (connection == null || !connection.isOpen()) {
            connect();
        }
        if (channel == null || !channel.isOpen()) {
            this.channel = connection.createChannel();
            log.info("Channel created.");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    String body = "";
    String msgId = "";
    // Fedora messaging wire format support
    Map<String, Object> headers = new HashMap<>();
    if (pd.isFedoraMessaging()) {
        headers.put("fedora_messaging_severity", pd.getSeverity());
        headers.put("fedora_messaging_schema", pd.getSchema());
        headers.put("sent_at", ZonedDateTime.now().toString());
    }
    System.out.println(headers);
    try {
        EnvVars env = new EnvVars();
        env.putAll(build.getEnvironment(listener));
        env.put("CI_NAME", build.getParent().getName());
        if (!build.isBuilding()) {
            env.put("CI_STATUS", (build.getResult() == Result.SUCCESS ? "passed" : "failed"));
            env.put("BUILD_STATUS", build.getResult().toString());
        }
        RabbitMQMessage msg = new RabbitMQMessage(PluginUtils.getSubstitutedValue(getTopic(provider), build.getEnvironment(listener)), PluginUtils.getSubstitutedValue(pd.getMessageContent(), env));
        msg.setTimestamp(System.currentTimeMillis() / 1000L);
        body = msg.getBodyJson();
        msgId = msg.getMsgId();
        try {
            channel.exchangeDeclarePassive(exchangeName);
            channel.basicPublish(exchangeName, msg.getTopic(), new AMQP.BasicProperties.Builder().headers(headers).messageId(msgId).build(), body.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            if (pd.isFailOnError()) {
                log.severe("Unhandled exception in perform: Failed to send message!");
                return new SendResult(false, msgId, body);
            }
        }
        log.fine("Message headers:\n" + headers);
        log.fine("JSON message:\n" + msg.toJson());
        listener.getLogger().println("Message id: " + msg.getMsgId());
        listener.getLogger().println("Message topic: " + msg.getTopic());
        listener.getLogger().println("Message headers:\n" + headers);
        listener.getLogger().println("JSON message body:\n" + body);
    } catch (Exception e) {
        if (pd.isFailOnError()) {
            log.severe("Unhandled exception in perform: ");
            log.severe(ExceptionUtils.getStackTrace(e));
            listener.fatalError("Unhandled exception in perform: ");
            listener.fatalError(ExceptionUtils.getStackTrace(e));
            return new SendResult(false, msgId, body);
        } else {
            log.warning("Unhandled exception in perform: ");
            log.warning(ExceptionUtils.getStackTrace(e));
            listener.error("Unhandled exception in perform: ");
            listener.error(ExceptionUtils.getStackTrace(e));
            return new SendResult(true, msgId, body);
        }
    } finally {
        try {
            channel.close();
        } catch (Exception e) {
            log.warning("Unhandled exception when closing channel: ");
            log.warning(ExceptionUtils.getStackTrace(e));
            listener.getLogger().println("exception in finally");
        }
    }
    return new SendResult(true, msgId, body);
}
Also used : EnvVars(hudson.EnvVars) HashMap(java.util.HashMap) AMQP(com.rabbitmq.client.AMQP) SendResult(com.redhat.jenkins.plugins.ci.messaging.data.SendResult) IOException(java.io.IOException) RabbitMQPublisherProviderData(com.redhat.jenkins.plugins.ci.provider.data.RabbitMQPublisherProviderData) IOException(java.io.IOException) RabbitMQMessage(com.redhat.jenkins.plugins.ci.messaging.data.RabbitMQMessage)

Example 2 with RabbitMQMessage

use of com.redhat.jenkins.plugins.ci.messaging.data.RabbitMQMessage in project jms-messaging-plugin by jenkinsci.

the class RabbitMQMessagingWorker method receive.

@Override
public void receive(String jobname, ProviderData pdata) {
    RabbitMQSubscriberProviderData pd = (RabbitMQSubscriberProviderData) pdata;
    int timeout = (pd.getTimeout() != null ? pd.getTimeout() : RabbitMQSubscriberProviderData.DEFAULT_TIMEOUT_IN_MINUTES) * 60 * 1000;
    if (interrupt) {
        log.info("we have been interrupted at start of receive");
        return;
    }
    // subscribe job
    while (!subscribe(jobname)) {
        if (!Thread.currentThread().isInterrupted()) {
            try {
                int WAIT_SECONDS = 2;
                Thread.sleep(WAIT_SECONDS * 1000);
            } catch (InterruptedException e) {
                // We were interrupted while waiting to retry. We will
                // jump ship on the next iteration.
                // NB: The interrupt flag was cleared when
                // InterruptedException was thrown. We have to
                // re-install it to make sure we eventually leave this
                // thread.
                Thread.currentThread().interrupt();
            }
        }
    }
    long lastSeenMessage = new Date().getTime();
    try {
        while ((new Date().getTime() - lastSeenMessage) < timeout) {
            if (!messageQueue.isEmpty()) {
                RabbitMQMessage data = messageQueue.poll();
                // Reset timer
                lastSeenMessage = data.getTimestamp().getTime();
                // 
                if (provider.verify(data.getBodyJson(), pd.getChecks(), jobname)) {
                    Map<String, String> params = new HashMap<>();
                    params.put("CI_MESSAGE", data.getBodyJson());
                    trigger(jobname, data.getBodyJson(), params);
                }
                channel.basicAck(data.getDeliveryTag(), false);
            } else {
                if (interrupt) {
                    log.info("We have been interrupted...");
                    break;
                }
            }
            TimeUnit.MILLISECONDS.sleep(500);
        }
    } catch (Exception e) {
        // for more info
        if (e.getClass() == InterruptedException.class) {
            Thread.currentThread().interrupt();
        }
        if (!Thread.currentThread().isInterrupted()) {
            // Something other than an interrupt causes this.
            // Unsubscribe, but stay in our loop and try to reconnect..
            log.log(Level.WARNING, "JMS exception raised, going to re-subscribe for job '" + jobname + "'.", e);
            // Try again next time.
            unsubscribe(jobname);
        }
    }
}
Also used : HashMap(java.util.HashMap) RabbitMQSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.RabbitMQSubscriberProviderData) Date(java.util.Date) RabbitMQMessage(com.redhat.jenkins.plugins.ci.messaging.data.RabbitMQMessage) IOException(java.io.IOException)

Example 3 with RabbitMQMessage

use of com.redhat.jenkins.plugins.ci.messaging.data.RabbitMQMessage 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 4 with RabbitMQMessage

use of com.redhat.jenkins.plugins.ci.messaging.data.RabbitMQMessage in project jms-messaging-plugin by jenkinsci.

the class RabbitMQMessagingWorker method subscribe.

@Override
public boolean subscribe(String jobname, String selector) {
    if (interrupt) {
        return true;
    }
    if (this.topic != null) {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                if (connection == null || !connection.isOpen()) {
                    if (!connect()) {
                        return false;
                    }
                }
                if (channel == null || !channel.isOpen()) {
                    this.channel = connection.createChannel();
                    log.info("Subscribing job '" + jobname + "' to " + this.topic + " topic.");
                    String queueName = getQueue(provider);
                    try {
                        // Check if queue exists
                        channel.queueDeclarePassive(queueName);
                    } catch (IOException e) {
                        // Request new queue - durable false, exclusive true, autodelete true
                        this.channel = connection.createChannel();
                        channel.queueDeclare(queueName, false, true, true, null);
                    }
                    channel.exchangeDeclarePassive(exchangeName);
                    channel.queueBind(queueName, exchangeName, this.topic);
                    // Create deliver callback to listen for messages
                    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
                        String json = new String(delivery.getBody(), StandardCharsets.UTF_8);
                        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);
                    };
                    this.consumerTag = channel.basicConsume(queueName, deliverCallback, (CancelCallback) null);
                    log.info("Successfully subscribed job '" + jobname + "' to topic '" + this.topic + "'.");
                } else {
                    log.info("Already subscribed job '" + jobname + "' to topic '" + this.topic + "'.");
                }
                return true;
            } catch (Exception ex) {
                // Either we were interrupted, or something else went
                // wrong. If we were interrupted, then we will jump ship
                // on the next iteration. If something else happened,
                // then we just unsubscribe here, sleep, so that we may
                // try again on the next iteration.
                log.log(Level.SEVERE, "Eexception raised while subscribing job '" + jobname + "', retrying in " + RETRY_MINUTES + " minutes.", ex);
                if (!Thread.currentThread().isInterrupted()) {
                    unsubscribe(jobname);
                    try {
                        Thread.sleep(RETRY_MINUTES * 60 * 1000);
                    } catch (InterruptedException ie) {
                        // We were interrupted while waiting to retry.
                        // We will jump ship on the next iteration.
                        // NB: The interrupt flag was cleared when
                        // InterruptedException was thrown. We have to
                        // re-install it to make sure we eventually
                        // leave this thread.
                        Thread.currentThread().interrupt();
                    }
                }
            }
        }
    }
    return false;
}
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) CancelCallback(com.rabbitmq.client.CancelCallback) DeliverCallback(com.rabbitmq.client.DeliverCallback) IOException(java.io.IOException) RabbitMQMessage(com.redhat.jenkins.plugins.ci.messaging.data.RabbitMQMessage) Date(java.util.Date) IOException(java.io.IOException)

Aggregations

RabbitMQMessage (com.redhat.jenkins.plugins.ci.messaging.data.RabbitMQMessage)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 AMQP (com.rabbitmq.client.AMQP)3 SendResult (com.redhat.jenkins.plugins.ci.messaging.data.SendResult)3 RabbitMQPublisherProviderData (com.redhat.jenkins.plugins.ci.provider.data.RabbitMQPublisherProviderData)3 RabbitMQSubscriberProviderData (com.redhat.jenkins.plugins.ci.provider.data.RabbitMQSubscriberProviderData)3 EnvVars (hudson.EnvVars)3 Date (java.util.Date)3 CancelCallback (com.rabbitmq.client.CancelCallback)2 Channel (com.rabbitmq.client.Channel)2 Connection (com.rabbitmq.client.Connection)2 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)2 DeliverCallback (com.rabbitmq.client.DeliverCallback)2 CIEnvironmentContributingAction (com.redhat.jenkins.plugins.ci.CIEnvironmentContributingAction)2 MsgCheck (com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck)2 ProviderData (com.redhat.jenkins.plugins.ci.provider.data.ProviderData)2 PluginUtils (com.redhat.utils.PluginUtils)2 Result (hudson.model.Result)2 Run (hudson.model.Run)2