|
| 1 | +using Microsoft.EntityFrameworkCore; |
| 2 | +using Microsoft.Extensions.Configuration; |
| 3 | +using Microsoft.Extensions.Hosting; |
| 4 | +using System; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Polly; |
| 11 | +using System.Data.SqlClient; |
| 12 | + |
| 13 | +namespace Catalog.API.Extensions |
| 14 | +{ |
| 15 | + public static class HostExtensions |
| 16 | + { |
| 17 | + public static bool IsInKubernetes(this IHost host) |
| 18 | + { |
| 19 | + var cfg = host.Services.GetService<IConfiguration>(); |
| 20 | + var orchestratorType = cfg.GetValue<string>("OrchestratorType"); |
| 21 | + return orchestratorType?.ToUpper() == "K8S"; |
| 22 | + } |
| 23 | + |
| 24 | + public static IHost MigrateDbContext<TContext>(this IHost host, Action<TContext, IServiceProvider> seeder) where TContext : DbContext |
| 25 | + { |
| 26 | + var underK8s = host.IsInKubernetes(); |
| 27 | + |
| 28 | + using (var scope = host.Services.CreateScope()) |
| 29 | + { |
| 30 | + var services = scope.ServiceProvider; |
| 31 | + |
| 32 | + var logger = services.GetRequiredService<ILogger<TContext>>(); |
| 33 | + |
| 34 | + var context = services.GetService<TContext>(); |
| 35 | + |
| 36 | + try |
| 37 | + { |
| 38 | + logger.LogInformation("Migrating database associated with context {DbContextName}", typeof(TContext).Name); |
| 39 | + |
| 40 | + if (underK8s) |
| 41 | + { |
| 42 | + InvokeSeeder(seeder, context, services); |
| 43 | + } |
| 44 | + else |
| 45 | + { |
| 46 | + var retry = Policy.Handle<SqlException>() |
| 47 | + .WaitAndRetry(new TimeSpan[] |
| 48 | + { |
| 49 | + TimeSpan.FromSeconds(3), |
| 50 | + TimeSpan.FromSeconds(5), |
| 51 | + TimeSpan.FromSeconds(8), |
| 52 | + }); |
| 53 | + |
| 54 | + //if the sql server container is not created on run docker compose this |
| 55 | + //migration can't fail for network related exception. The retry options for DbContext only |
| 56 | + //apply to transient exceptions |
| 57 | + // Note that this is NOT applied when running some orchestrators (let the orchestrator to recreate the failing service) |
| 58 | + retry.Execute(() => InvokeSeeder(seeder, context, services)); |
| 59 | + } |
| 60 | + |
| 61 | + logger.LogInformation("Migrated database associated with context {DbContextName}", typeof(TContext).Name); |
| 62 | + } |
| 63 | + catch (Exception ex) |
| 64 | + { |
| 65 | + logger.LogError(ex, "An error occurred while migrating the database used on context {DbContextName}", typeof(TContext).Name); |
| 66 | + if (underK8s) |
| 67 | + { |
| 68 | + throw; // Rethrow under k8s because we rely on k8s to re-run the pod |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return host; |
| 74 | + } |
| 75 | + |
| 76 | + private static void InvokeSeeder<TContext>(Action<TContext, IServiceProvider> seeder, TContext context, IServiceProvider services) |
| 77 | + where TContext : DbContext |
| 78 | + { |
| 79 | + context.Database.Migrate(); |
| 80 | + seeder(context, services); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments