diff --git a/README.md b/README.md index 27d403ae..672fdcd1 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,13 @@ You can either run in Visual Studio, or using [Microsoft Tye](https://github.com - Wait until all applications are up! - - You can check running application from tye dashboard ([localhost:8000](http://127.0.0.1:8000/)) + - You can check running application from tye dashboard ([localhost:8000](http://127.0.0.1:8000/)) + - **Note**: If you see all of your applications keep restarting on tye dashboard or tye console, you may be facing ssl certificate issues. To diagnose the problems better, check any application logs. If it is related with SSL, developer certificate creation may have failed because of powershell issues regarding authorization. Check the powershell script running configuration and set policy for your local machine as: + ```bash + Get-ExecutionPolicy list + Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine + ``` + See [Microsoft Powershell documentation](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/get-executionpolicy?view=powershell-7.2) for more information. - After all your backend services are up, start the angular application: diff --git a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs b/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs index beafc3f7..b2017ef9 100644 --- a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs +++ b/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs @@ -1,22 +1,24 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using EShopOnAbp.AdministrationService.DbMigrations; using EShopOnAbp.AdministrationService.EntityFrameworkCore; using EShopOnAbp.Shared.Hosting.AspNetCore; using EShopOnAbp.Shared.Hosting.Microservices; +using Medallion.Threading; +using Medallion.Threading.Redis; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using StackExchange.Redis; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy; using Volo.Abp.Http.Client.IdentityModel.Web; using Volo.Abp.Identity; using Volo.Abp.Modularity; -using Volo.Abp.Threading; namespace EShopOnAbp.AdministrationService; diff --git a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs b/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs index 4640dfcf..2fb26184 100644 --- a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs +++ b/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs @@ -1,10 +1,13 @@ using EShopOnAbp.AdministrationService.EntityFrameworkCore; +using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore; +using Microsoft.Extensions.Logging; using System; using System.Linq; using System.Threading.Tasks; -using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore; +using Serilog; using Volo.Abp.Authorization.Permissions; using Volo.Abp.Data; +using Volo.Abp.DistributedLocking; using Volo.Abp.EventBus.Distributed; using Volo.Abp.MultiTenancy; using Volo.Abp.PermissionManagement; @@ -13,7 +16,7 @@ using Volo.Abp.Uow; namespace EShopOnAbp.AdministrationService.DbMigrations { public class AdministrationServiceDatabaseMigrationEventHandler - : DatabaseEfCoreMigrationEventHandler, + : DatabaseEfCoreMigrationEventHandler, IDistributedEventHandler, IDistributedEventHandler { @@ -26,13 +29,16 @@ namespace EShopOnAbp.AdministrationService.DbMigrations ITenantStore tenantStore, IPermissionDefinitionManager permissionDefinitionManager, IPermissionDataSeeder permissionDataSeeder, - IDistributedEventBus distributedEventBus + IDistributedEventBus distributedEventBus, + IAbpDistributedLock distributedLockProvider ) : base( currentTenant, unitOfWorkManager, tenantStore, distributedEventBus, - AdministrationServiceDbProperties.ConnectionStringName) + AdministrationServiceDbProperties.ConnectionStringName, + distributedLockProvider + ) { _permissionDefinitionManager = permissionDefinitionManager; _permissionDataSeeder = permissionDataSeeder; @@ -47,8 +53,16 @@ namespace EShopOnAbp.AdministrationService.DbMigrations try { - var schemaMigrated = await MigrateDatabaseSchemaAsync(eventData.TenantId); - await SeedDataAsync(eventData.TenantId); + await using (var handle = await DistributedLockProvider.TryAcquireAsync(DatabaseName)) + { + Log.Information("AdministrationService acquired lock for db migration and seeding..."); + + if (handle != null) + { + await MigrateDatabaseSchemaAsync(eventData.TenantId); + await SeedDataAsync(eventData.TenantId); + } + } } catch (Exception ex) { @@ -61,6 +75,7 @@ namespace EShopOnAbp.AdministrationService.DbMigrations try { await MigrateDatabaseSchemaAsync(eventData.Id); + Logger.LogInformation("Starting AdministrationService DataSeeder..."); await SeedDataAsync(eventData.Id); } catch (Exception ex) @@ -82,7 +97,8 @@ namespace EShopOnAbp.AdministrationService.DbMigrations var permissionNames = _permissionDefinitionManager .GetPermissions() .Where(p => p.MultiTenancySide.HasFlag(multiTenancySide)) - .Where(p => !p.Providers.Any() || p.Providers.Contains(RolePermissionValueProvider.ProviderName)) + .Where(p => !p.Providers.Any() || + p.Providers.Contains(RolePermissionValueProvider.ProviderName)) .Select(p => p.Name) .ToArray(); @@ -98,4 +114,4 @@ namespace EShopOnAbp.AdministrationService.DbMigrations } } } -} +} \ No newline at end of file diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs index b8a4881d..72c8cf92 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs @@ -1,24 +1,21 @@ using EShopOnAbp.CatalogService.DbMigrations; +using EShopOnAbp.CatalogService.Grpc; +using EShopOnAbp.CatalogService.MongoDB; using EShopOnAbp.Shared.Hosting.AspNetCore; using EShopOnAbp.Shared.Hosting.Microservices; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Threading.Tasks; -using EShopOnAbp.CatalogService.Grpc; -using EShopOnAbp.CatalogService.MongoDB; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Configuration; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc.AntiForgery; using Volo.Abp.Modularity; -using Volo.Abp.Threading; using Volo.Abp.Uow; namespace EShopOnAbp.CatalogService; 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..0ad78438 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs @@ -1,8 +1,10 @@ -using System; -using System.Threading.Tasks; -using EShopOnAbp.CatalogService.MongoDB; +using EShopOnAbp.CatalogService.MongoDB; using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.MongoDb; +using System; +using System.Threading.Tasks; +using Serilog; using Volo.Abp.Data; +using Volo.Abp.DistributedLocking; using Volo.Abp.EventBus.Distributed; using Volo.Abp.MultiTenancy; using Volo.Abp.Uow; @@ -18,13 +20,16 @@ namespace EShopOnAbp.CatalogService.DbMigrations IUnitOfWorkManager unitOfWorkManager, ITenantStore tenantStore, IDistributedEventBus distributedEventBus, - IServiceProvider serviceProvider + IServiceProvider serviceProvider, + IAbpDistributedLock distributedLockProvider ) : base( currentTenant, unitOfWorkManager, tenantStore, distributedEventBus, - CatalogServiceDbProperties.ConnectionStringName,serviceProvider) + CatalogServiceDbProperties.ConnectionStringName, + serviceProvider, + distributedLockProvider) { } @@ -42,7 +47,16 @@ namespace EShopOnAbp.CatalogService.DbMigrations try { - await MigrateDatabaseSchemaAsync(null); + Log.Information("CatalogService has acquired lock for db migration..."); + + await using (var handle = await DistributedLockProvider.TryAcquireAsync(DatabaseName)) + { + if (handle != null) + { + Log.Information("CatalogService is migrating database..."); + await MigrateDatabaseSchemaAsync(null); + } + } } catch (Exception ex) { diff --git a/services/catalog/src/EShopOnAbp.CatalogService.MongoDB/MongoDB/CatalogServiceMongoDbContext.cs b/services/catalog/src/EShopOnAbp.CatalogService.MongoDB/MongoDB/CatalogServiceMongoDbContext.cs index 3e7570b4..c82af0c6 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.MongoDB/MongoDB/CatalogServiceMongoDbContext.cs +++ b/services/catalog/src/EShopOnAbp.CatalogService.MongoDB/MongoDB/CatalogServiceMongoDbContext.cs @@ -8,7 +8,7 @@ namespace EShopOnAbp.CatalogService.MongoDB [ConnectionStringName(CatalogServiceDbProperties.ConnectionStringName)] public class CatalogServiceMongoDbContext : AbpMongoDbContext, ICatalogServiceMongoDbContext { - public IMongoCollection Questions => Collection(); + public IMongoCollection Products => Collection(); protected override void CreateModel(IMongoModelBuilder modelBuilder) { diff --git a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs index 17d8dd98..71c99268 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs +++ b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs @@ -10,6 +10,7 @@ using Volo.Abp.EventBus.Local; using Volo.Abp.Identity; using Volo.Abp.MultiTenancy; using Volo.Abp.Uow; +using Volo.Abp.DistributedLocking; namespace EShopOnAbp.IdentityService.DbMigrations { @@ -29,13 +30,15 @@ namespace EShopOnAbp.IdentityService.DbMigrations IIdentityDataSeeder identityDataSeeder, IdentityServerDataSeeder identityServerDataSeeder, IDistributedEventBus distributedEventBus, - ILocalEventBus localEventBus + ILocalEventBus localEventBus, + IAbpDistributedLock distributedLockProvider ) : base( currentTenant, unitOfWorkManager, tenantStore, distributedEventBus, - IdentityServiceDbProperties.ConnectionStringName) + IdentityServiceDbProperties.ConnectionStringName, + distributedLockProvider) { _identityDataSeeder = identityDataSeeder; _identityServerDataSeeder = identityServerDataSeeder; @@ -51,12 +54,22 @@ namespace EShopOnAbp.IdentityService.DbMigrations try { - var schemaMigrated = await MigrateDatabaseSchemaAsync(eventData.TenantId); - await SeedDataAsync( - tenantId: eventData.TenantId, - adminEmail: IdentityServiceDbProperties.DefaultAdminEmailAddress, - adminPassword: IdentityServiceDbProperties.DefaultAdminPassword - ); + await using (var handle = await DistributedLockProvider.TryAcquireAsync(DatabaseName)) + { + Log.Information("IdentityService has acquired lock for db migration..."); + + if (handle != null) + { + Log.Information("IdentityService is migrating database..."); + await MigrateDatabaseSchemaAsync(eventData.TenantId); + Log.Information("IdentityService is seeding data..."); + await SeedDataAsync( + tenantId: eventData.TenantId, + adminEmail: IdentityServiceDbProperties.DefaultAdminEmailAddress, + adminPassword: IdentityServiceDbProperties.DefaultAdminPassword + ); + } + } await _localEventBus.PublishAsync(new ApplyDatabaseSeedsEto()); } @@ -64,7 +77,6 @@ namespace EShopOnAbp.IdentityService.DbMigrations { await HandleErrorOnApplyDatabaseMigrationAsync(eventData, ex); } - } public async Task HandleEventAsync(TenantCreatedEto eventData) diff --git a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs index 5fbed800..ddffb7a5 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs +++ b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using EShopOnAbp.IdentityService.DbMigrations; using EShopOnAbp.IdentityService.EntityFrameworkCore; using EShopOnAbp.Shared.Hosting.AspNetCore; @@ -12,9 +8,12 @@ using Microsoft.AspNetCore.Cors; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.Modularity; -using Volo.Abp.Threading; namespace EShopOnAbp.IdentityService; diff --git a/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs b/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs index 98f77546..b2402578 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs @@ -1,9 +1,10 @@ using EShopOnAbp.OrderingService.EntityFrameworkCore; +using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore; using System; using System.Threading.Tasks; -using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore; -using Microsoft.Extensions.Logging; +using Serilog; using Volo.Abp.Data; +using Volo.Abp.DistributedLocking; using Volo.Abp.EventBus.Distributed; using Volo.Abp.MultiTenancy; using Volo.Abp.Uow; @@ -15,18 +16,21 @@ namespace EShopOnAbp.OrderingService.DbMigrations IDistributedEventHandler { private readonly IDataSeeder _dataSeeder; + public OrderingServiceDatabaseMigrationEventHandler( ICurrentTenant currentTenant, IUnitOfWorkManager unitOfWorkManager, ITenantStore tenantStore, - IDistributedEventBus distributedEventBus, - IDataSeeder dataSeeder) + IDistributedEventBus distributedEventBus, + IDataSeeder dataSeeder, + IAbpDistributedLock distributedLockProvider) : base( currentTenant, unitOfWorkManager, tenantStore, distributedEventBus, - OrderingServiceDbProperties.ConnectionStringName) + OrderingServiceDbProperties.ConnectionStringName, + distributedLockProvider) { _dataSeeder = dataSeeder; } @@ -45,9 +49,18 @@ namespace EShopOnAbp.OrderingService.DbMigrations try { - await MigrateDatabaseSchemaAsync(null); - Logger.LogInformation("Starting OrderingService DataSeeder..."); - await _dataSeeder.SeedAsync(); + await using (var handle = await DistributedLockProvider.TryAcquireAsync(DatabaseName)) + { + Log.Information("OrderingService has acquired lock for db migration..."); + + if (handle != null) + { + Log.Information("OrderingService is migrating database..."); + await MigrateDatabaseSchemaAsync(null); + Log.Information("OrderingService is seeding data..."); + await _dataSeeder.SeedAsync(); + } + } } catch (Exception ex) { diff --git a/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/OrderingServiceHttpApiHostModule.cs b/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/OrderingServiceHttpApiHostModule.cs index d5a9a9eb..ed376a3e 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/OrderingServiceHttpApiHostModule.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/OrderingServiceHttpApiHostModule.cs @@ -4,17 +4,16 @@ using EShopOnAbp.Shared.Hosting.AspNetCore; using EShopOnAbp.Shared.Hosting.Microservices; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Microsoft.Extensions.Configuration; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.Modularity; -using Volo.Abp.Threading; namespace EShopOnAbp.OrderingService; @@ -22,6 +21,7 @@ namespace EShopOnAbp.OrderingService; typeof(OrderingServiceHttpApiModule), typeof(OrderingServiceApplicationModule), typeof(OrderingServiceEntityFrameworkCoreModule), + //typeof(MedallionAbpDistributedLock), typeof(EShopOnAbpSharedHostingMicroservicesModule) )] public class OrderingServiceHttpApiHostModule : AbpModule diff --git a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs index 7fd87b4f..2f447e7b 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs +++ b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs @@ -1,8 +1,10 @@ using EShopOnAbp.PaymentService.EntityFrameworkCore; +using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore; using System; using System.Threading.Tasks; -using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore; +using Serilog; using Volo.Abp.Data; +using Volo.Abp.DistributedLocking; using Volo.Abp.EventBus.Distributed; using Volo.Abp.MultiTenancy; using Volo.Abp.Uow; @@ -17,13 +19,15 @@ namespace EShopOnAbp.PaymentService.DbMigrations ICurrentTenant currentTenant, IUnitOfWorkManager unitOfWorkManager, ITenantStore tenantStore, - IDistributedEventBus distributedEventBus) + IDistributedEventBus distributedEventBus, + IAbpDistributedLock distributedLockProvider) : base( currentTenant, unitOfWorkManager, tenantStore, distributedEventBus, - PaymentServiceDbProperties.ConnectionStringName) + PaymentServiceDbProperties.ConnectionStringName, + distributedLockProvider) { } @@ -41,7 +45,16 @@ namespace EShopOnAbp.PaymentService.DbMigrations try { - await MigrateDatabaseSchemaAsync(null); + Log.Information("PaymentService has acquired lock for db migration..."); + + await using (var handle = await DistributedLockProvider.TryAcquireAsync(DatabaseName)) + { + if (handle != null) + { + Log.Information("PaymentService is migrating database..."); + await MigrateDatabaseSchemaAsync(null); + } + } } catch (Exception ex) { diff --git a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs index e267e8d3..d51fbba3 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs +++ b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs @@ -4,16 +4,15 @@ using EShopOnAbp.Shared.Hosting.AspNetCore; using EShopOnAbp.Shared.Hosting.Microservices; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Microsoft.Extensions.Configuration; using Volo.Abp; using Volo.Abp.Modularity; -using Volo.Abp.Threading; namespace EShopOnAbp.PaymentService; diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs index 298102b5..d35f2b91 100644 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs +++ b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Medallion.Threading; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -9,6 +10,7 @@ using Microsoft.Extensions.Logging.Abstractions; using Serilog; 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; @@ -29,19 +31,23 @@ public abstract class DatabaseEfCoreMigrationEventHandler : Database 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) + string databaseName, + IAbpDistributedLock distributedLockProvider + ) { CurrentTenant = currentTenant; UnitOfWorkManager = unitOfWorkManager; TenantStore = tenantStore; DatabaseName = databaseName; DistributedEventBus = distributedEventBus; + DistributedLockProvider = distributedLockProvider; Logger = NullLogger>.Instance; } @@ -76,6 +82,7 @@ public abstract class DatabaseEfCoreMigrationEventHandler : Database if (tenantId == null) { //Migrating the host database + Log.Information($"There is no tenant. Migrating {DatabaseName}..."); result = await MigrateDatabaseSchemaWithDbContextAsync(); } else @@ -85,6 +92,7 @@ public abstract class DatabaseEfCoreMigrationEventHandler : Database !tenantConfiguration.ConnectionStrings.GetOrDefault(DatabaseName).IsNullOrWhiteSpace()) { //Migrating the tenant database (only if tenant has a separate database) + Log.Information($"Migrating tenant database:{DatabaseName} with tenantId:{tenantId}..."); result = await MigrateDatabaseSchemaWithDbContextAsync(); } } @@ -126,7 +134,7 @@ public abstract class DatabaseEfCoreMigrationEventHandler : Database var tryCount = IncrementEventTryCount(eventData); if (tryCount <= MaxEventTryCount) { - Logger.LogWarning( + Log.Warning( $"Could not perform tenant created event. Re-queueing the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}."); Logger.LogException(exception, LogLevel.Warning); diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs index 452cd7de..18235262 100644 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs +++ b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs @@ -1,13 +1,13 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; +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; @@ -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 IAbpDistributedLock DistributedLockProvider { get; } + protected DatabaseMongoDbMigrationEventHandler( ICurrentTenant currentTenant, IUnitOfWorkManager unitOfWorkManager, ITenantStore tenantStore, IDistributedEventBus distributedEventBus, - string databaseName, IServiceProvider serviceProvider) + string databaseName, + IServiceProvider serviceProvider, + IAbpDistributedLock 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; } diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbp.Shared.Hosting.Microservices.csproj b/shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbp.Shared.Hosting.Microservices.csproj index b4bd451f..948da22e 100644 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbp.Shared.Hosting.Microservices.csproj +++ b/shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbp.Shared.Hosting.Microservices.csproj @@ -9,6 +9,7 @@ + diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbpSharedHostingMicroservicesModule.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbpSharedHostingMicroservicesModule.cs index 1e9fcfe6..c3cb453e 100644 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbpSharedHostingMicroservicesModule.cs +++ b/shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbpSharedHostingMicroservicesModule.cs @@ -1,5 +1,7 @@ using EShopOnAbp.AdministrationService.EntityFrameworkCore; using EShopOnAbp.Shared.Hosting.AspNetCore; +using Medallion.Threading; +using Medallion.Threading.Redis; using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.DependencyInjection; using StackExchange.Redis; @@ -26,7 +28,6 @@ namespace EShopOnAbp.Shared.Hosting.Microservices public override void ConfigureServices(ServiceConfigurationContext context) { var configuration = context.Services.GetConfiguration(); - var hostingEnvironment = context.Services.GetHostingEnvironment(); Configure(options => { @@ -42,6 +43,13 @@ namespace EShopOnAbp.Shared.Hosting.Microservices context.Services .AddDataProtection() .PersistKeysToStackExchangeRedis(redis, "EShopOnAbp-Protection-Keys"); + + + context.Services.AddSingleton(sp => + { + var connection = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); + return new RedisDistributedSynchronizationProvider(connection.GetDatabase()); + }); } } } \ No newline at end of file