diff --git a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs b/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs index 0832f65b..9b349ff8 100644 --- a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs +++ b/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs @@ -103,6 +103,6 @@ public class AdministrationServiceHttpApiHostModule : AbpModule { await context.ServiceProvider .GetRequiredService() - .CheckAndApplyDatabaseMigrations(); + .CheckAndApplyDatabaseMigrationsAsync(); } } \ No newline at end of file diff --git a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationChecker.cs b/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationChecker.cs index 985627c2..615b3702 100644 --- a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationChecker.cs +++ b/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() diff --git a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationChecker.cs b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationChecker.cs index 7f4aa5ed..b60910e0 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationChecker.cs +++ b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationChecker.cs @@ -12,14 +12,11 @@ namespace EShopOnAbp.IdentityService.DbMigrations; public class IdentityServiceDatabaseMigrationChecker : PendingEfCoreMigrationsChecker { - 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; } } \ No newline at end of file diff --git a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs index 5c8c5df7..98ed74e0 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs +++ b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs @@ -100,6 +100,6 @@ public class IdentityServiceHttpApiHostModule : AbpModule { await context.ServiceProvider .GetRequiredService() - .CheckAndApplyDatabaseMigrations(); + .CheckAndApplyDatabaseMigrationsAsync(); } } \ No newline at end of file diff --git a/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/OrderingServiceHttpApiHostModule.cs b/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/OrderingServiceHttpApiHostModule.cs index 8d49ba83..04057038 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/OrderingServiceHttpApiHostModule.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/OrderingServiceHttpApiHostModule.cs @@ -109,6 +109,6 @@ public class OrderingServiceHttpApiHostModule : AbpModule { await context.ServiceProvider .GetRequiredService() - .CheckAndApplyDatabaseMigrations(); + .CheckAndApplyDatabaseMigrationsAsync(); } } \ No newline at end of file diff --git a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs index 99351464..f9af9d45 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs +++ b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs @@ -100,6 +100,6 @@ public class PaymentServiceHttpApiHostModule : AbpModule { await context.ServiceProvider .GetRequiredService() - .CheckAndApplyDatabaseMigrations(); + .CheckAndApplyDatabaseMigrationsAsync(); } } \ No newline at end of file diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/PendingEfCoreMigrationsChecker.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/PendingEfCoreMigrationsChecker.cs index a93161dd..15e01e67 100644 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/PendingEfCoreMigrationsChecker.cs +++ b/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 : 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)) { diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/PendingMongoDbMigrationsChecker.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/PendingMongoDbMigrationsChecker.cs index a91f2673..1b27a8c3 100644 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/PendingMongoDbMigrationsChecker.cs +++ b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/PendingMongoDbMigrationsChecker.cs @@ -39,18 +39,21 @@ public class PendingMongoDbMigrationsChecker : 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(); + } } - } + }); } /// diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/PendingMigrationsCheckerBase.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/PendingMigrationsCheckerBase.cs index 22d69b2d..8aac8078 100644 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/PendingMigrationsCheckerBase.cs +++ b/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, 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); + } + } } \ No newline at end of file