From aa99b3dd14f4d2e26444be91eec08e7032265f49 Mon Sep 17 00:00:00 2001 From: malik masis Date: Fri, 25 Mar 2022 14:51:18 +0300 Subject: [PATCH] Removed the mentioned event handlers --- .../DatabaseMigrationEventHandlerBase.cs | 7 - .../DatabaseEfCoreMigrationEventHandler.cs | 133 ---------------- .../DatabaseMongoDbMigrationEventHandler.cs | 149 ------------------ 3 files changed, 289 deletions(-) delete mode 100644 shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/DatabaseMigrationEventHandlerBase.cs delete mode 100644 shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs delete mode 100644 shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/DatabaseMigrationEventHandlerBase.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/DatabaseMigrationEventHandlerBase.cs deleted file mode 100644 index dcaf6366..00000000 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/DatabaseMigrationEventHandlerBase.cs +++ /dev/null @@ -1,7 +0,0 @@ -using Volo.Abp.DependencyInjection; - -namespace EShopOnAbp.Shared.Hosting.Microservices.DbMigrations; - -public abstract class DatabaseMigrationEventHandlerBase : ITransientDependency -{ -} \ No newline at end of file diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs deleted file mode 100644 index 02719e9a..00000000 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs +++ /dev/null @@ -1,133 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; -using Serilog; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Volo.Abp; -using Volo.Abp.Data; -using Volo.Abp.DistributedLocking; -using Volo.Abp.Domain.Entities.Events.Distributed; -using Volo.Abp.EntityFrameworkCore; -using Volo.Abp.EventBus.Distributed; -using Volo.Abp.MultiTenancy; -using Volo.Abp.Uow; - -namespace EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore; - -public abstract class DatabaseEfCoreMigrationEventHandler : DatabaseMigrationEventHandlerBase - where TDbContext : DbContext, IEfCoreDbContext -{ - protected const string TryCountPropertyName = "TryCount"; - protected const int MaxEventTryCount = 3; - protected ICurrentTenant CurrentTenant { get; } - protected IUnitOfWorkManager UnitOfWorkManager { get; } - protected ITenantStore TenantStore { get; } - protected IDistributedEventBus DistributedEventBus { get; } - protected ILogger> Logger { get; set; } - protected string DatabaseName { get; } - protected IAbpDistributedLock DistributedLockProvider { get; } - - protected DatabaseEfCoreMigrationEventHandler( - ICurrentTenant currentTenant, - IUnitOfWorkManager unitOfWorkManager, - ITenantStore tenantStore, - IDistributedEventBus distributedEventBus, - string databaseName, - IAbpDistributedLock distributedLockProvider - ) - { - CurrentTenant = currentTenant; - UnitOfWorkManager = unitOfWorkManager; - TenantStore = tenantStore; - DatabaseName = databaseName; - DistributedEventBus = distributedEventBus; - DistributedLockProvider = distributedLockProvider; - - Logger = NullLogger>.Instance; - } - - /// - /// Apply pending EF Core schema migrations to the database. - /// Returns true if any migration has applied. - /// - protected virtual async Task MigrateDatabaseSchemaAsync() - { - var result = false; - - using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false)) - { - async Task MigrateDatabaseSchemaWithDbContextAsync() - { - var dbContext = await uow.ServiceProvider - .GetRequiredService>() - .GetDbContextAsync(); - - if ((await dbContext.Database.GetPendingMigrationsAsync()).Any()) - { - await dbContext.Database.MigrateAsync(); - return true; - } - - return false; - } - - //Migrating the host database - Log.Information($"There is no tenant. Migrating {DatabaseName}..."); - result = await MigrateDatabaseSchemaWithDbContextAsync(); - - await uow.CompleteAsync(); - } - - return result; - } - - protected virtual async Task HandleErrorOnApplyDatabaseMigrationAsync( - ApplyDatabaseMigrationsEto eventData, - Exception exception) - { - var tryCount = IncrementEventTryCount(eventData); - if (tryCount <= MaxEventTryCount) - { - Log.Warning( - $"Could not apply database migrations. Re-queueing the operation. TenantId = {eventData.TenantId}, Database Name = {eventData.DatabaseName}."); - Log.Error(exception.ToString()); - - await Task.Delay(RandomHelper.GetRandom(5000, 15000)); - Log.Warning("Re publishing the event!"); - await DistributedEventBus.PublishAsync(eventData); - } - else - { - Log.Warning( - $"Could not apply database migrations. Canceling the operation. TenantId = {eventData.TenantId}, DatabaseName = {eventData.DatabaseName}."); - Log.Error(exception.ToString()); - } - } - - private static int GetEventTryCount(EtoBase eventData) - { - var tryCountAsString = eventData.Properties.GetOrDefault(TryCountPropertyName); - if (tryCountAsString.IsNullOrEmpty()) - { - return 0; - } - - return int.Parse(tryCountAsString); - } - - private static void SetEventTryCount(EtoBase eventData, int count) - { - eventData.Properties[TryCountPropertyName] = count.ToString(); - } - - private static int IncrementEventTryCount(EtoBase eventData) - { - var count = GetEventTryCount(eventData) + 1; - SetEventTryCount(eventData, count); - return count; - } -} \ No newline at end of file diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs deleted file mode 100644 index 79784821..00000000 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs +++ /dev/null @@ -1,149 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; -using MongoDB.Driver; -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Volo.Abp; -using Volo.Abp.Data; -using Volo.Abp.DistributedLocking; -using Volo.Abp.Domain.Entities.Events.Distributed; -using Volo.Abp.EventBus.Distributed; -using Volo.Abp.MongoDB; -using Volo.Abp.MultiTenancy; -using Volo.Abp.Uow; - -namespace EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.MongoDb; - -public abstract class DatabaseMongoDbMigrationEventHandler : DatabaseMigrationEventHandlerBase - where TDbContext : AbpMongoDbContext, IAbpMongoDbContext -{ - protected const string TryCountPropertyName = "TryCount"; - protected const int MaxEventTryCount = 3; - - protected ICurrentTenant CurrentTenant { get; } - protected IUnitOfWorkManager UnitOfWorkManager { get; } - protected ITenantStore TenantStore { get; } - protected IDistributedEventBus DistributedEventBus { get; } - protected ILogger> Logger { get; set; } - protected IServiceProvider ServiceProvider { get; } - protected string DatabaseName { get; } - protected IAbpDistributedLock DistributedLockProvider { get; } - - - protected DatabaseMongoDbMigrationEventHandler( - ICurrentTenant currentTenant, - IUnitOfWorkManager unitOfWorkManager, - ITenantStore tenantStore, - IDistributedEventBus distributedEventBus, - string databaseName, - IServiceProvider serviceProvider, - IAbpDistributedLock distributedLockProvider - ) - { - CurrentTenant = currentTenant; - UnitOfWorkManager = unitOfWorkManager; - TenantStore = tenantStore; - DatabaseName = databaseName; - ServiceProvider = serviceProvider; - DistributedEventBus = distributedEventBus; - DistributedLockProvider = distributedLockProvider; - - Logger = NullLogger>.Instance; - } - - /// - /// Apply pending EF Core schema migrations to the database. - /// Returns true if any migration has applied. - /// - protected virtual async Task MigrateDatabaseSchemaAsync() - { - var result = false; - - using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false)) - { - async Task MigrateDatabaseSchemaWithDbContextAsync() - { - var dbContexts = ServiceProvider.GetServices(); - var connectionStringResolver = ServiceProvider.GetRequiredService(); - - foreach (var dbContext in dbContexts) - { - var connectionString = - await connectionStringResolver.ResolveAsync( - ConnectionStringNameAttribute.GetConnStringName(dbContext.GetType())); - if (connectionString.IsNullOrWhiteSpace()) - { - continue; - } - - var mongoUrl = new MongoUrl(connectionString); - var databaseName = mongoUrl.DatabaseName; - var client = new MongoClient(mongoUrl); - - if (databaseName.IsNullOrWhiteSpace()) - { - databaseName = ConnectionStringNameAttribute.GetConnStringName(dbContext.GetType()); - } - - (dbContext as AbpMongoDbContext)?.InitializeCollections(client.GetDatabase(databaseName)); - } - - return true; - } - - //Migrating the host database - result = await MigrateDatabaseSchemaWithDbContextAsync(); - - await uow.CompleteAsync(); - } - - return result; - } - - protected virtual async Task HandleErrorOnApplyDatabaseMigrationAsync( - ApplyDatabaseMigrationsEto eventData, - Exception exception) - { - var tryCount = IncrementEventTryCount(eventData); - if (tryCount <= MaxEventTryCount) - { - Logger.LogWarning( - $"Could not apply database migrations. Re-queueing the operation. TenantId = {eventData.TenantId}, Database Name = {eventData.DatabaseName}."); - Logger.LogException(exception, LogLevel.Warning); - - await Task.Delay(RandomHelper.GetRandom(5000, 15000)); - await DistributedEventBus.PublishAsync(eventData); - } - else - { - Logger.LogError( - $"Could not apply database migrations. Canceling the operation. TenantId = {eventData.TenantId}, DatabaseName = {eventData.DatabaseName}."); - Logger.LogException(exception); - } - } - - private static int GetEventTryCount(EtoBase eventData) - { - var tryCountAsString = eventData.Properties.GetOrDefault(TryCountPropertyName); - if (tryCountAsString.IsNullOrEmpty()) - { - return 0; - } - - return int.Parse(tryCountAsString); - } - - private static void SetEventTryCount(EtoBase eventData, int count) - { - eventData.Properties[TryCountPropertyName] = count.ToString(); - } - - private static int IncrementEventTryCount(EtoBase eventData) - { - var count = GetEventTryCount(eventData) + 1; - SetEventTryCount(eventData, count); - return count; - } -} \ No newline at end of file