From 17443fb5af62a5126fdc6579a36480e41dbca59c Mon Sep 17 00:00:00 2001 From: Stephen Tung Date: Fri, 4 Apr 2025 12:05:08 +0800 Subject: [PATCH 01/11] Start up docker compose on postCreate once again --- polyglot-persistence/postCreateCommand.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polyglot-persistence/postCreateCommand.sh b/polyglot-persistence/postCreateCommand.sh index 5f05671..28264fa 100644 --- a/polyglot-persistence/postCreateCommand.sh +++ b/polyglot-persistence/postCreateCommand.sh @@ -1,4 +1,4 @@ -# docker compose -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.yml up -d +docker compose -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.yml up -d # dotnet run --project /workspaces/developer-bootcamp/polyglot-persistence/DemoWeb & From 7e064def5235bc38a33199dd6ce2f13f58caf046 Mon Sep 17 00:00:00 2001 From: Stephen Tung Date: Fri, 4 Apr 2025 16:02:15 +0800 Subject: [PATCH 02/11] Added dotnet projects to docker compose. Added host name env vars, --- polyglot-persistence/.devcontainer/Dockerfile | 14 ------ .../.devcontainer/devcontainer.json | 14 ------ polyglot-persistence/Common/Common.csproj | 2 +- .../DemoWeb/PostgresService.cs | 4 +- polyglot-persistence/DemoWeb/Program.cs | 3 +- .../DemoWeb/Properties/launchSettings.json | 4 +- polyglot-persistence/DemoWeb/appsettings.json | 3 -- .../MongoProjection/Program.cs | 6 ++- .../PostgresProjection/Program.cs | 7 ++- .../RedisProjection/Program.cs | 8 +++- polyglot-persistence/docker-compose.yml | 48 ++++++++++++++++++- polyglot-persistence/generate-data.sh | 20 ++++++++ polyglot-persistence/postCreateCommand.sh | 11 +++++ 13 files changed, 100 insertions(+), 44 deletions(-) delete mode 100644 polyglot-persistence/.devcontainer/Dockerfile delete mode 100644 polyglot-persistence/.devcontainer/devcontainer.json diff --git a/polyglot-persistence/.devcontainer/Dockerfile b/polyglot-persistence/.devcontainer/Dockerfile deleted file mode 100644 index cfe6e08..0000000 --- a/polyglot-persistence/.devcontainer/Dockerfile +++ /dev/null @@ -1,14 +0,0 @@ -FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-14 - -# Install any additional tools or dependencies needed for development -RUN apt-get update && apt-get install -y \ - curl \ - git \ - && apt-get clean \ - && rm -rf /var/lib/apt/lists/* - -# Set the working directory -WORKDIR /workspace - -# Copy any necessary files (if needed) -# COPY . . \ No newline at end of file diff --git a/polyglot-persistence/.devcontainer/devcontainer.json b/polyglot-persistence/.devcontainer/devcontainer.json deleted file mode 100644 index 6b4be9e..0000000 --- a/polyglot-persistence/.devcontainer/devcontainer.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name": "My Dev Environment", - "dockerComposeFile": "../docker-compose.yml", - "service": "app", - "workspaceFolder": "/workspace", - "settings": { - "terminal.integrated.shell.linux": "/bin/bash" - }, - "extensions": [ - "ms-azuretools.vscode-docker", - "esbenp.prettier-vscode" - ], - "postCreateCommand": "npm install" -} \ No newline at end of file diff --git a/polyglot-persistence/Common/Common.csproj b/polyglot-persistence/Common/Common.csproj index 6f2149d..4f1a7b9 100644 --- a/polyglot-persistence/Common/Common.csproj +++ b/polyglot-persistence/Common/Common.csproj @@ -1,7 +1,7 @@  - net8.0 + net9.0 enable enable diff --git a/polyglot-persistence/DemoWeb/PostgresService.cs b/polyglot-persistence/DemoWeb/PostgresService.cs index dcd2400..fb7aa5b 100644 --- a/polyglot-persistence/DemoWeb/PostgresService.cs +++ b/polyglot-persistence/DemoWeb/PostgresService.cs @@ -10,8 +10,8 @@ public class PostgresService public PostgresService(IConfiguration configuration, ILogger logger) { _logger = logger; - _connectionString = configuration.GetConnectionString("PostgreSQL") - ?? "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=postgres"; + var postgresHost = Environment.GetEnvironmentVariable("POSTGRES_HOST") ?? "localhost"; + _connectionString = $"Host={postgresHost};Port=5432;Database=postgres;Username=postgres;Password=postgres"; _logger.LogInformation("PostgreSQL connection string: {ConnectionString}", _connectionString.Replace("Password=", "Password=***")); diff --git a/polyglot-persistence/DemoWeb/Program.cs b/polyglot-persistence/DemoWeb/Program.cs index cb9583f..c5152f6 100644 --- a/polyglot-persistence/DemoWeb/Program.cs +++ b/polyglot-persistence/DemoWeb/Program.cs @@ -11,8 +11,9 @@ builder.Services.AddSignalR(); // Register Redis services +var redisHost = Environment.GetEnvironmentVariable("REDIS_HOST") ?? "localhost"; builder.Services.AddSingleton(sp => - ConnectionMultiplexer.Connect("localhost:6379")); + ConnectionMultiplexer.Connect($"{redisHost}:6379")); builder.Services.AddSingleton(); // Register PostgreSQL service diff --git a/polyglot-persistence/DemoWeb/Properties/launchSettings.json b/polyglot-persistence/DemoWeb/Properties/launchSettings.json index 7c54133..2b5a75d 100644 --- a/polyglot-persistence/DemoWeb/Properties/launchSettings.json +++ b/polyglot-persistence/DemoWeb/Properties/launchSettings.json @@ -13,7 +13,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "http://localhost:5108", + "applicationUrl": "http://0.0.0.0:5108", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -22,7 +22,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "https://localhost:7164;http://localhost:5108", + "applicationUrl": "https://0.0.0.0:7164;http://0.0.0.0:5108", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/polyglot-persistence/DemoWeb/appsettings.json b/polyglot-persistence/DemoWeb/appsettings.json index 7df6b9b..fb87850 100644 --- a/polyglot-persistence/DemoWeb/appsettings.json +++ b/polyglot-persistence/DemoWeb/appsettings.json @@ -1,7 +1,4 @@ { - "ConnectionStrings": { - "PostgreSQL": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=postgres" - }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/polyglot-persistence/MongoProjection/Program.cs b/polyglot-persistence/MongoProjection/Program.cs index a6796a1..cf0a18d 100644 --- a/polyglot-persistence/MongoProjection/Program.cs +++ b/polyglot-persistence/MongoProjection/Program.cs @@ -8,10 +8,12 @@ Console.WriteLine($"{AppDomain.CurrentDomain.FriendlyName} started"); // Connect to MongoDB -var mongoCollection = new MongoClient("mongodb://localhost:27017").GetDatabase("polyglot-persistence").GetCollection("total-payment"); +var mongoHost = Environment.GetEnvironmentVariable("MONGO_HOST") ?? "localhost"; +var mongoCollection = new MongoClient($"mongodb://{mongoHost}:27017").GetDatabase("polyglot-persistence").GetCollection("total-payment"); // Connect to EventStoreDB -var esdb = new EventStoreClient(EventStoreClientSettings.Create("esdb://admin:changeit@localhost:2113?tls=false")); +var esdbHost = Environment.GetEnvironmentVariable("ESDB_HOST") ?? "localhost"; +var esdb = new EventStoreClient(EventStoreClientSettings.Create($"esdb://admin:changeit@{esdbHost}:2113?tls=false")); var checkpointValue = mongoCollection // Get the checkpoint value from MongoDB.. .Find(Builders.Filter.Eq("_id", "total")) // from the total document's.. diff --git a/polyglot-persistence/PostgresProjection/Program.cs b/polyglot-persistence/PostgresProjection/Program.cs index 63148f0..6173700 100644 --- a/polyglot-persistence/PostgresProjection/Program.cs +++ b/polyglot-persistence/PostgresProjection/Program.cs @@ -26,8 +26,11 @@ // Connect to PostgreSQL and EventStoreDB // // -------------------------------------- // -var postgres = new PostgresDataAccess(new NpgsqlConnection("Host=localhost;Port=5432;Database=postgres;Username=postgres")); -var esdb = new EventStoreClient(EventStoreClientSettings.Create("esdb://admin:changeit@localhost:2113?tls=false")); +var postgresHost = Environment.GetEnvironmentVariable("POSTGRES_HOST") ?? "localhost"; +var postgres = new PostgresDataAccess(new NpgsqlConnection($"Host={postgresHost};Port=5432;Database=postgres;Username=postgres")); + +var esdbHost = Environment.GetEnvironmentVariable("ESDB_HOST") ?? "localhost"; +var esdb = new EventStoreClient(EventStoreClientSettings.Create($"esdb://admin:changeit@{esdbHost}:2113?tls=false")); postgres.Execute(CartProjection.GetCreateCartTableCommand()); // Create the checkpoint table if it doesn't exist postgres.Execute(Checkpoint.GetCreateTableCommand()); // Create the checkpoint table if it doesn't exist diff --git a/polyglot-persistence/RedisProjection/Program.cs b/polyglot-persistence/RedisProjection/Program.cs index c250af6..8f52c3e 100644 --- a/polyglot-persistence/RedisProjection/Program.cs +++ b/polyglot-persistence/RedisProjection/Program.cs @@ -6,8 +6,12 @@ Console.WriteLine($"{AppDomain.CurrentDomain.FriendlyName} started"); -var redis = ConnectionMultiplexer.Connect("localhost:6379").GetDatabase(); // Connect to Redis -var esdb = new EventStoreClient(EventStoreClientSettings.Create("esdb://admin:changeit@localhost:2113?tls=false")); // Connect to EventStoreDB +// Program.cs +var redisHost = Environment.GetEnvironmentVariable("REDIS_HOST") ?? "localhost"; +var redis = ConnectionMultiplexer.Connect($"{redisHost}:6379").GetDatabase(); + // Connect to Redis +var esdbHost = Environment.GetEnvironmentVariable("ESDB_HOST") ?? "localhost"; +var esdb = new EventStoreClient(EventStoreClientSettings.Create($"esdb://admin:changeit@{esdbHost}:2113?tls=false")); // Connect to EventStoreDB var checkpointValue = redis.StringGet("checkpoint"); // Get the checkpoint value from redis var streamPosition = long.TryParse(checkpointValue, out var checkpoint) // Check if it exists and convertible to long diff --git a/polyglot-persistence/docker-compose.yml b/polyglot-persistence/docker-compose.yml index 7423764..e46c13d 100644 --- a/polyglot-persistence/docker-compose.yml +++ b/polyglot-persistence/docker-compose.yml @@ -29,4 +29,50 @@ services: - EVENTSTORE_RUN_PROJECTIONS=All - EVENTSTORE_START_STANDARD_PROJECTIONS=true - EVENTSTORE_INSECURE=true - - EVENTSTORE_ENABLE_ATOM_PUB_OVER_HTTP=true \ No newline at end of file + - EVENTSTORE_ENABLE_ATOM_PUB_OVER_HTTP=true + + demoweb: + image: mcr.microsoft.com/dotnet/sdk:9.0 + container_name: demoweb + working_dir: /app + volumes: + - ./DemoWeb:/app # Mount the DemoWeb project directory + command: ["dotnet", "run"] + ports: + - "5108:5108" + environment: + - REDIS_HOST=redis + - POSTGRES_HOST=postgres + + postgresprojection: + image: mcr.microsoft.com/dotnet/sdk:9.0 + container_name: postgresprojection + working_dir: /app + volumes: + - ./:/app + command: ["dotnet", "run", "--project", "./PostgresProjection"] + environment: + - POSTGRES_HOST=postgres + - ESDB_HOST=eventstore + + redisprojection: + image: mcr.microsoft.com/dotnet/sdk:9.0 + container_name: redisprojection + working_dir: /app + volumes: + - ./:/app + command: ["dotnet", "run", "--project", "./RedisProjection"] + environment: + - REDIS_HOST=redis + - ESDB_HOST=eventstore + + mongoprojection: + image: mcr.microsoft.com/dotnet/sdk:9.0 + container_name: mongoprojection + working_dir: /app + volumes: + - ./:/app + command: ["dotnet", "run", "--project", "./MongoProjection"] + environment: + - MONGO_HOST=mongo + - ESDB_HOST=eventstore \ No newline at end of file diff --git a/polyglot-persistence/generate-data.sh b/polyglot-persistence/generate-data.sh index e69de29..d603a2c 100644 --- a/polyglot-persistence/generate-data.sh +++ b/polyglot-persistence/generate-data.sh @@ -0,0 +1,20 @@ +unzip -o /workspaces/developer-bootcamp/polyglot-persistence/data/commerce-data-set.zip -d /workspaces/developer-bootcamp/polyglot-persistence/ + +chmod +x /workspaces/developer-bootcamp/polyglot-persistence/tools/Kurrent.Extensions.Commerce/linux-x64/edb-commerce + +# Wait for EventStoreDB to be ready +echo "Waiting for EventStoreDB to be ready..." +max_attempts=30 +attempt=0 +while ! curl -s http://localhost:2113/ > /dev/null; do + attempt=$((attempt+1)) + if [ $attempt -eq $max_attempts ]; then + echo "EventStoreDB failed to start after $max_attempts attempts. Exiting." + exit 1 + fi + echo "Waiting for EventStoreDB to be ready... (attempt $attempt/$max_attempts)" + sleep 2 +done +echo "EventStoreDB is ready." + +/workspaces/developer-bootcamp/polyglot-persistence/tools/Kurrent.Extensions.Commerce/linux-x64/edb-commerce seed-data-set /workspaces/developer-bootcamp/polyglot-persistence/data.json \ No newline at end of file diff --git a/polyglot-persistence/postCreateCommand.sh b/polyglot-persistence/postCreateCommand.sh index 28264fa..a10788d 100644 --- a/polyglot-persistence/postCreateCommand.sh +++ b/polyglot-persistence/postCreateCommand.sh @@ -1,3 +1,14 @@ +max_attempts=10 +attempt=0 +while ! docker ps > /dev/null 2>&1; do # While docker fails to run (e.g. Docker daemon is not running) + if [ "$attempt" -ge "$max_attempts" ]; then # If number of attempt exceeds the max_attempts then we exit + echo "Docker daemon is still not available. Exiting" + exit 1 + fi + attempt=$((attempt+1)) # Increment the attempt count + sleep 2 # Wait for few seconds before we check again +done + docker compose -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.yml up -d # dotnet run --project /workspaces/developer-bootcamp/polyglot-persistence/DemoWeb & From 155a3f96fde510d4d3af4c91ab35d38e849a5ed8 Mon Sep 17 00:00:00 2001 From: Stephen Tung Date: Fri, 4 Apr 2025 21:29:14 +0800 Subject: [PATCH 03/11] Added a db profile in docker compose to only load up db during oncreate --- polyglot-persistence/docker-compose.yml | 10 +++++++++- polyglot-persistence/onCreateCommand.sh | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/polyglot-persistence/docker-compose.yml b/polyglot-persistence/docker-compose.yml index e46c13d..cfde762 100644 --- a/polyglot-persistence/docker-compose.yml +++ b/polyglot-persistence/docker-compose.yml @@ -4,6 +4,7 @@ services: container_name: mongo ports: - "27017:27017" + profiles: ["db", "all"] postgres: image: postgres:16 @@ -12,12 +13,14 @@ services: - "5432:5432" environment: - POSTGRES_HOST_AUTH_METHOD=trust + profiles: ["db", "all"] redis: image: redis:7.2 container_name: redis ports: - "6379:6379" + profiles: ["db", "all"] eventstore: image: eventstore/eventstore:24.10 @@ -30,6 +33,7 @@ services: - EVENTSTORE_START_STANDARD_PROJECTIONS=true - EVENTSTORE_INSECURE=true - EVENTSTORE_ENABLE_ATOM_PUB_OVER_HTTP=true + profiles: ["db", "all"] demoweb: image: mcr.microsoft.com/dotnet/sdk:9.0 @@ -43,6 +47,7 @@ services: environment: - REDIS_HOST=redis - POSTGRES_HOST=postgres + profiles: ["app", "all"] postgresprojection: image: mcr.microsoft.com/dotnet/sdk:9.0 @@ -54,6 +59,7 @@ services: environment: - POSTGRES_HOST=postgres - ESDB_HOST=eventstore + profiles: ["app", "all"] redisprojection: image: mcr.microsoft.com/dotnet/sdk:9.0 @@ -65,6 +71,7 @@ services: environment: - REDIS_HOST=redis - ESDB_HOST=eventstore + profiles: ["app", "all"] mongoprojection: image: mcr.microsoft.com/dotnet/sdk:9.0 @@ -75,4 +82,5 @@ services: command: ["dotnet", "run", "--project", "./MongoProjection"] environment: - MONGO_HOST=mongo - - ESDB_HOST=eventstore \ No newline at end of file + - ESDB_HOST=eventstore + profiles: ["app", "all"] \ No newline at end of file diff --git a/polyglot-persistence/onCreateCommand.sh b/polyglot-persistence/onCreateCommand.sh index 441eb87..6379705 100644 --- a/polyglot-persistence/onCreateCommand.sh +++ b/polyglot-persistence/onCreateCommand.sh @@ -1,4 +1,4 @@ -docker compose -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.yml up -d +docker compose --profile db -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.yml up -d unzip -o /workspaces/developer-bootcamp/polyglot-persistence/data/init-data.zip -d /workspaces/developer-bootcamp/polyglot-persistence/ From 6e0dfa9ba4637097300a27d0b87a8030106972e3 Mon Sep 17 00:00:00 2001 From: Stephen Tung Date: Fri, 4 Apr 2025 23:25:19 +0800 Subject: [PATCH 04/11] Remove postCreate afterall --- .devcontainer/devcontainer.json | 2 +- polyglot-persistence/docker-compose.yml | 23 ++++++++--------------- polyglot-persistence/postCreateCommand.sh | 22 +++++++++++----------- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 75fca1e..8e3a6b7 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -8,7 +8,7 @@ "DOTNET_NOLOGO": "true" }, "onCreateCommand": "chmod +x /workspaces/developer-bootcamp/polyglot-persistence/onCreateCommand.sh && /workspaces/developer-bootcamp/polyglot-persistence/onCreateCommand.sh", - "postCreateCommand": "chmod +x /workspaces/developer-bootcamp/polyglot-persistence/postCreateCommand.sh && /workspaces/developer-bootcamp/polyglot-persistence/postCreateCommand.sh", + // "postCreateCommand": "chmod +x /workspaces/developer-bootcamp/polyglot-persistence/postCreateCommand.sh && /workspaces/developer-bootcamp/polyglot-persistence/postCreateCommand.sh", "forwardPorts": [27017, 5432, 6379, 2113, 1113], "portsAttributes": { "2113": { diff --git a/polyglot-persistence/docker-compose.yml b/polyglot-persistence/docker-compose.yml index cfde762..0814322 100644 --- a/polyglot-persistence/docker-compose.yml +++ b/polyglot-persistence/docker-compose.yml @@ -4,8 +4,7 @@ services: container_name: mongo ports: - "27017:27017" - profiles: ["db", "all"] - + postgres: image: postgres:16 container_name: postgres @@ -13,15 +12,13 @@ services: - "5432:5432" environment: - POSTGRES_HOST_AUTH_METHOD=trust - profiles: ["db", "all"] - + redis: image: redis:7.2 container_name: redis ports: - "6379:6379" - profiles: ["db", "all"] - + eventstore: image: eventstore/eventstore:24.10 container_name: eventstore @@ -33,8 +30,7 @@ services: - EVENTSTORE_START_STANDARD_PROJECTIONS=true - EVENTSTORE_INSECURE=true - EVENTSTORE_ENABLE_ATOM_PUB_OVER_HTTP=true - profiles: ["db", "all"] - + demoweb: image: mcr.microsoft.com/dotnet/sdk:9.0 container_name: demoweb @@ -47,8 +43,7 @@ services: environment: - REDIS_HOST=redis - POSTGRES_HOST=postgres - profiles: ["app", "all"] - + postgresprojection: image: mcr.microsoft.com/dotnet/sdk:9.0 container_name: postgresprojection @@ -59,8 +54,7 @@ services: environment: - POSTGRES_HOST=postgres - ESDB_HOST=eventstore - profiles: ["app", "all"] - + redisprojection: image: mcr.microsoft.com/dotnet/sdk:9.0 container_name: redisprojection @@ -71,8 +65,7 @@ services: environment: - REDIS_HOST=redis - ESDB_HOST=eventstore - profiles: ["app", "all"] - + mongoprojection: image: mcr.microsoft.com/dotnet/sdk:9.0 container_name: mongoprojection @@ -83,4 +76,4 @@ services: environment: - MONGO_HOST=mongo - ESDB_HOST=eventstore - profiles: ["app", "all"] \ No newline at end of file + \ No newline at end of file diff --git a/polyglot-persistence/postCreateCommand.sh b/polyglot-persistence/postCreateCommand.sh index a10788d..0a239d8 100644 --- a/polyglot-persistence/postCreateCommand.sh +++ b/polyglot-persistence/postCreateCommand.sh @@ -1,15 +1,15 @@ -max_attempts=10 -attempt=0 -while ! docker ps > /dev/null 2>&1; do # While docker fails to run (e.g. Docker daemon is not running) - if [ "$attempt" -ge "$max_attempts" ]; then # If number of attempt exceeds the max_attempts then we exit - echo "Docker daemon is still not available. Exiting" - exit 1 - fi - attempt=$((attempt+1)) # Increment the attempt count - sleep 2 # Wait for few seconds before we check again -done +# max_attempts=10 +# attempt=0 +# while ! docker ps > /dev/null 2>&1; do # While docker fails to run (e.g. Docker daemon is not running) +# if [ "$attempt" -ge "$max_attempts" ]; then # If number of attempt exceeds the max_attempts then we exit +# echo "Docker daemon is still not available. Exiting" +# exit 1 +# fi +# attempt=$((attempt+1)) # Increment the attempt count +# sleep 2 # Wait for few seconds before we check again +# done -docker compose -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.yml up -d +# docker compose -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.yml up -d # dotnet run --project /workspaces/developer-bootcamp/polyglot-persistence/DemoWeb & From 776ecddb994cf3d68deab0a9f380d09d82f4d35e Mon Sep 17 00:00:00 2001 From: Stephen Tung Date: Fri, 4 Apr 2025 23:32:10 +0800 Subject: [PATCH 05/11] Try fix access denied problem --- .devcontainer/devcontainer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 8e3a6b7..a806559 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -7,6 +7,7 @@ "containerEnv": { "DOTNET_NOLOGO": "true" }, + "postStartCommand": "git clean -xdf", //remove absolutely every file on the .gitignore list, so we don't run into permission issues "onCreateCommand": "chmod +x /workspaces/developer-bootcamp/polyglot-persistence/onCreateCommand.sh && /workspaces/developer-bootcamp/polyglot-persistence/onCreateCommand.sh", // "postCreateCommand": "chmod +x /workspaces/developer-bootcamp/polyglot-persistence/postCreateCommand.sh && /workspaces/developer-bootcamp/polyglot-persistence/postCreateCommand.sh", "forwardPorts": [27017, 5432, 6379, 2113, 1113], From 898bc5409f80112259689a377a802cdd0a6b4c59 Mon Sep 17 00:00:00 2001 From: Stephen Tung Date: Fri, 4 Apr 2025 23:49:52 +0800 Subject: [PATCH 06/11] Try the updateContentCommand instead --- .devcontainer/devcontainer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index a806559..546f540 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -8,7 +8,7 @@ "DOTNET_NOLOGO": "true" }, "postStartCommand": "git clean -xdf", //remove absolutely every file on the .gitignore list, so we don't run into permission issues - "onCreateCommand": "chmod +x /workspaces/developer-bootcamp/polyglot-persistence/onCreateCommand.sh && /workspaces/developer-bootcamp/polyglot-persistence/onCreateCommand.sh", + "updateContentCommand": "chmod +x /workspaces/developer-bootcamp/polyglot-persistence/onCreateCommand.sh && /workspaces/developer-bootcamp/polyglot-persistence/onCreateCommand.sh", // "postCreateCommand": "chmod +x /workspaces/developer-bootcamp/polyglot-persistence/postCreateCommand.sh && /workspaces/developer-bootcamp/polyglot-persistence/postCreateCommand.sh", "forwardPorts": [27017, 5432, 6379, 2113, 1113], "portsAttributes": { From 582d0ff8733f6b75f86c70c46f4f7adc7c1f35e8 Mon Sep 17 00:00:00 2001 From: Stephen Tung Date: Sat, 5 Apr 2025 01:42:20 +0800 Subject: [PATCH 07/11] Added start.sh to manage the start of all services --- .devcontainer/devcontainer.json | 4 +- .../docker-compose.codespaces-prebuild.yml | 79 +++++++++++++++++++ polyglot-persistence/docker-compose.yml | 10 ++- polyglot-persistence/onCreateCommand.sh | 2 +- polyglot-persistence/postCreateCommand.sh | 20 ----- polyglot-persistence/start.sh | 43 ++++++++++ 6 files changed, 133 insertions(+), 25 deletions(-) create mode 100644 polyglot-persistence/docker-compose.codespaces-prebuild.yml delete mode 100644 polyglot-persistence/postCreateCommand.sh create mode 100644 polyglot-persistence/start.sh diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 546f540..deef4d8 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -7,9 +7,7 @@ "containerEnv": { "DOTNET_NOLOGO": "true" }, - "postStartCommand": "git clean -xdf", //remove absolutely every file on the .gitignore list, so we don't run into permission issues - "updateContentCommand": "chmod +x /workspaces/developer-bootcamp/polyglot-persistence/onCreateCommand.sh && /workspaces/developer-bootcamp/polyglot-persistence/onCreateCommand.sh", - // "postCreateCommand": "chmod +x /workspaces/developer-bootcamp/polyglot-persistence/postCreateCommand.sh && /workspaces/developer-bootcamp/polyglot-persistence/postCreateCommand.sh", + "onCreateCommand": "chmod +x /workspaces/developer-bootcamp/polyglot-persistence/onCreateCommand.sh && /workspaces/developer-bootcamp/polyglot-persistence/onCreateCommand.sh", "forwardPorts": [27017, 5432, 6379, 2113, 1113], "portsAttributes": { "2113": { diff --git a/polyglot-persistence/docker-compose.codespaces-prebuild.yml b/polyglot-persistence/docker-compose.codespaces-prebuild.yml new file mode 100644 index 0000000..0c93d79 --- /dev/null +++ b/polyglot-persistence/docker-compose.codespaces-prebuild.yml @@ -0,0 +1,79 @@ +services: + mongo: + image: mongo:7.0 + container_name: mongo + ports: + - "27017:27017" + + postgres: + image: postgres:16 + container_name: postgres + ports: + - "5432:5432" + environment: + - POSTGRES_HOST_AUTH_METHOD=trust + + redis: + image: redis:7.2 + container_name: redis + ports: + - "6379:6379" + + eventstore: + image: eventstore/eventstore:24.10 + container_name: eventstore + ports: + - "2113:2113" + - "1113:1113" + environment: + - EVENTSTORE_RUN_PROJECTIONS=All + - EVENTSTORE_START_STANDARD_PROJECTIONS=true + - EVENTSTORE_INSECURE=true + - EVENTSTORE_ENABLE_ATOM_PUB_OVER_HTTP=true + + demoweb: + image: mcr.microsoft.com/dotnet/sdk:9.0 + container_name: demoweb + working_dir: /app + volumes: + - ./DemoWeb:/app # Mount the DemoWeb project directory + command: ["dotnet", "build"] + ports: + - "5108:5108" + environment: + - REDIS_HOST=redis + - POSTGRES_HOST=postgres + + postgresprojection: + image: mcr.microsoft.com/dotnet/sdk:9.0 + container_name: postgresprojection + working_dir: /app + volumes: + - ./:/app + command: ["dotnet", "build", "./PostgresProjection"] + environment: + - POSTGRES_HOST=postgres + - ESDB_HOST=eventstore + + redisprojection: + image: mcr.microsoft.com/dotnet/sdk:9.0 + container_name: redisprojection + working_dir: /app + volumes: + - ./:/app + command: ["dotnet", "build", "./RedisProjection"] + environment: + - REDIS_HOST=redis + - ESDB_HOST=eventstore + + mongoprojection: + image: mcr.microsoft.com/dotnet/sdk:9.0 + container_name: mongoprojection + working_dir: /app + volumes: + - ./:/app + command: ["dotnet", "build", "./MongoProjection"] + environment: + - MONGO_HOST=mongo + - ESDB_HOST=eventstore + \ No newline at end of file diff --git a/polyglot-persistence/docker-compose.yml b/polyglot-persistence/docker-compose.yml index 0814322..b0ce631 100644 --- a/polyglot-persistence/docker-compose.yml +++ b/polyglot-persistence/docker-compose.yml @@ -54,6 +54,9 @@ services: environment: - POSTGRES_HOST=postgres - ESDB_HOST=eventstore + depends_on: + - postgres + - eventstore redisprojection: image: mcr.microsoft.com/dotnet/sdk:9.0 @@ -65,6 +68,9 @@ services: environment: - REDIS_HOST=redis - ESDB_HOST=eventstore + depends_on: + - redis + - eventstore mongoprojection: image: mcr.microsoft.com/dotnet/sdk:9.0 @@ -76,4 +82,6 @@ services: environment: - MONGO_HOST=mongo - ESDB_HOST=eventstore - \ No newline at end of file + depends_on: + - mongo + - eventstore \ No newline at end of file diff --git a/polyglot-persistence/onCreateCommand.sh b/polyglot-persistence/onCreateCommand.sh index 6379705..3bf60ac 100644 --- a/polyglot-persistence/onCreateCommand.sh +++ b/polyglot-persistence/onCreateCommand.sh @@ -1,4 +1,4 @@ -docker compose --profile db -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.yml up -d +docker compose -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.codespaces-prebuild.yml up -d unzip -o /workspaces/developer-bootcamp/polyglot-persistence/data/init-data.zip -d /workspaces/developer-bootcamp/polyglot-persistence/ diff --git a/polyglot-persistence/postCreateCommand.sh b/polyglot-persistence/postCreateCommand.sh deleted file mode 100644 index 0a239d8..0000000 --- a/polyglot-persistence/postCreateCommand.sh +++ /dev/null @@ -1,20 +0,0 @@ -# max_attempts=10 -# attempt=0 -# while ! docker ps > /dev/null 2>&1; do # While docker fails to run (e.g. Docker daemon is not running) -# if [ "$attempt" -ge "$max_attempts" ]; then # If number of attempt exceeds the max_attempts then we exit -# echo "Docker daemon is still not available. Exiting" -# exit 1 -# fi -# attempt=$((attempt+1)) # Increment the attempt count -# sleep 2 # Wait for few seconds before we check again -# done - -# docker compose -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.yml up -d - -# dotnet run --project /workspaces/developer-bootcamp/polyglot-persistence/DemoWeb & - -# dotnet run --project /workspaces/developer-bootcamp/polyglot-persistence/PostgresProjection & - -# dotnet run --project /workspaces/developer-bootcamp/polyglot-persistence/RedisProjection & - -# dotnet run --project /workspaces/developer-bootcamp/polyglot-persistence/MongoProjection & \ No newline at end of file diff --git a/polyglot-persistence/start.sh b/polyglot-persistence/start.sh new file mode 100644 index 0000000..552d377 --- /dev/null +++ b/polyglot-persistence/start.sh @@ -0,0 +1,43 @@ +max_attempts=30 +attempt=0 +while ! docker ps > /dev/null 2>&1; do # While docker fails to run (e.g. Docker daemon is not running) + if [ "$attempt" -ge "$max_attempts" ]; then # If number of attempt exceeds the max_attempts then we exit + echo "Docker daemon is not available. Exiting" + exit 1 + fi + echo "Waiting for Docker daemon to start... (attempt $attempt)" + attempt=$((attempt+1)) # Increment the attempt count + sleep 2 # Wait for few seconds before we check again +done + +echo "Docker daemon is running." + +docker compose -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.yml up -d + +max_attempts=30 +attempt=0 +while ! curl -s -o /dev/null -w "%{http_code}" http://localhost:2113/web/index.html | grep -q "200"; do + if [ "$attempt" -ge "$max_attempts" ]; then # If number of attempts exceeds max_attempts then we exit + echo "EventStoreDB is not available. Exiting" + exit 1 + fi + echo "Waiting for EventStoreDB to start... (attempt $attempt)" + attempt=$((attempt+1)) # Increment the attempt count + sleep 2 # Wait for a few seconds before checking again +done + +echo "EventStoreDB is running." + +max_attempts=30 +attempt=0 +while ! curl -s -o /dev/null -w "%{http_code}" http://localhost:5108/carts | grep -q "200"; do + if [ "$attempt" -ge "$max_attempts" ]; then # If number of attempts exceeds max_attempts then we exit + echo "DemoWeb is not available. Exiting" + exit 1 + fi + echo "Waiting for DemoWeb to start... (attempt $attempt)" + attempt=$((attempt+1)) # Increment the attempt count + sleep 2 # Wait for a few seconds before checking again +done + +echo "DemoWeb is running." \ No newline at end of file From cbb35f37166cdf71609cde4592f16d904ce0269d Mon Sep 17 00:00:00 2001 From: Stephen Tung Date: Sat, 5 Apr 2025 02:16:34 +0800 Subject: [PATCH 08/11] Still getting access denied error. Let's not build from script --- polyglot-persistence/onCreateCommand.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/polyglot-persistence/onCreateCommand.sh b/polyglot-persistence/onCreateCommand.sh index 3bf60ac..d50a190 100644 --- a/polyglot-persistence/onCreateCommand.sh +++ b/polyglot-persistence/onCreateCommand.sh @@ -1,5 +1,7 @@ docker compose -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.codespaces-prebuild.yml up -d +docker compose logs -f & + unzip -o /workspaces/developer-bootcamp/polyglot-persistence/data/init-data.zip -d /workspaces/developer-bootcamp/polyglot-persistence/ chmod +x /workspaces/developer-bootcamp/polyglot-persistence/tools/Kurrent.Extensions.Commerce/linux-x64/edb-commerce @@ -21,4 +23,6 @@ echo "EventStoreDB is ready." /workspaces/developer-bootcamp/polyglot-persistence/tools/Kurrent.Extensions.Commerce/linux-x64/edb-commerce seed-data-set /workspaces/developer-bootcamp/polyglot-persistence/data.json -dotnet build /workspaces/developer-bootcamp/polyglot-persistence/polyglot-persistence.sln \ No newline at end of file +echo "Updated EventStoreDB with initial data." + +sleep 30 \ No newline at end of file From d4730181deb5ef5460b7f169652a264937c3768a Mon Sep 17 00:00:00 2001 From: Stephen Tung Date: Sat, 5 Apr 2025 02:46:44 +0800 Subject: [PATCH 09/11] Try use dotnet run instead --- polyglot-persistence/onCreateCommand.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/polyglot-persistence/onCreateCommand.sh b/polyglot-persistence/onCreateCommand.sh index d50a190..da82e6b 100644 --- a/polyglot-persistence/onCreateCommand.sh +++ b/polyglot-persistence/onCreateCommand.sh @@ -1,6 +1,4 @@ -docker compose -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.codespaces-prebuild.yml up -d - -docker compose logs -f & +docker compose -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.yml up -d unzip -o /workspaces/developer-bootcamp/polyglot-persistence/data/init-data.zip -d /workspaces/developer-bootcamp/polyglot-persistence/ @@ -25,4 +23,4 @@ echo "EventStoreDB is ready." echo "Updated EventStoreDB with initial data." -sleep 30 \ No newline at end of file +docker compose logs \ No newline at end of file From 558265343a2d714802df404242dd4553488f2a0f Mon Sep 17 00:00:00 2001 From: Stephen Tung Date: Sat, 5 Apr 2025 02:52:33 +0800 Subject: [PATCH 10/11] Add missing path to docker compose --- polyglot-persistence/onCreateCommand.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polyglot-persistence/onCreateCommand.sh b/polyglot-persistence/onCreateCommand.sh index da82e6b..f1e83bf 100644 --- a/polyglot-persistence/onCreateCommand.sh +++ b/polyglot-persistence/onCreateCommand.sh @@ -23,4 +23,4 @@ echo "EventStoreDB is ready." echo "Updated EventStoreDB with initial data." -docker compose logs \ No newline at end of file +docker compose -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.yml logs \ No newline at end of file From 22b85d8f01155be8123d21f8bc6c2e6c55dd0a82 Mon Sep 17 00:00:00 2001 From: Stephen Tung Date: Sat, 5 Apr 2025 03:26:48 +0800 Subject: [PATCH 11/11] Wait till all the services run in onCreate --- polyglot-persistence/DemoWeb/Program.cs | 4 +- polyglot-persistence/onCreateCommand.sh | 59 +++++++++++++++++++------ 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/polyglot-persistence/DemoWeb/Program.cs b/polyglot-persistence/DemoWeb/Program.cs index c5152f6..f2ae663 100644 --- a/polyglot-persistence/DemoWeb/Program.cs +++ b/polyglot-persistence/DemoWeb/Program.cs @@ -45,4 +45,6 @@ // Map SignalR hub app.MapHub("/topProductsHub"); -app.Run(); \ No newline at end of file +app.Run(); + +Console.WriteLine($"{AppDomain.CurrentDomain.FriendlyName} started"); \ No newline at end of file diff --git a/polyglot-persistence/onCreateCommand.sh b/polyglot-persistence/onCreateCommand.sh index f1e83bf..7882b07 100644 --- a/polyglot-persistence/onCreateCommand.sh +++ b/polyglot-persistence/onCreateCommand.sh @@ -4,23 +4,56 @@ unzip -o /workspaces/developer-bootcamp/polyglot-persistence/data/init-data.zip chmod +x /workspaces/developer-bootcamp/polyglot-persistence/tools/Kurrent.Extensions.Commerce/linux-x64/edb-commerce -# Wait for EventStoreDB to be ready -echo "Waiting for EventStoreDB to be ready..." -max_attempts=30 + +max_attempts=60 attempt=0 -while ! curl -s http://localhost:2113/ > /dev/null; do - attempt=$((attempt+1)) - if [ $attempt -eq $max_attempts ]; then - echo "EventStoreDB failed to start after $max_attempts attempts. Exiting." - exit 1 - fi - echo "Waiting for EventStoreDB to be ready... (attempt $attempt/$max_attempts)" - sleep 2 +while ! curl -s -o /dev/null -w "%{http_code}" http://localhost:2113/web/index.html | grep -q "200"; do + if [ "$attempt" -ge "$max_attempts" ]; then # If number of attempts exceeds max_attempts then we exit + echo "EventStoreDB is not available. Exiting" + exit 1 + fi + echo "Waiting for EventStoreDB to start... (attempt $attempt)" + attempt=$((attempt+1)) # Increment the attempt count + sleep 2 # Wait for a few seconds before checking again +done + +echo "EventStoreDB is running." + +max_attempts=60 +attempt=0 +while ! curl -s -o /dev/null -w "%{http_code}" http://localhost:5108/carts | grep -q "200"; do + if [ "$attempt" -ge "$max_attempts" ]; then # If number of attempts exceeds max_attempts then we exit + echo "DemoWeb is not available. Exiting" + exit 1 + fi + echo "Waiting for DemoWeb to start... (attempt $attempt)" + attempt=$((attempt+1)) # Increment the attempt count + sleep 2 # Wait for a few seconds before checking again done -echo "EventStoreDB is ready." + +echo "DemoWeb is running." /workspaces/developer-bootcamp/polyglot-persistence/tools/Kurrent.Extensions.Commerce/linux-x64/edb-commerce seed-data-set /workspaces/developer-bootcamp/polyglot-persistence/data.json echo "Updated EventStoreDB with initial data." -docker compose -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.yml logs \ No newline at end of file +# Wait for the required projection messages to be detected +echo "Waiting for 'MongoProjection started', 'RedisProjection started' and 'DemoWeb started' messages..." +max_attempts=60 +attempt=0 +while true; do + logs=$(docker compose -f /workspaces/developer-bootcamp/polyglot-persistence/docker-compose.yml logs 2>&1) + if echo "$logs" | grep -q "MongoProjection started" && \ + echo "$logs" | grep -q "RedisProjection started" && \ + echo "$logs" | grep -q "PostgresProjection started"; then + echo "All required messages detected." + break + fi + attempt=$((attempt+1)) + if [ $attempt -ge $max_attempts ]; then + echo "Required projections did not start after $max_attempts attempts. Exiting." + exit 1 + fi + echo "Waiting for required projections... (attempt $attempt/$max_attempts)" + sleep 2 +done \ No newline at end of file