Skip to content

Commit 610154c

Browse files
Merge pull request #71 from freenowtech/sergioasantiago/60_fix_rabbitmq_listener_startup
Fix rabbitmq listener to start after the loading of the plugins
2 parents 6dd96f0 + 9ee4a78 commit 610154c

File tree

7 files changed

+72
-16
lines changed

7 files changed

+72
-16
lines changed

sauron-service/src/main/java/com/freenow/sauron/config/PluginsLoadedIndicator.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.freenow.sauron.config;
22

3+
import com.freenow.sauron.plugins.PluginsLoadedEvent;
34
import java.util.concurrent.atomic.AtomicBoolean;
45
import org.springframework.boot.actuate.health.Health;
56
import org.springframework.boot.actuate.health.HealthIndicator;
7+
import org.springframework.context.event.EventListener;
68
import org.springframework.stereotype.Component;
79

810
@Component
@@ -20,8 +22,9 @@ public Health health()
2022
}
2123

2224

23-
public static void healthy()
25+
@EventListener
26+
public void onPluginLoadedEvent(PluginsLoadedEvent event)
2427
{
25-
healthy.compareAndExchange(false, true);
28+
healthy.set(event.isSuccess());
2629
}
2730
}

sauron-service/src/main/java/com/freenow/sauron/config/RequestHandlerConfig.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.freenow.sauron.handler.impl.SpringEventsRequestHandler;
66
import lombok.extern.slf4j.Slf4j;
77
import org.springframework.amqp.rabbit.core.RabbitTemplate;
8+
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
89
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
910
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
1011
import org.springframework.context.ApplicationEventPublisher;
@@ -17,10 +18,10 @@ public class RequestHandlerConfig
1718
{
1819
@Bean
1920
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "enabled", havingValue = "true")
20-
public RequestHandler rabbitmqRequestHandler(RabbitTemplate template)
21+
public RequestHandler rabbitmqRequestHandler(RabbitTemplate template, RabbitListenerEndpointRegistry registry)
2122
{
2223
log.info("Rabbitmq is enabled as request handler. Your pipeline build requests will be queued in Rabbitmq.");
23-
return new RabbitmqRequestHandler(template);
24+
return new RabbitmqRequestHandler(template, registry);
2425
}
2526

2627

@@ -29,7 +30,7 @@ public RequestHandler rabbitmqRequestHandler(RabbitTemplate template)
2930
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "enabled", havingValue = "false", matchIfMissing = true)
3031
public RequestHandler springEventRequestHandler(ApplicationEventPublisher applicationEventPublisher)
3132
{
32-
log.info("Spring events request handler is enabled. You requests will be processed by spring event dispatcher thread pool");
33+
log.info("Spring events request handler is enabled. You pipeline build requests will be processed by spring event dispatcher thread pool.");
3334
return new SpringEventsRequestHandler(applicationEventPublisher);
3435
}
3536
}

sauron-service/src/main/java/com/freenow/sauron/handler/impl/RabbitmqRequestHandler.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import com.freenow.sauron.datatransferobject.BuildRequest;
44
import com.freenow.sauron.handler.RequestHandler;
5+
import com.freenow.sauron.plugins.PluginsLoadedEvent;
56
import java.util.function.Consumer;
67
import lombok.Setter;
78
import lombok.extern.slf4j.Slf4j;
89
import org.springframework.amqp.rabbit.annotation.RabbitListener;
910
import org.springframework.amqp.rabbit.connection.SimpleResourceHolder;
1011
import org.springframework.amqp.rabbit.core.RabbitTemplate;
12+
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
13+
import org.springframework.context.event.EventListener;
1114

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

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

23+
private static final String LISTENER_ID = "sauron-consumer";
24+
25+
private static final String AUTOSTARTUP = "false";
26+
2027
private final RabbitTemplate rabbitTemplate;
2128

29+
private final RabbitListenerEndpointRegistry registry;
30+
2231
private Consumer<BuildRequest> consumer;
2332

2433

25-
public RabbitmqRequestHandler(RabbitTemplate rabbitTemplate)
34+
public RabbitmqRequestHandler(RabbitTemplate rabbitTemplate, RabbitListenerEndpointRegistry registry)
2635
{
2736
this.rabbitTemplate = rabbitTemplate;
37+
this.registry = registry;
2838
}
2939

3040

3141
@Override
3242
public void handle(BuildRequest request)
3343
{
34-
log.debug(String.format("Handling request in Rabbitmq Request Handler. (%s) %s - %s:%s",
44+
log.debug(String.format(
45+
"Handling request in Rabbitmq Request Handler. (%s) %s - %s:%s",
3546
request.getBuildId(),
3647
request.getEnvironment(),
3748
request.getServiceName(),
@@ -44,12 +55,15 @@ public void handle(BuildRequest request)
4455

4556

4657
@RabbitListener(
58+
id = LISTENER_ID,
4759
containerFactory = ASYNC_CONTAINER_FACTORY,
48-
queues = SAURON_QUEUE_NAME
60+
queues = SAURON_QUEUE_NAME,
61+
autoStartup = AUTOSTARTUP
4962
)
5063
public void consume(BuildRequest request)
5164
{
52-
log.debug(String.format("Rabbitmq consumed message from the queue. (%s) %s - %s:%s",
65+
log.debug(String.format(
66+
"Rabbitmq consumed message from the queue. (%s) %s - %s:%s",
5367
request.getBuildId(),
5468
request.getEnvironment(),
5569
request.getServiceName(),
@@ -64,4 +78,18 @@ public void consume(BuildRequest request)
6478
log.error("Consumer is not set. Message will be discarded.");
6579
}
6680
}
81+
82+
83+
@EventListener
84+
public void onPluginLoadedEvent(PluginsLoadedEvent event)
85+
{
86+
if (event.isSuccess())
87+
{
88+
registry.getListenerContainer(LISTENER_ID).start();
89+
}
90+
else
91+
{
92+
registry.getListenerContainer(LISTENER_ID).stop();
93+
}
94+
}
6795
}

sauron-service/src/main/java/com/freenow/sauron/handler/impl/SpringEventsRequestHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.springframework.context.event.EventListener;
1010
import org.springframework.scheduling.annotation.Async;
1111

12-
1312
@Slf4j
1413
@Setter
1514
public class SpringEventsRequestHandler implements RequestHandler
@@ -28,7 +27,8 @@ public SpringEventsRequestHandler(ApplicationEventPublisher applicationEventPubl
2827
@Override
2928
public void handle(BuildRequest request)
3029
{
31-
log.debug(String.format("Handling request in Spring Events Request Handler. (%s) %s - %s:%s",
30+
log.debug(String.format(
31+
"Handling request in Spring Events Request Handler. (%s) %s - %s:%s",
3232
request.getBuildId(),
3333
request.getEnvironment(),
3434
request.getServiceName(),
@@ -42,7 +42,8 @@ public void handle(BuildRequest request)
4242
@EventListener
4343
public void consume(BuildRequest request)
4444
{
45-
log.debug(String.format("Spring Events Listener consumed a message from events. (%s) %s - %s:%s",
45+
log.debug(String.format(
46+
"Spring Events Listener consumed a message from events. (%s) %s - %s:%s",
4647
request.getBuildId(),
4748
request.getEnvironment(),
4849
request.getServiceName(),

sauron-service/src/main/java/com/freenow/sauron/plugins/AutomaticPluginUpdater.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.freenow.sauron.plugins;
22

3-
import com.freenow.sauron.config.PluginsLoadedIndicator;
43
import lombok.extern.slf4j.Slf4j;
54
import org.pf4j.PluginManager;
65
import org.pf4j.PluginRuntimeException;
76
import org.pf4j.update.PluginInfo;
87
import org.pf4j.update.UpdateManager;
98
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.context.ApplicationEventPublisher;
1010
import org.springframework.scheduling.annotation.Scheduled;
1111
import org.springframework.stereotype.Component;
1212

@@ -18,20 +18,25 @@ public class AutomaticPluginUpdater
1818

1919
private final PluginManager pluginManager;
2020

21+
private final ApplicationEventPublisher applicationEventPublisher;
22+
2123
private static final long PLUGIN_UPDATE_DELAY_MILLIS = 300000;
2224

2325

2426
@Autowired
25-
public AutomaticPluginUpdater(UpdateManager updateManager, PluginManager pluginManager)
27+
public AutomaticPluginUpdater(UpdateManager updateManager, PluginManager pluginManager, ApplicationEventPublisher applicationEventPublisher)
2628
{
2729
this.updateManager = updateManager;
2830
this.pluginManager = pluginManager;
31+
this.applicationEventPublisher = applicationEventPublisher;
2932
}
3033

3134

3235
@Scheduled(fixedDelay = PLUGIN_UPDATE_DELAY_MILLIS)
3336
public void update()
3437
{
38+
PluginsLoadedEvent event = new PluginsLoadedEvent();
39+
3540
try
3641
{
3742
log.debug("Searching plugins in plugin repository...");
@@ -42,14 +47,19 @@ public void update()
4247

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

45-
PluginsLoadedIndicator.healthy();
50+
event.setSuccess(true);
51+
4652
}
4753
catch (Exception e)
4854
{
4955
log.error("Cannot load plugins '{}'", e.getMessage(), e);
56+
event.setSuccess(false);
5057
}
58+
59+
applicationEventPublisher.publishEvent(event);
5160
}
5261

62+
5363
public void forceReload(String pluginId)
5464
{
5565
try
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.freenow.sauron.plugins;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class PluginsLoadedEvent
7+
{
8+
private boolean success;
9+
}

sauron-service/src/test/java/com/freenow/sauron/service/AutomaticPluginUpdaterTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.pf4j.update.FileVerifier;
3434
import org.pf4j.update.PluginInfo;
3535
import org.pf4j.update.UpdateManager;
36+
import org.springframework.context.ApplicationEventPublisher;
3637

3738
import static org.mockito.ArgumentMatchers.any;
3839
import static org.mockito.Mockito.doNothing;
@@ -61,6 +62,9 @@ public class AutomaticPluginUpdaterTest
6162
@Mock
6263
private ArtifactoryDownloader downloader;
6364

65+
@Mock
66+
private ApplicationEventPublisher applicationEventPublisher;
67+
6468
private ArtifactoryRepository repository;
6569

6670
private UpdateManager updateManager;
@@ -93,7 +97,7 @@ public void initEach() throws IOException
9397

9498
PluginManager pluginManager = new DefaultPluginManager();
9599
updateManager = spy(new UpdateManager(pluginManager, Collections.singletonList(repository)));
96-
automaticPluginUpdater = new AutomaticPluginUpdater(updateManager, pluginManager);
100+
automaticPluginUpdater = new AutomaticPluginUpdater(updateManager, pluginManager, applicationEventPublisher);
97101
}
98102

99103

0 commit comments

Comments
 (0)