Skip to content

Commit

Permalink
chore: wait until elastic is up
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas committed Nov 7, 2023
1 parent 9a31a39 commit be7cd1a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 25 deletions.
13 changes: 8 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ services:
- "9200:9200"
environment:
- discovery.type=single-node
healthcheck:
test: [ "CMD-SHELL", "curl --silent --fail localhost:9200/_cluster/health || exit 1" ]
interval: 30s
timeout: 10s
retries: 5

backoffice_backend_server_java:
container_name: codely-java_ddd_example-backoffice_backend_server
Expand All @@ -55,6 +50,7 @@ services:
- "8040:8040"
volumes:
- .:/app:delegated
- backoffice_backend_gradle_cache:/app/.gradle
depends_on:
- shared_mysql
- shared_rabbitmq
Expand All @@ -71,6 +67,7 @@ services:
- "8041:8041"
volumes:
- .:/app:delegated
- backoffice_frontend_gradle_cache:/app/.gradle
depends_on:
- shared_mysql
- shared_rabbitmq
Expand All @@ -87,8 +84,14 @@ services:
- "8030:8030"
volumes:
- .:/app:delegated
- mooc_backend_gradle_cache:/app/.gradle
depends_on:
- shared_mysql
- shared_rabbitmq
- backoffice_elasticsearch
command: ["./gradlew", "bootRun", "--args", "mooc_backend server"]

volumes:
backoffice_backend_gradle_cache:
backoffice_frontend_gradle_cache:
mooc_backend_gradle_cache:
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import tv.codely.shared.domain.Utils;
import tv.codely.shared.infrastructure.config.Parameter;
import tv.codely.shared.infrastructure.config.ParameterNotExist;
import tv.codely.shared.infrastructure.elasticsearch.ElasticsearchClient;
Expand All @@ -29,27 +30,33 @@ public BackofficeElasticsearchConfiguration(Parameter config, ResourcePatternRes
}

@Bean
public ElasticsearchClient elasticsearchClient() throws ParameterNotExist, IOException {
ElasticsearchClient client = new ElasticsearchClient(
new RestHighLevelClient(
RestClient.builder(
new HttpHost(
config.get("BACKOFFICE_ELASTICSEARCH_HOST"),
config.getInt("BACKOFFICE_ELASTICSEARCH_PORT"),
"http"
)
)
),
RestClient.builder(
new HttpHost(
config.get("BACKOFFICE_ELASTICSEARCH_HOST"),
config.getInt("BACKOFFICE_ELASTICSEARCH_PORT"),
"http"
)).build(),
config.get("BACKOFFICE_ELASTICSEARCH_INDEX_PREFIX")
);
public ElasticsearchClient elasticsearchClient() throws ParameterNotExist, Exception {
ElasticsearchClient client = new ElasticsearchClient(
new RestHighLevelClient(
RestClient.builder(
new HttpHost(
config.get("BACKOFFICE_ELASTICSEARCH_HOST"),
config.getInt("BACKOFFICE_ELASTICSEARCH_PORT"),
"http"
)
)
),
RestClient.builder(
new HttpHost(
config.get("BACKOFFICE_ELASTICSEARCH_HOST"),
config.getInt("BACKOFFICE_ELASTICSEARCH_PORT"),
"http"
)).build(),
config.get("BACKOFFICE_ELASTICSEARCH_INDEX_PREFIX")
);

generateIndexIfNotExists(client, "backoffice");
Utils.retry(10, 10000, () -> {
try {
generateIndexIfNotExists(client, "backoffice");
} catch (IOException e) {
throw new RuntimeException(e);
}
});

return client;
}
Expand Down
23 changes: 23 additions & 0 deletions src/shared/main/tv/codely/shared/domain/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,27 @@ public static String toCamel(String text) {
public static String toCamelFirstLower(String text) {
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, text);
}

public static void retry(int numberOfRetries, long waitTimeInMillis, Runnable operation) throws Exception {
for (int i = 0; i < numberOfRetries; i++) {
try {
operation.run();
return; // Success, exit the method
} catch (Exception ex) {
System.out.println("Retry " + (i + 1) + "/" + numberOfRetries + " fail. Retrying…");
if (i >= numberOfRetries - 1) {
throw ex;
}

try {
Thread.sleep(waitTimeInMillis);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();

throw new Exception("Operation interrupted while retrying", ie);
}
}
}
}

}

0 comments on commit be7cd1a

Please sign in to comment.