diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs index b8a4881d..029aa848 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs @@ -20,6 +20,9 @@ using Volo.Abp.AspNetCore.Mvc.AntiForgery; using Volo.Abp.Modularity; using Volo.Abp.Threading; using Volo.Abp.Uow; +using Medallion.Threading.Redis; +using Medallion.Threading; +using StackExchange.Redis; namespace EShopOnAbp.CatalogService; @@ -67,6 +70,12 @@ public class CatalogServiceHttpApiHostModule : AbpModule }); }); + context.Services.AddSingleton(sp => + { + var connection = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); + return new RedisDistributedSynchronizationProvider(connection.GetDatabase()); + }); + Configure(options => { options.ConventionalControllers.Create(typeof(CatalogServiceApplicationModule).Assembly, opts => diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs index 9fb54311..1b513492 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs @@ -2,6 +2,8 @@ using System.Threading.Tasks; using EShopOnAbp.CatalogService.MongoDB; using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.MongoDb; +using Medallion.Threading; +using Microsoft.Extensions.Logging; using Volo.Abp.Data; using Volo.Abp.EventBus.Distributed; using Volo.Abp.MultiTenancy; @@ -18,13 +20,16 @@ namespace EShopOnAbp.CatalogService.DbMigrations IUnitOfWorkManager unitOfWorkManager, ITenantStore tenantStore, IDistributedEventBus distributedEventBus, - IServiceProvider serviceProvider + IServiceProvider serviceProvider, + IDistributedLockProvider distributedLockProvider ) : base( currentTenant, unitOfWorkManager, tenantStore, distributedEventBus, - CatalogServiceDbProperties.ConnectionStringName,serviceProvider) + CatalogServiceDbProperties.ConnectionStringName, + serviceProvider, + distributedLockProvider) { } @@ -42,7 +47,15 @@ namespace EShopOnAbp.CatalogService.DbMigrations try { - await MigrateDatabaseSchemaAsync(null); + Logger.LogInformation("CatalogService - Before Acquire "); + + await using (var handle = await DistributedLockProvider.AcquireLockAsync(DatabaseName)) + { + if (handle != null) + { + await MigrateDatabaseSchemaAsync(null); + } + } } catch (Exception ex) { diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs index 452cd7de..251e6a92 100644 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs +++ b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs @@ -1,11 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using Medallion.Threading; 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.Domain.Entities.Events.Distributed; @@ -27,16 +27,20 @@ public abstract class DatabaseMongoDbMigrationEventHandler : Databas protected ITenantStore TenantStore { get; } protected IDistributedEventBus DistributedEventBus { get; } protected ILogger> Logger { get; set; } - protected IServiceProvider ServiceProvider { get; } protected string DatabaseName { get; } + protected IDistributedLockProvider DistributedLockProvider { get; } + protected DatabaseMongoDbMigrationEventHandler( ICurrentTenant currentTenant, IUnitOfWorkManager unitOfWorkManager, ITenantStore tenantStore, IDistributedEventBus distributedEventBus, - string databaseName, IServiceProvider serviceProvider) + string databaseName, + IServiceProvider serviceProvider, + IDistributedLockProvider distributedLockProvider + ) { CurrentTenant = currentTenant; UnitOfWorkManager = unitOfWorkManager; @@ -44,6 +48,7 @@ public abstract class DatabaseMongoDbMigrationEventHandler : Databas DatabaseName = databaseName; ServiceProvider = serviceProvider; DistributedEventBus = distributedEventBus; + DistributedLockProvider = distributedLockProvider; Logger = NullLogger>.Instance; }