From 8a31efeab2a330643792ea80786f7da3e93dc957 Mon Sep 17 00:00:00 2001 From: eiximenis Date: Tue, 29 Jan 2019 11:43:15 +0100 Subject: [PATCH] badge for buid status --- README.md | 3 ++ .../WebHostExtensions.cs | 49 +++++++++++++------ src/Services/Ordering/Ordering.API/web.config | 10 ++-- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 037d532a3..ffdf6450a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ # eShopOnContainers - Microservices Architecture and Containers based Reference Application (**BETA state** - Visual Studio 2017 and CLI environments compatible) Sample .NET Core reference application, powered by Microsoft, based on a simplified microservices architecture and Docker containers. +[![Build status (Linux images)](https://msftdevtools.visualstudio.com/eShopOnContainers/_apis/build/status/All%20Microservices%20Linux)](https://msftdevtools.visualstudio.com/eShopOnContainers/_build/latest?definitionId=184) + + ## IMPORTANT NOTES! **You can use either the latest version of Visual Studio or simply Docker CLI and .NET CLI for Windows, Mac and Linux**. diff --git a/src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHostExtensions.cs b/src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHostExtensions.cs index ef80f77cf..0da18f581 100644 --- a/src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHostExtensions.cs +++ b/src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHostExtensions.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Polly; @@ -10,8 +11,17 @@ namespace Microsoft.AspNetCore.Hosting { public static class IWebHostExtensions { + public static bool IsInKubernetes(this IWebHost webHost) + { + var cfg = webHost.Services.GetService(); + var orchestratorType = cfg.GetValue("OrchestratorType"); + return orchestratorType?.ToUpper() == "K8S"; + } + public static IWebHost MigrateDbContext(this IWebHost webHost, Action seeder) where TContext : DbContext { + var underK8s = webHost.IsInKubernetes(); + using (var scope = webHost.Services.CreateScope()) { var services = scope.ServiceProvider; @@ -24,36 +34,47 @@ namespace Microsoft.AspNetCore.Hosting { logger.LogInformation($"Migrating database associated with context {typeof(TContext).Name}"); - var retry = Policy.Handle() - .WaitAndRetry(new TimeSpan[] - { + if (underK8s) + { + InvokeSeeder(seeder, context, services); + } + else + { + var retry = Policy.Handle() + .WaitAndRetry(new TimeSpan[] + { TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(8), - }); + }); - retry.Execute(() => - { //if the sql server container is not created on run docker compose this //migration can't fail for network related exception. The retry options for DbContext only - //apply to transient exceptions. - - context.Database - .Migrate(); - - seeder(context, services); - }); - + //apply to transient exceptions + // Note that this is NOT applied when running some orchestrators (let the orchestrator to recreate the failing service) + retry.Execute(() => InvokeSeeder(seeder, context, services)); + } logger.LogInformation($"Migrated database associated with context {typeof(TContext).Name}"); } catch (Exception ex) { logger.LogError(ex, $"An error occurred while migrating the database used on context {typeof(TContext).Name}"); + if (underK8s) + { + throw; // Rethrow under k8s because we rely on k8s to re-run the pod + } } } return webHost; } + + private static void InvokeSeeder(Action seeder, TContext context, IServiceProvider services) + where TContext : DbContext + { + context.Database.Migrate(); + seeder(context, services); + } } } diff --git a/src/Services/Ordering/Ordering.API/web.config b/src/Services/Ordering/Ordering.API/web.config index dc0514fca..3d49211e5 100644 --- a/src/Services/Ordering/Ordering.API/web.config +++ b/src/Services/Ordering/Ordering.API/web.config @@ -1,14 +1,14 @@  - - - + - + + + - + \ No newline at end of file