Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added connection check for plugin registration #1

Merged
merged 3 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions .github/workflows/buildAndPushImage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -54,30 +61,51 @@ 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");

connectionAttempt();

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));
}

/**
* 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 {
Expand All @@ -93,8 +121,29 @@ private AbstractMessageListenerContainer createListenerForRequestQueue(String re
listener.addQueueNames(requestQueueName);
listener.setMessageListener(messageListener);
listener.start();

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
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;
}
}
}
Loading