diff --git a/docker-compose.yml b/docker-compose.yml index eb951c24..25ca2bc2 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 @@ -55,6 +50,7 @@ services: - "8040:8040" volumes: - .:/app:delegated + - backoffice_backend_gradle_cache:/app/.gradle depends_on: - shared_mysql - shared_rabbitmq @@ -71,6 +67,7 @@ services: - "8041:8041" volumes: - .:/app:delegated + - backoffice_frontend_gradle_cache:/app/.gradle depends_on: - shared_mysql - shared_rabbitmq @@ -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: diff --git a/src/backoffice/main/tv/codely/backoffice/shared/infrastructure/persistence/BackofficeElasticsearchConfiguration.java b/src/backoffice/main/tv/codely/backoffice/shared/infrastructure/persistence/BackofficeElasticsearchConfiguration.java index 582309c9..98c0f1ad 100644 --- a/src/backoffice/main/tv/codely/backoffice/shared/infrastructure/persistence/BackofficeElasticsearchConfiguration.java +++ b/src/backoffice/main/tv/codely/backoffice/shared/infrastructure/persistence/BackofficeElasticsearchConfiguration.java @@ -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; @@ -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; } diff --git a/src/shared/main/tv/codely/shared/domain/Utils.java b/src/shared/main/tv/codely/shared/domain/Utils.java index b0b26f75..53dbc3ea 100644 --- a/src/shared/main/tv/codely/shared/domain/Utils.java +++ b/src/shared/main/tv/codely/shared/domain/Utils.java @@ -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); + } + } + } + } + }