/*
 * Copyright 2019 EPAM Systems
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epam.ta.reportportal.ws.rabbit;

import com.epam.ta.reportportal.core.configs.Conditions;
import com.epam.ta.reportportal.exception.ReportPortalException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ShutdownSignalException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.ListenerContainerConsumerFailedEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * Finds a queue that doesn't have any connected consumers and set's it
 * to consumer that should be restarted, so it can be registered
 * with a different queue.
 *
 * @author <a href="mailto:pavel_bortnik@epam.com">Pavel Bortnik</a>
 */
@Component
@Conditional(Conditions.NotTestCondition.class)
public class ConsumerEventListener implements ApplicationListener<ListenerContainerConsumerFailedEvent> {

	private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerEventListener.class);

	@Autowired
	@Qualifier("queues")
	private List<Queue> queues;

	@Autowired
	private ConnectionFactory connectionFactory;

	@Override
	public void onApplicationEvent(ListenerContainerConsumerFailedEvent event) {
		Object source = event.getSource();
		if (source instanceof AbstractMessageListenerContainer) {
			AbstractMessageListenerContainer listenerContainer = (AbstractMessageListenerContainer) source;
			Throwable throwable = event.getThrowable();
			if (throwable.getCause() instanceof IOException && throwable.getCause().getCause() instanceof ShutdownSignalException
					&& throwable.getCause().getCause().getMessage().contains("in exclusive use")) {
				for (Queue q : queues) {
					if (getQueueConsumerCount(q) == 0) {
						listenerContainer.setQueueNames(q.getName());
						LOGGER.info("Restarting consumer with a queue {}", q.getName());
					}
				}
			}
		}
	}

	private int getQueueConsumerCount(Queue queue) {
		Channel channel = connectionFactory.createConnection().createChannel(false);
		try {
			return channel.queueDeclarePassive(queue.getName()).getConsumerCount();
		} catch (IOException e) {
			throw new ReportPortalException(e.getMessage());
		}
	}
}

@@ -1520,16 +1520,62 @@
 il.List;
+%0Aimport java.util.concurrent.TimeoutException;
 %0A%0A/**%0A *
@@ -3004,16 +3004,21 @@
 ue) %7B%0A%09%09
+try (
 Channel 
@@ -3088,15 +3088,9 @@
 lse)
-;%0A%09%09try
+)
  %7B%0A%09
@@ -3163,16 +3163,16 @@
 ount();%0A
-
 %09%09%7D catc
@@ -3186,16 +3186,35 @@
 ception 
+%7C TimeoutException 
 e) %7B%0A%09%09%09

enable channels autoclosing