Skip to content

Commit

Permalink
bug [membership-service] Retry db connection and end program if succe…
Browse files Browse the repository at this point in the history
…ssful

When using ArgoCD for deployment, "wait:true" to wait for the database
in the skaffold.yaml, is not applied, instead all services are created
in parallel. The membership-service tries to connect once to the not yet ready db,
and then waits, doing nothing. To mitigate this the option EnableRetryOnFailure
was added. Now the service tries in a timespan of 60 seconds to connect ten times,
and if it still fails exists the program, causing a recreation of the pod.
  • Loading branch information
MfCrizz committed Mar 10, 2023
1 parent c55a40b commit 753a269
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/membership-service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ private static void CreateDbIfNotExists(IHost host)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred creating the DB.");
// Restart the pod until success
System.Environment.Exit(1);
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/membership-service/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ public void ConfigureServices(IServiceCollection services)
string connectString = $"Server={dbHost};Port=3306;Database=memberships;user=root;password={dbPwd}";
services.AddDbContext<MariaDbContext>(
options => options.UseMySql(
connectString,
new MySqlServerVersion(new Version(10, 5, 8))
connectString,
new MySqlServerVersion(new Version(10, 5, 8)),
options => options.EnableRetryOnFailure(
maxRetryCount: 10,
maxRetryDelay: System.TimeSpan.FromSeconds(60),
errorNumbersToAdd: null)
)
);

Expand Down

0 comments on commit 753a269

Please sign in to comment.