Skip to content

Commit

Permalink
Merge pull request #71 from freenowtech/sergioasantiago/60_fix_rabbit…
Browse files Browse the repository at this point in the history
…mq_listener_startup

Fix rabbitmq listener to start after the loading of the plugins
  • Loading branch information
sergioasantiago authored Jul 27, 2021
2 parents 6dd96f0 + 9ee4a78 commit 610154c
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.freenow.sauron.config;

import com.freenow.sauron.plugins.PluginsLoadedEvent;
import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -20,8 +22,9 @@ public Health health()
}


public static void healthy()
@EventListener
public void onPluginLoadedEvent(PluginsLoadedEvent event)
{
healthy.compareAndExchange(false, true);
healthy.set(event.isSuccess());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.freenow.sauron.handler.impl.SpringEventsRequestHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationEventPublisher;
Expand All @@ -17,10 +18,10 @@ public class RequestHandlerConfig
{
@Bean
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "enabled", havingValue = "true")
public RequestHandler rabbitmqRequestHandler(RabbitTemplate template)
public RequestHandler rabbitmqRequestHandler(RabbitTemplate template, RabbitListenerEndpointRegistry registry)
{
log.info("Rabbitmq is enabled as request handler. Your pipeline build requests will be queued in Rabbitmq.");
return new RabbitmqRequestHandler(template);
return new RabbitmqRequestHandler(template, registry);
}


Expand All @@ -29,7 +30,7 @@ public RequestHandler rabbitmqRequestHandler(RabbitTemplate template)
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "enabled", havingValue = "false", matchIfMissing = true)
public RequestHandler springEventRequestHandler(ApplicationEventPublisher applicationEventPublisher)
{
log.info("Spring events request handler is enabled. You requests will be processed by spring event dispatcher thread pool");
log.info("Spring events request handler is enabled. You pipeline build requests will be processed by spring event dispatcher thread pool.");
return new SpringEventsRequestHandler(applicationEventPublisher);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import com.freenow.sauron.datatransferobject.BuildRequest;
import com.freenow.sauron.handler.RequestHandler;
import com.freenow.sauron.plugins.PluginsLoadedEvent;
import java.util.function.Consumer;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.SimpleResourceHolder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.context.event.EventListener;

import static com.freenow.sauron.utils.Constants.SAURON_QUEUE_NAME;

Expand All @@ -17,21 +20,29 @@ public class RabbitmqRequestHandler implements RequestHandler
{
private static final String ASYNC_CONTAINER_FACTORY = "eventBus";

private static final String LISTENER_ID = "sauron-consumer";

private static final String AUTOSTARTUP = "false";

private final RabbitTemplate rabbitTemplate;

private final RabbitListenerEndpointRegistry registry;

private Consumer<BuildRequest> consumer;


public RabbitmqRequestHandler(RabbitTemplate rabbitTemplate)
public RabbitmqRequestHandler(RabbitTemplate rabbitTemplate, RabbitListenerEndpointRegistry registry)
{
this.rabbitTemplate = rabbitTemplate;
this.registry = registry;
}


@Override
public void handle(BuildRequest request)
{
log.debug(String.format("Handling request in Rabbitmq Request Handler. (%s) %s - %s:%s",
log.debug(String.format(
"Handling request in Rabbitmq Request Handler. (%s) %s - %s:%s",
request.getBuildId(),
request.getEnvironment(),
request.getServiceName(),
Expand All @@ -44,12 +55,15 @@ public void handle(BuildRequest request)


@RabbitListener(
id = LISTENER_ID,
containerFactory = ASYNC_CONTAINER_FACTORY,
queues = SAURON_QUEUE_NAME
queues = SAURON_QUEUE_NAME,
autoStartup = AUTOSTARTUP
)
public void consume(BuildRequest request)
{
log.debug(String.format("Rabbitmq consumed message from the queue. (%s) %s - %s:%s",
log.debug(String.format(
"Rabbitmq consumed message from the queue. (%s) %s - %s:%s",
request.getBuildId(),
request.getEnvironment(),
request.getServiceName(),
Expand All @@ -64,4 +78,18 @@ public void consume(BuildRequest request)
log.error("Consumer is not set. Message will be discarded.");
}
}


@EventListener
public void onPluginLoadedEvent(PluginsLoadedEvent event)
{
if (event.isSuccess())
{
registry.getListenerContainer(LISTENER_ID).start();
}
else
{
registry.getListenerContainer(LISTENER_ID).stop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;


@Slf4j
@Setter
public class SpringEventsRequestHandler implements RequestHandler
Expand All @@ -28,7 +27,8 @@ public SpringEventsRequestHandler(ApplicationEventPublisher applicationEventPubl
@Override
public void handle(BuildRequest request)
{
log.debug(String.format("Handling request in Spring Events Request Handler. (%s) %s - %s:%s",
log.debug(String.format(
"Handling request in Spring Events Request Handler. (%s) %s - %s:%s",
request.getBuildId(),
request.getEnvironment(),
request.getServiceName(),
Expand All @@ -42,7 +42,8 @@ public void handle(BuildRequest request)
@EventListener
public void consume(BuildRequest request)
{
log.debug(String.format("Spring Events Listener consumed a message from events. (%s) %s - %s:%s",
log.debug(String.format(
"Spring Events Listener consumed a message from events. (%s) %s - %s:%s",
request.getBuildId(),
request.getEnvironment(),
request.getServiceName(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.freenow.sauron.plugins;

import com.freenow.sauron.config.PluginsLoadedIndicator;
import lombok.extern.slf4j.Slf4j;
import org.pf4j.PluginManager;
import org.pf4j.PluginRuntimeException;
import org.pf4j.update.PluginInfo;
import org.pf4j.update.UpdateManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

Expand All @@ -18,20 +18,25 @@ public class AutomaticPluginUpdater

private final PluginManager pluginManager;

private final ApplicationEventPublisher applicationEventPublisher;

private static final long PLUGIN_UPDATE_DELAY_MILLIS = 300000;


@Autowired
public AutomaticPluginUpdater(UpdateManager updateManager, PluginManager pluginManager)
public AutomaticPluginUpdater(UpdateManager updateManager, PluginManager pluginManager, ApplicationEventPublisher applicationEventPublisher)
{
this.updateManager = updateManager;
this.pluginManager = pluginManager;
this.applicationEventPublisher = applicationEventPublisher;
}


@Scheduled(fixedDelay = PLUGIN_UPDATE_DELAY_MILLIS)
public void update()
{
PluginsLoadedEvent event = new PluginsLoadedEvent();

try
{
log.debug("Searching plugins in plugin repository...");
Expand All @@ -42,14 +47,19 @@ public void update()

updateManager.getAvailablePlugins().parallelStream().forEach(this::installPlugin);

PluginsLoadedIndicator.healthy();
event.setSuccess(true);

}
catch (Exception e)
{
log.error("Cannot load plugins '{}'", e.getMessage(), e);
event.setSuccess(false);
}

applicationEventPublisher.publishEvent(event);
}


public void forceReload(String pluginId)
{
try
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.freenow.sauron.plugins;

import lombok.Data;

@Data
public class PluginsLoadedEvent
{
private boolean success;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.pf4j.update.FileVerifier;
import org.pf4j.update.PluginInfo;
import org.pf4j.update.UpdateManager;
import org.springframework.context.ApplicationEventPublisher;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
Expand Down Expand Up @@ -61,6 +62,9 @@ public class AutomaticPluginUpdaterTest
@Mock
private ArtifactoryDownloader downloader;

@Mock
private ApplicationEventPublisher applicationEventPublisher;

private ArtifactoryRepository repository;

private UpdateManager updateManager;
Expand Down Expand Up @@ -93,7 +97,7 @@ public void initEach() throws IOException

PluginManager pluginManager = new DefaultPluginManager();
updateManager = spy(new UpdateManager(pluginManager, Collections.singletonList(repository)));
automaticPluginUpdater = new AutomaticPluginUpdater(updateManager, pluginManager);
automaticPluginUpdater = new AutomaticPluginUpdater(updateManager, pluginManager, applicationEventPublisher);
}


Expand Down

0 comments on commit 610154c

Please sign in to comment.