From 8a054ed3492d4e25ec399e4e78e513d5c0b672fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20Tom=C3=A1s?= Date: Mon, 20 Mar 2017 15:37:31 +0100 Subject: [PATCH] Removed the catch with retry logic from CatalogContextSeed and create a retry loop at startup for db availability --- .../Infrastructure/CatalogContextSeed.cs | 54 +++++++------------ src/Services/Catalog/Catalog.API/Startup.cs | 29 +++++++++- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs b/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs index 55ed81d23..40782e5d1 100644 --- a/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs +++ b/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs @@ -13,47 +13,33 @@ { public static async Task SeedAsync(IApplicationBuilder applicationBuilder, ILoggerFactory loggerFactory, int? retry = 0) { - int retryForAvaiability = retry.Value; - try - { - var context = (CatalogContext)applicationBuilder - .ApplicationServices.GetService(typeof(CatalogContext)); - - context.Database.Migrate(); + var context = (CatalogContext)applicationBuilder + .ApplicationServices.GetService(typeof(CatalogContext)); - if (!context.CatalogBrands.Any()) - { - context.CatalogBrands.AddRange( - GetPreconfiguredCatalogBrands()); + context.Database.Migrate(); - await context.SaveChangesAsync(); - } - - if (!context.CatalogTypes.Any()) - { - context.CatalogTypes.AddRange( - GetPreconfiguredCatalogTypes()); + if (!context.CatalogBrands.Any()) + { + context.CatalogBrands.AddRange( + GetPreconfiguredCatalogBrands()); - await context.SaveChangesAsync(); - } + await context.SaveChangesAsync(); + } - if (!context.CatalogItems.Any()) - { - context.CatalogItems.AddRange( - GetPreconfiguredItems()); + if (!context.CatalogTypes.Any()) + { + context.CatalogTypes.AddRange( + GetPreconfiguredCatalogTypes()); - await context.SaveChangesAsync(); - } + await context.SaveChangesAsync(); } - catch (Exception ex) + + if (!context.CatalogItems.Any()) { - if (retryForAvaiability < 10) - { - retryForAvaiability++; - var log = loggerFactory.CreateLogger("catalog seed"); - log.LogError(ex.Message); - await SeedAsync(applicationBuilder, loggerFactory, retryForAvaiability); - } + context.CatalogItems.AddRange( + GetPreconfiguredItems()); + + await context.SaveChangesAsync(); } } diff --git a/src/Services/Catalog/Catalog.API/Startup.cs b/src/Services/Catalog/Catalog.API/Startup.cs index fb6cc3907..3df8c6d20 100644 --- a/src/Services/Catalog/Catalog.API/Startup.cs +++ b/src/Services/Catalog/Catalog.API/Startup.cs @@ -11,6 +11,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; + using System.Data.SqlClient; using System.IO; using System.Reflection; using System.Threading; @@ -95,11 +96,35 @@ app.UseSwagger() .UseSwaggerUi(); + var context = (CatalogContext)app + .ApplicationServices.GetService(typeof(CatalogContext)); + + WaitForSqlAvailability(context, loggerFactory); //Seed Data CatalogContextSeed.SeedAsync(app, loggerFactory) - .Wait(); + .Wait(); + } - + private void WaitForSqlAvailability(CatalogContext ctx, ILoggerFactory loggerFactory, int? retry = 0) + { + int retryForAvailability = retry.Value; + try + { + ctx.Database.OpenConnection(); + } + catch(SqlException ex) + { + if (retryForAvailability < 10) + { + retryForAvailability++; + var log = loggerFactory.CreateLogger(nameof(Startup)); + log.LogError(ex.Message); + WaitForSqlAvailability(ctx, loggerFactory, retryForAvailability); + } + } + finally { + ctx.Database.CloseConnection(); + } } } }