Skip to content

Commit

Permalink
chore: add all servers to docker (#85)
Browse files Browse the repository at this point in the history
* chore: add all servers to docker

* chore: add all servers to docker

* chore: wait until elastic is up

* ci: add test server to run the tests

* ci: use specific docker-compose for ci
  • Loading branch information
rgomezcasas authored Nov 13, 2023
1 parent d4f712f commit c577764
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 97 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
all: build

start:
@docker compose up -d
@docker-compose -f docker-compose.ci.yml up -d

build:
@./gradlew build --warning-mode all

lint:
@docker exec codelytv-ddd_example-java ./gradlew spotlessCheck
@docker exec codely-java_ddd_example-test_server ./gradlew spotlessCheck

run-tests:
@./gradlew test --warning-mode all

test:
@docker exec codelytv-ddd_example-java ./gradlew test --warning-mode all
@docker exec codely-java_ddd_example-test_server ./gradlew test --warning-mode all

run:
@./gradlew :run

ping-mysql:
@docker exec codelytv-java_ddd_example-mysql mysqladmin --user=root --password= --host "127.0.0.1" ping --silent
@docker exec codely-java_ddd_example-mysql mysqladmin --user=root --password= --host "127.0.0.1" ping --silent

# Start the app
start-mooc_backend:
Expand Down
16 changes: 8 additions & 8 deletions apps/main/resources/.env
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
# MOOC #
#--------------------------------#
MOOC_BACKEND_SERVER_PORT=8081
MOOC_BACKEND_SERVER_PORT=8030
# MySql
MOOC_DATABASE_HOST=codelytv-java_ddd_example-mysql
MOOC_DATABASE_HOST=codely-java_ddd_example-mysql
MOOC_DATABASE_PORT=3306
MOOC_DATABASE_NAME=mooc
MOOC_DATABASE_USER=root
MOOC_DATABASE_PASSWORD=

# BACKOFFICE #
#--------------------------------#
BACKOFFICE_FRONTEND_SERVER_PORT=8090
BACKOFFICE_BACKEND_SERVER_PORT=8091
BACKOFFICE_BACKEND_SERVER_PORT=8040
BACKOFFICE_FRONTEND_SERVER_PORT=8041
# MySql
BACKOFFICE_DATABASE_HOST=codelytv-java_ddd_example-mysql
BACKOFFICE_DATABASE_HOST=codely-java_ddd_example-mysql
BACKOFFICE_DATABASE_PORT=3306
BACKOFFICE_DATABASE_NAME=backoffice
BACKOFFICE_DATABASE_USER=root
BACKOFFICE_DATABASE_PASSWORD=
# Elasticsearch
BACKOFFICE_ELASTICSEARCH_HOST=codelytv-java_ddd_example-elasticsearch
BACKOFFICE_ELASTICSEARCH_HOST=codely-java_ddd_example-elasticsearch
BACKOFFICE_ELASTICSEARCH_PORT=9200
BACKOFFICE_ELASTICSEARCH_INDEX_PREFIX=backoffice

# COMMON #
#--------------------------------#
# RabbitMQ
RABBITMQ_HOST=codelytv-java_ddd_example-rabbitmq
RABBITMQ_HOST=codely-java_ddd_example-rabbitmq
RABBITMQ_PORT=5672
RABBITMQ_LOGIN=codelytv
RABBITMQ_LOGIN=codely
RABBITMQ_PASSWORD=c0d3ly
RABBITMQ_EXCHANGE=domain_events
RABBITMQ_MAX_RETRIES=5
55 changes: 55 additions & 0 deletions docker-compose.ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
version: '3'

services:
shared_mysql:
container_name: codely-java_ddd_example-mysql
image: mysql:8
platform: linux/amd64
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
entrypoint:
sh -c "
echo 'CREATE DATABASE IF NOT EXISTS mooc;CREATE DATABASE IF NOT EXISTS backoffice;' > /docker-entrypoint-initdb.d/init.sql;
/usr/local/bin/docker-entrypoint.sh --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
"
command: ["--default-authentication-plugin=mysql_native_password"]

shared_rabbitmq:
container_name: codely-java_ddd_example-rabbitmq
image: 'rabbitmq:3.7-management'
platform: linux/amd64
restart: unless-stopped
ports:
- "5630:5672"
- "8090:15672"
environment:
- RABBITMQ_DEFAULT_USER=codely
- RABBITMQ_DEFAULT_PASS=c0d3ly

backoffice_elasticsearch:
container_name: codely-java_ddd_example-elasticsearch
image: 'elasticsearch:6.8.4'
platform: linux/amd64
restart: unless-stopped
ports:
- "9300:9300"
- "9200:9200"
environment:
- discovery.type=single-node

test_server_java:
container_name: codely-java_ddd_example-test_server
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
volumes:
- .:/app:delegated
depends_on:
- shared_mysql
- shared_rabbitmq
- backoffice_elasticsearch
tty: true
86 changes: 70 additions & 16 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version: '3'

services:
mysql:
container_name: codelytv-java_ddd_example-mysql
shared_mysql:
container_name: codely-java_ddd_example-mysql
image: mysql:8
platform: linux/amd64
ports:
Expand All @@ -17,20 +17,20 @@ services:
"
command: ["--default-authentication-plugin=mysql_native_password"]

rabbitmq:
container_name: codelytv-java_ddd_example-rabbitmq
shared_rabbitmq:
container_name: codely-java_ddd_example-rabbitmq
image: 'rabbitmq:3.7-management'
platform: linux/amd64
restart: unless-stopped
ports:
- "5630:5672"
- "8090:15672"
environment:
- RABBITMQ_DEFAULT_USER=codelytv
- RABBITMQ_DEFAULT_USER=codely
- RABBITMQ_DEFAULT_PASS=c0d3ly

elasticsearch:
container_name: codelytv-java_ddd_example-elasticsearch
backoffice_elasticsearch:
container_name: codely-java_ddd_example-elasticsearch
image: 'elasticsearch:6.8.4'
platform: linux/amd64
restart: unless-stopped
Expand All @@ -40,20 +40,74 @@ services:
environment:
- discovery.type=single-node

java:
container_name: codelytv-ddd_example-java
backoffice_backend_server_java:
container_name: codely-java_ddd_example-backoffice_backend_server
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
platform: linux/amd64
ports:
- "8030:8080"
- "8040:8040"
volumes:
- .:/app:delegated
env_file:
- .env
tty: true
- backoffice_backend_gradle_cache:/app/.gradle
depends_on:
- shared_mysql
- shared_rabbitmq
- backoffice_elasticsearch
command: ["./gradlew", "bootRun", "--args", "backoffice_backend server"]

backoffice_frontend_server_java:
container_name: codely-java_ddd_example-backoffice_frontend_server
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
ports:
- "8041:8041"
volumes:
- .:/app:delegated
- backoffice_frontend_gradle_cache:/app/.gradle
depends_on:
- shared_mysql
- shared_rabbitmq
- backoffice_elasticsearch
command: ["./gradlew", "bootRun", "--args", "backoffice_frontend server"]

mooc_backend_server_java:
container_name: codely-java_ddd_example-mooc_backend_server
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
ports:
- "8030:8030"
volumes:
- .:/app:delegated
- mooc_backend_gradle_cache:/app/.gradle
depends_on:
- mysql
- rabbitmq
- shared_mysql
- shared_rabbitmq
- backoffice_elasticsearch
command: ["./gradlew", "bootRun", "--args", "mooc_backend server"]

test_server_java:
container_name: codely-java_ddd_example-test_server
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
volumes:
- .:/app:delegated
- test_gradle_cache:/app/.gradle
depends_on:
- shared_mysql
- shared_rabbitmq
- backoffice_elasticsearch
tty: true

volumes:
backoffice_backend_gradle_cache:
backoffice_frontend_gradle_cache:
mooc_backend_gradle_cache:
test_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
1 change: 1 addition & 0 deletions src/mooc/main/resources/database/mooc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CREATE TABLE IF NOT EXISTS courses_counter (
ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
INSERT IGNORE INTO courses_counter (id, total, existing_courses) VALUES ('efbaff16-8fcd-4689-9fc9-ec545d641c46', 0, '[]');

CREATE TABLE IF NOT EXISTS steps (
id CHAR(36) NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</component>

<property name="existingCourses" column="existing_courses" access="field">
<type name="json_string">
<type name="tv.codely.shared.infrastructure.hibernate.JsonListType">
<param name="list_of">tv.codely.mooc.courses.domain.CourseId</param>
</type>
</property>
Expand Down

This file was deleted.

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public List<Class<? extends DomainEvent>> subscribedEvents() {
}

public String formatRabbitMqQueueName() {
return String.format("codelytv.%s.%s.%s", contextName(), moduleName(), Utils.toSnake(className()));
return String.format("codely.%s.%s.%s", contextName(), moduleName(), Utils.toSnake(className()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,19 @@ private List<String> subdirectoriesFor(String contextName) {
}

private String[] mappingFilesIn(String path) {
String[] files = new File(path).list((current, name) -> new File(current, name).getName().contains(".hbm.xml"));
List<String> fileList = new ArrayList<>();

if (null == files) {
return new String[0];
}
String[] hbmFiles = new File(path).list((current, name) -> new File(current, name).getName().contains(".hbm.xml"));
String[] ormFiles = new File(path).list((current, name) -> new File(current, name).getName().contains(".orm.xml"));

if (hbmFiles != null) {
fileList.addAll(Arrays.asList(hbmFiles));
}
if (ormFiles != null) {
fileList.addAll(Arrays.asList(ormFiles));
}

return files;
return fileList.toArray(new String[0]);
}

private Properties hibernateProperties() {
Expand Down
Loading

0 comments on commit c577764

Please sign in to comment.