From a9346716463771e1b2723417db80e72231c0c51f Mon Sep 17 00:00:00 2001 From: Arikuma Date: Sun, 4 Aug 2024 17:02:16 +0200 Subject: [PATCH 1/3] Added connection check for plugin registration --- .../PluginRegistrationRunner.java | 63 ++++++++++++++----- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/src/main/java/ust/tad/helmplugin/registration/PluginRegistrationRunner.java b/src/main/java/ust/tad/helmplugin/registration/PluginRegistrationRunner.java index 0b392ff..59f3ccb 100644 --- a/src/main/java/ust/tad/helmplugin/registration/PluginRegistrationRunner.java +++ b/src/main/java/ust/tad/helmplugin/registration/PluginRegistrationRunner.java @@ -23,14 +23,18 @@ import ust.tad.helmplugin.analysistask.AnalysisTaskReceiver; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; + /** * Runner that is executed at application startup to register this plugin at the Analysis Manager. */ @Component public class PluginRegistrationRunner implements ApplicationRunner{ - + private static final Logger LOG = - LoggerFactory.getLogger(PluginRegistrationRunner.class); + LoggerFactory.getLogger(PluginRegistrationRunner.class); @Autowired private GenericApplicationContext context; @@ -44,6 +48,9 @@ public class PluginRegistrationRunner implements ApplicationRunner{ @Autowired private AnalysisTaskReceiver analysisTaskReceiver; + @Value("${analysis-manager.plugin-registration.url}") + private String pluginRegistrationURI; + @Value("${plugin.technology}") private String pluginTechnology; @@ -54,30 +61,42 @@ public class PluginRegistrationRunner implements ApplicationRunner{ private String responseExchangeName; @Override - public void run(ApplicationArguments args) throws JsonProcessingException { + public void run(ApplicationArguments args) throws JsonProcessingException, InterruptedException { LOG.info("Registering Plugin"); + String host = pluginRegistrationURI.split(":")[1].replace("/",""); + int port = Integer.parseInt(pluginRegistrationURI.split(":")[2].split("/")[0]); + int maxAttempts = 20; + + for (int attempt = 0; attempt < maxAttempts; attempt++) + { + if(isServiceReachable(host, port, 5000, attempt + 1, maxAttempts)) + { + break; + } + Thread.sleep(2000); + } String body = createPluginRegistrationBody(); PluginRegistrationResponse response = pluginRegistrationApiClient.post() - .contentType(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON) - .body(BodyInserters.fromValue(body)) - .retrieve() - .bodyToMono(PluginRegistrationResponse.class) - .block(); + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .body(BodyInserters.fromValue(body)) + .retrieve() + .bodyToMono(PluginRegistrationResponse.class) + .block(); LOG.info("Received response: " + response.toString()); - + AbstractMessageListenerContainer requestQueueListener = createListenerForRequestQueue( - response.getRequestQueueName(), - message -> analysisTaskReceiver.receive(message)); + response.getRequestQueueName(), + message -> analysisTaskReceiver.receive(message)); context.registerBean("requestQueueListener", requestQueueListener.getClass(), requestQueueListener); - context.registerBean(responseExchangeName, FanoutExchange.class, - () -> new FanoutExchange(response.getResponseExchangeName(), true, false)); + context.registerBean(responseExchangeName, FanoutExchange.class, + () -> new FanoutExchange(response.getResponseExchangeName(), true, false)); } private String createPluginRegistrationBody() throws JsonProcessingException { @@ -93,8 +112,20 @@ private AbstractMessageListenerContainer createListenerForRequestQueue(String re listener.addQueueNames(requestQueueName); listener.setMessageListener(messageListener); listener.start(); - + return listener; } - + + public static boolean isServiceReachable(String hostNameOrIP, int port, int timeout, int attempt, int maxAttempts) { + try (Socket socket = new Socket()) { + // Attempt to connect to the host and port within the given timeout + socket.connect(new InetSocketAddress(hostNameOrIP, port), timeout); + LOG.info("Service " + hostNameOrIP + " is reachable. Start registration." ); + return true; + } catch (IOException e) { + // Connection failed or timed out + LOG.info("Service " + hostNameOrIP + " isn't reachable. Attempt (" + attempt + "/" + maxAttempts +")"); + return false; + } + } } From 2df91a653467da8811197e13015576d59e2b3c36 Mon Sep 17 00:00:00 2001 From: Arikuma Date: Sun, 11 Aug 2024 17:58:50 +0200 Subject: [PATCH 2/3] Adjusted buildAndPushImage.yml so dev branch is also an image --- .github/workflows/buildAndPushImage.yml | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/.github/workflows/buildAndPushImage.yml b/.github/workflows/buildAndPushImage.yml index c8d2017..f3716b0 100644 --- a/.github/workflows/buildAndPushImage.yml +++ b/.github/workflows/buildAndPushImage.yml @@ -2,12 +2,16 @@ name: buildAndPushImage on: push: - branches: + branches: - 'main' - + - 'dev' + pull_request: + branches: + - 'main' + env: IMAGE_NAME: helm-plugin - + IMAGE_TAG: ${{ github.ref == 'refs/heads/main' && 'latest' || 'testing' }} jobs: build-with-paketo-push-2-dockerhub: runs-on: ubuntu-latest @@ -24,8 +28,8 @@ jobs: - name: Build app with pack CLI using Buildpack Cache image (see https://buildpacks.io/docs/app-developer-guide/using-cache-image/) & publish to Docker Hub run: | - pack build index.docker.io/well5a/$IMAGE_NAME:latest \ + pack build index.docker.io/well5a/$IMAGE_NAME:$IMAGE_TAG \ --builder paketobuildpacks/builder:base \ --path . \ - --cache-image index.docker.io/well5a/$IMAGE_NAME-paketo-cache-image:latest \ + --cache-image index.docker.io/well5a/$IMAGE_NAME-paketo-cache-image:$IMAGE_TAG \ --publish From 511ec7651e0bb123e0ffe2eadd1107ff3b810eb8 Mon Sep 17 00:00:00 2001 From: Arikuma Date: Sun, 11 Aug 2024 18:18:42 +0200 Subject: [PATCH 3/3] Added JavaDoc & refactored Method --- .../PluginRegistrationRunner.java | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/main/java/ust/tad/helmplugin/registration/PluginRegistrationRunner.java b/src/main/java/ust/tad/helmplugin/registration/PluginRegistrationRunner.java index 59f3ccb..0dedb69 100644 --- a/src/main/java/ust/tad/helmplugin/registration/PluginRegistrationRunner.java +++ b/src/main/java/ust/tad/helmplugin/registration/PluginRegistrationRunner.java @@ -65,18 +65,8 @@ public void run(ApplicationArguments args) throws JsonProcessingException, Inter LOG.info("Registering Plugin"); - String host = pluginRegistrationURI.split(":")[1].replace("/",""); - int port = Integer.parseInt(pluginRegistrationURI.split(":")[2].split("/")[0]); - int maxAttempts = 20; + connectionAttempt(); - for (int attempt = 0; attempt < maxAttempts; attempt++) - { - if(isServiceReachable(host, port, 5000, attempt + 1, maxAttempts)) - { - break; - } - Thread.sleep(2000); - } String body = createPluginRegistrationBody(); PluginRegistrationResponse response = pluginRegistrationApiClient.post() @@ -99,6 +89,25 @@ public void run(ApplicationArguments args) throws JsonProcessingException, Inter () -> new FanoutExchange(response.getResponseExchangeName(), true, false)); } + /** + * Try to reach the service 20 times (maxAttempts). + * @throws InterruptedException + */ + private void connectionAttempt() throws InterruptedException { + String host = pluginRegistrationURI.split(":")[1].replace("/",""); + int port = Integer.parseInt(pluginRegistrationURI.split(":")[2].split("/")[0]); + int maxAttempts = 20; + + for (int attempt = 0; attempt < maxAttempts; attempt++) + { + if(isServiceReachable(host, port, 5000, attempt + 1, maxAttempts)) + { + break; + } + Thread.sleep(2000); + } + } + private String createPluginRegistrationBody() throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); ObjectNode plugin = mapper.createObjectNode(); @@ -116,6 +125,15 @@ private AbstractMessageListenerContainer createListenerForRequestQueue(String re return listener; } + /** + * Checks if Service is reachable. If so, it returns true so the plugin can start connecting otherwise it returns false. + * @param hostNameOrIP + * @param port + * @param timeout + * @param attempt + * @param maxAttempts + * @return boolean + */ public static boolean isServiceReachable(String hostNameOrIP, int port, int timeout, int attempt, int maxAttempts) { try (Socket socket = new Socket()) { // Attempt to connect to the host and port within the given timeout