|
|
@ -1,68 +0,0 @@ |
|
|
|
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Extensions; |
|
|
|
|
|
|
|
public static class WebHostExtensions |
|
|
|
{ |
|
|
|
public static bool IsInKubernetes(this IServiceProvider services) |
|
|
|
{ |
|
|
|
var cfg = services.GetService<IConfiguration>(); |
|
|
|
var orchestratorType = cfg.GetValue<string>("OrchestratorType"); |
|
|
|
return orchestratorType?.ToUpper() == "K8S"; |
|
|
|
} |
|
|
|
|
|
|
|
public static IServiceProvider MigrateDbContext<TContext>(this IServiceProvider services, Action<TContext, IServiceProvider> seeder) where TContext : DbContext |
|
|
|
{ |
|
|
|
var underK8s = services.IsInKubernetes(); |
|
|
|
|
|
|
|
using var scope = services.CreateScope(); |
|
|
|
var scopedServices = scope.ServiceProvider; |
|
|
|
|
|
|
|
var logger = scopedServices.GetRequiredService<ILogger<TContext>>(); |
|
|
|
|
|
|
|
var context = scopedServices.GetService<TContext>(); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
logger.LogInformation("Migrating database associated with context {DbContextName}", typeof(TContext).Name); |
|
|
|
|
|
|
|
if (underK8s) |
|
|
|
{ |
|
|
|
InvokeSeeder(seeder, context, scopedServices); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
var retry = Policy.Handle<SqlException>() |
|
|
|
.WaitAndRetry(new TimeSpan[] |
|
|
|
{ |
|
|
|
TimeSpan.FromSeconds(3), |
|
|
|
TimeSpan.FromSeconds(5), |
|
|
|
TimeSpan.FromSeconds(8), |
|
|
|
}); |
|
|
|
|
|
|
|
//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
|
|
|
|
// Note that this is NOT applied when running some orchestrators (let the orchestrator to recreate the failing service)
|
|
|
|
retry.Execute(() => InvokeSeeder(seeder, context, scopedServices)); |
|
|
|
} |
|
|
|
|
|
|
|
logger.LogInformation("Migrated database associated with context {DbContextName}", typeof(TContext).Name); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
logger.LogError(ex, "An error occurred while migrating the database used on context {DbContextName}", typeof(TContext).Name); |
|
|
|
if (underK8s) |
|
|
|
{ |
|
|
|
throw; // Rethrow under k8s because we rely on k8s to re-run the pod
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return services; |
|
|
|
} |
|
|
|
|
|
|
|
private static void InvokeSeeder<TContext>(Action<TContext, IServiceProvider> seeder, TContext context, IServiceProvider services) |
|
|
|
where TContext : DbContext |
|
|
|
{ |
|
|
|
context.Database.Migrate(); |
|
|
|
seeder(context, services); |
|
|
|
} |
|
|
|
} |