Browse Source

Merge pull request #94 from abpframework/enisn/migration-refactoring-retry-logic

Retry logic for migrations
pull/90/head
Enis Necipoglu 5 years ago
committed by GitHub
parent
commit
f23dbbfbca
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs
  2. 6
      services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationChecker.cs
  3. 4
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationChecker.cs
  4. 2
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs
  5. 2
      services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/OrderingServiceHttpApiHostModule.cs
  6. 2
      services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs
  7. 8
      shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/PendingEfCoreMigrationsChecker.cs
  8. 17
      shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/PendingMongoDbMigrationsChecker.cs
  9. 28
      shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/PendingMigrationsCheckerBase.cs

2
services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs

@ -103,6 +103,6 @@ public class AdministrationServiceHttpApiHostModule : AbpModule
{
await context.ServiceProvider
.GetRequiredService<AdministrationServiceDatabaseMigrationChecker>()
.CheckAndApplyDatabaseMigrations();
.CheckAndApplyDatabaseMigrationsAsync();
}
}

6
services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationChecker.cs

@ -38,11 +38,11 @@ public class AdministrationServiceDatabaseMigrationChecker
_permissionDataSeeder = permissionDataSeeder;
}
public override async Task CheckAndApplyDatabaseMigrations()
public override async Task CheckAndApplyDatabaseMigrationsAsync()
{
await base.CheckAndApplyDatabaseMigrations();
await base.CheckAndApplyDatabaseMigrationsAsync();
await SeedDataAsync();
await TryAsync(async () => await SeedDataAsync());
}
private async Task SeedDataAsync()

4
services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationChecker.cs

@ -12,14 +12,11 @@ namespace EShopOnAbp.IdentityService.DbMigrations;
public class IdentityServiceDatabaseMigrationChecker : PendingEfCoreMigrationsChecker<IdentityServiceDbContext>
{
protected ILocalEventBus LocalEventBus { get; }
public IdentityServiceDatabaseMigrationChecker(
IUnitOfWorkManager unitOfWorkManager,
IServiceProvider serviceProvider,
ICurrentTenant currentTenant,
IDistributedEventBus distributedEventBus,
ILocalEventBus localEventBus,
IAbpDistributedLock abpDistributedLock)
: base(
unitOfWorkManager,
@ -29,6 +26,5 @@ public class IdentityServiceDatabaseMigrationChecker : PendingEfCoreMigrationsCh
abpDistributedLock,
IdentityServiceDbProperties.ConnectionStringName)
{
LocalEventBus = localEventBus;
}
}

2
services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs

@ -100,6 +100,6 @@ public class IdentityServiceHttpApiHostModule : AbpModule
{
await context.ServiceProvider
.GetRequiredService<IdentityServiceDatabaseMigrationChecker>()
.CheckAndApplyDatabaseMigrations();
.CheckAndApplyDatabaseMigrationsAsync();
}
}

2
services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/OrderingServiceHttpApiHostModule.cs

@ -109,6 +109,6 @@ public class OrderingServiceHttpApiHostModule : AbpModule
{
await context.ServiceProvider
.GetRequiredService<OrderingServiceDatabaseMigrationChecker>()
.CheckAndApplyDatabaseMigrations();
.CheckAndApplyDatabaseMigrationsAsync();
}
}

2
services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs

@ -100,6 +100,6 @@ public class PaymentServiceHttpApiHostModule : AbpModule
{
await context.ServiceProvider
.GetRequiredService<PaymentServiceDatabaseMigrationChecker>()
.CheckAndApplyDatabaseMigrations();
.CheckAndApplyDatabaseMigrationsAsync();
}
}

8
shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/PendingEfCoreMigrationsChecker.cs

@ -9,6 +9,7 @@ using Volo.Abp.DistributedLocking;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
using Volo.Abp.VirtualFileSystem;
namespace EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore;
@ -38,7 +39,12 @@ public abstract class PendingEfCoreMigrationsChecker<TDbContext> : PendingMigrat
DatabaseName = databaseName;
}
public virtual async Task CheckAndApplyDatabaseMigrations()
public virtual async Task CheckAndApplyDatabaseMigrationsAsync()
{
await TryAsync(LockAndApplyDatabaseMigrationsAsync);
}
protected virtual async Task LockAndApplyDatabaseMigrationsAsync()
{
await using (var handle = await DistributedLockProvider.TryAcquireAsync("Migration_" + DatabaseName))
{

17
shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/PendingMongoDbMigrationsChecker.cs

@ -39,18 +39,21 @@ public class PendingMongoDbMigrationsChecker<TDbContext> : PendingMigrationsChec
public virtual async Task CheckAndApplyDatabaseMigrationsAsync()
{
using (CurrentTenant.Change(null))
await TryAsync(async () =>
{
// Create database tables if needed
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
using (CurrentTenant.Change(null))
{
await MigrateDatabaseSchemaAsync();
// Create database tables if needed
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
await MigrateDatabaseSchemaAsync();
await DataSeeder.SeedAsync();
await DataSeeder.SeedAsync();
await uow.CompleteAsync();
await uow.CompleteAsync();
}
}
}
});
}
/// <summary>

28
shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/PendingMigrationsCheckerBase.cs

@ -1,7 +1,33 @@
using Volo.Abp.DependencyInjection;
using Serilog;
using System;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.DependencyInjection;
namespace EShopOnAbp.Shared.Hosting.Microservices.DbMigrations;
public abstract class PendingMigrationsCheckerBase : ITransientDependency
{
public async Task TryAsync(Func<Task> task, int retryCount = 3)
{
try
{
await task();
}
catch (Exception ex)
{
retryCount--;
if (retryCount <= 0)
{
throw;
}
Log.Warning($"{ex.GetType().Name} has been thrown. The operation will be tried {retryCount} times more. Exception:\n{ex.Message}");
await Task.Delay(RandomHelper.GetRandom(5000, 15000));
await TryAsync(task, retryCount);
}
}
}
Loading…
Cancel
Save