Browse Source

Merge pull request #88 from abpframework/Issue-11904-Remove-TenantCreatedEto

Removed TenantCreatedEto handling
pull/89/head
Galip Tolga Erdem 5 years ago
committed by GitHub
parent
commit
f09b7b91a4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 60
      services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs
  2. 2
      services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs
  3. 52
      services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs
  4. 2
      services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs
  5. 2
      services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs
  6. 106
      shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs
  7. 112
      shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs

60
services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs

@ -1,10 +1,9 @@
using EShopOnAbp.AdministrationService.EntityFrameworkCore;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore;
using Microsoft.Extensions.Logging;
using Serilog;
using System;
using System.Linq;
using System.Threading.Tasks;
using Serilog;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Data;
using Volo.Abp.DistributedLocking;
@ -17,7 +16,6 @@ namespace EShopOnAbp.AdministrationService.DbMigrations
{
public class AdministrationServiceDatabaseMigrationEventHandler
: DatabaseEfCoreMigrationEventHandler<AdministrationServiceDbContext>,
IDistributedEventHandler<TenantCreatedEto>,
IDistributedEventHandler<ApplyDatabaseMigrationsEto>
{
private readonly IPermissionDefinitionManager _permissionDefinitionManager;
@ -59,8 +57,8 @@ namespace EShopOnAbp.AdministrationService.DbMigrations
if (handle != null)
{
await MigrateDatabaseSchemaAsync(eventData.TenantId);
await SeedDataAsync(eventData.TenantId);
await MigrateDatabaseSchemaAsync();
await SeedDataAsync();
}
}
}
@ -70,48 +68,30 @@ namespace EShopOnAbp.AdministrationService.DbMigrations
}
}
public async Task HandleEventAsync(TenantCreatedEto eventData)
private async Task SeedDataAsync()
{
try
{
await MigrateDatabaseSchemaAsync(eventData.Id);
Logger.LogInformation("Starting AdministrationService DataSeeder...");
await SeedDataAsync(eventData.Id);
}
catch (Exception ex)
{
await HandleErrorTenantCreatedAsync(eventData, ex);
}
}
private async Task SeedDataAsync(Guid? tenantId)
{
using (CurrentTenant.Change(tenantId))
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: true))
{
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: true))
{
var multiTenancySide = tenantId == null
? MultiTenancySides.Host
: MultiTenancySides.Tenant;
var multiTenancySide = MultiTenancySides.Host;
var permissionNames = _permissionDefinitionManager
.GetPermissions()
.Where(p => p.MultiTenancySide.HasFlag(multiTenancySide))
.Where(p => !p.Providers.Any() ||
p.Providers.Contains(RolePermissionValueProvider.ProviderName))
.Select(p => p.Name)
.ToArray();
var permissionNames = _permissionDefinitionManager
.GetPermissions()
.Where(p => p.MultiTenancySide.HasFlag(multiTenancySide))
.Where(p => !p.Providers.Any() ||
p.Providers.Contains(RolePermissionValueProvider.ProviderName))
.Select(p => p.Name)
.ToArray();
await _permissionDataSeeder.SeedAsync(
RolePermissionValueProvider.ProviderName,
"admin",
permissionNames,
tenantId
);
await _permissionDataSeeder.SeedAsync(
RolePermissionValueProvider.ProviderName,
"admin",
permissionNames
);
await uow.CompleteAsync();
}
await uow.CompleteAsync();
}
}
}
}

2
services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs

@ -54,7 +54,7 @@ namespace EShopOnAbp.CatalogService.DbMigrations
if (handle != null)
{
Log.Information("CatalogService is migrating database...");
await MigrateDatabaseSchemaAsync(null);
await MigrateDatabaseSchemaAsync();
}
}
}

52
services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs

@ -1,22 +1,20 @@
using EShopOnAbp.IdentityService.EntityFrameworkCore;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore;
using Serilog;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore;
using Volo.Abp.Data;
using Volo.Abp.DistributedLocking;
using Volo.Abp.EventBus.Distributed;
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
{
public class IdentityServiceDatabaseMigrationEventHandler
: DatabaseEfCoreMigrationEventHandler<IdentityServiceDbContext>,
IDistributedEventHandler<TenantCreatedEto>,
IDistributedEventHandler<ApplyDatabaseMigrationsEto>
{
private readonly IIdentityDataSeeder _identityDataSeeder;
@ -57,14 +55,13 @@ namespace EShopOnAbp.IdentityService.DbMigrations
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);
await MigrateDatabaseSchemaAsync();
Log.Information("IdentityService is seeding data...");
await SeedDataAsync(
tenantId: eventData.TenantId,
adminEmail: IdentityServiceDbProperties.DefaultAdminEmailAddress,
adminPassword: IdentityServiceDbProperties.DefaultAdminPassword
);
@ -79,38 +76,17 @@ namespace EShopOnAbp.IdentityService.DbMigrations
}
}
public async Task HandleEventAsync(TenantCreatedEto eventData)
private async Task SeedDataAsync(string adminEmail, string adminPassword)
{
try
{
await MigrateDatabaseSchemaAsync(eventData.Id);
await SeedDataAsync(
tenantId: eventData.Id,
adminEmail: eventData.Properties.GetOrDefault(IdentityDataSeedContributor.AdminEmailPropertyName) ?? IdentityServiceDbProperties.DefaultAdminEmailAddress,
adminPassword: eventData.Properties.GetOrDefault(IdentityDataSeedContributor.AdminPasswordPropertyName) ?? IdentityServiceDbProperties.DefaultAdminPassword
);
}
catch (Exception ex)
{
await HandleErrorTenantCreatedAsync(eventData, ex);
}
}
private async Task SeedDataAsync(Guid? tenantId, string adminEmail, string adminPassword)
{
using (CurrentTenant.Change(tenantId))
{
if (tenantId == null)
{
Log.Information($"Seeding IdentityServer data...");
await _identityServerDataSeeder.SeedAsync();
}
Log.Information($"Seeding user data...");
await _identityDataSeeder.SeedAsync(
adminEmail,
adminPassword,
tenantId
);
}
Log.Information($"Seeding IdentityServer data...");
await _identityServerDataSeeder.SeedAsync();
Log.Information($"Seeding user data...");
await _identityDataSeeder.SeedAsync(
adminEmail,
adminPassword
);
}
}
}

2
services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs

@ -56,7 +56,7 @@ namespace EShopOnAbp.OrderingService.DbMigrations
if (handle != null)
{
Log.Information("OrderingService is migrating database...");
await MigrateDatabaseSchemaAsync(null);
await MigrateDatabaseSchemaAsync();
Log.Information("OrderingService is seeding data...");
await _dataSeeder.SeedAsync();
}

2
services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs

@ -52,7 +52,7 @@ namespace EShopOnAbp.PaymentService.DbMigrations
if (handle != null)
{
Log.Information("PaymentService is migrating database...");
await MigrateDatabaseSchemaAsync(null);
await MigrateDatabaseSchemaAsync();
}
}
}

106
shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs

@ -1,13 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Medallion.Threading;
using Microsoft.EntityFrameworkCore;
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;
@ -56,51 +55,36 @@ public abstract class DatabaseEfCoreMigrationEventHandler<TDbContext> : Database
/// Apply pending EF Core schema migrations to the database.
/// Returns true if any migration has applied.
/// </summary>
protected virtual async Task<bool> MigrateDatabaseSchemaAsync(Guid? tenantId)
protected virtual async Task<bool> MigrateDatabaseSchemaAsync()
{
var result = false;
using (CurrentTenant.Change(tenantId))
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
async Task<bool> MigrateDatabaseSchemaWithDbContextAsync()
{
async Task<bool> MigrateDatabaseSchemaWithDbContextAsync()
{
var dbContext = await uow.ServiceProvider
.GetRequiredService<IDbContextProvider<TDbContext>>()
.GetDbContextAsync();
if ((await dbContext.Database.GetPendingMigrationsAsync()).Any())
{
await dbContext.Database.MigrateAsync();
return true;
}
var dbContext = await uow.ServiceProvider
.GetRequiredService<IDbContextProvider<TDbContext>>()
.GetDbContextAsync();
return false;
}
if (tenantId == null)
if ((await dbContext.Database.GetPendingMigrationsAsync()).Any())
{
//Migrating the host database
Log.Information($"There is no tenant. Migrating {DatabaseName}...");
result = await MigrateDatabaseSchemaWithDbContextAsync();
}
else
{
var tenantConfiguration = await TenantStore.FindAsync(tenantId.Value);
if (!tenantConfiguration.ConnectionStrings.Default.IsNullOrWhiteSpace() ||
!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();
}
await dbContext.Database.MigrateAsync();
return true;
}
await uow.CompleteAsync();
return false;
}
//Migrating the host database
Log.Information($"There is no tenant. Migrating {DatabaseName}...");
result = await MigrateDatabaseSchemaWithDbContextAsync();
await uow.CompleteAsync();
}
return result;
}
@ -127,50 +111,6 @@ public abstract class DatabaseEfCoreMigrationEventHandler<TDbContext> : Database
}
}
protected virtual async Task HandleErrorTenantCreatedAsync(
TenantCreatedEto eventData,
Exception exception)
{
var tryCount = IncrementEventTryCount(eventData);
if (tryCount <= MaxEventTryCount)
{
Log.Warning(
$"Could not perform tenant created event. Re-queueing the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception, LogLevel.Warning);
await Task.Delay(RandomHelper.GetRandom(5000, 15000));
await DistributedEventBus.PublishAsync(eventData);
}
else
{
Logger.LogError(
$"Could not perform tenant created event. Canceling the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception);
}
}
protected virtual async Task HandleErrorTenantConnectionStringUpdatedAsync(
TenantConnectionStringUpdatedEto eventData,
Exception exception)
{
var tryCount = IncrementEventTryCount(eventData);
if (tryCount <= MaxEventTryCount)
{
Logger.LogWarning(
$"Could not perform tenant connection string updated event. Re-queueing the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception, LogLevel.Warning);
await Task.Delay(RandomHelper.GetRandom(5000, 15000));
await DistributedEventBus.PublishAsync(eventData);
}
else
{
Logger.LogError(
$"Could not perform tenant connection string updated event. Canceling the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception);
}
}
private static int GetEventTryCount(EtoBase eventData)
{
var tryCountAsString = eventData.Properties.GetOrDefault(TryCountPropertyName);

112
shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs

@ -37,7 +37,7 @@ public abstract class DatabaseMongoDbMigrationEventHandler<TDbContext> : Databas
IUnitOfWorkManager unitOfWorkManager,
ITenantStore tenantStore,
IDistributedEventBus distributedEventBus,
string databaseName,
string databaseName,
IServiceProvider serviceProvider,
IAbpDistributedLock distributedLockProvider
)
@ -57,62 +57,46 @@ public abstract class DatabaseMongoDbMigrationEventHandler<TDbContext> : Databas
/// Apply pending EF Core schema migrations to the database.
/// Returns true if any migration has applied.
/// </summary>
protected virtual async Task<bool> MigrateDatabaseSchemaAsync(Guid? tenantId)
protected virtual async Task<bool> MigrateDatabaseSchemaAsync()
{
var result = false;
using (CurrentTenant.Change(tenantId))
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
async Task<bool> MigrateDatabaseSchemaWithDbContextAsync()
{
async Task<bool> MigrateDatabaseSchemaWithDbContextAsync()
{
var dbContexts = ServiceProvider.GetServices<IAbpMongoDbContext>();
var connectionStringResolver = ServiceProvider.GetRequiredService<IConnectionStringResolver>();
var dbContexts = ServiceProvider.GetServices<IAbpMongoDbContext>();
var connectionStringResolver = ServiceProvider.GetRequiredService<IConnectionStringResolver>();
foreach (var dbContext in dbContexts)
foreach (var dbContext in dbContexts)
{
var connectionString =
await connectionStringResolver.ResolveAsync(
ConnectionStringNameAttribute.GetConnStringName(dbContext.GetType()));
if (connectionString.IsNullOrWhiteSpace())
{
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));
continue;
}
return true;
}
var mongoUrl = new MongoUrl(connectionString);
var databaseName = mongoUrl.DatabaseName;
var client = new MongoClient(mongoUrl);
if (tenantId == null)
{
//Migrating the host database
result = await MigrateDatabaseSchemaWithDbContextAsync();
}
else
{
var tenantConfiguration = await TenantStore.FindAsync(tenantId.Value);
if (!tenantConfiguration.ConnectionStrings.Default.IsNullOrWhiteSpace() ||
!tenantConfiguration.ConnectionStrings.GetOrDefault(DatabaseName).IsNullOrWhiteSpace())
if (databaseName.IsNullOrWhiteSpace())
{
//Migrating the tenant database (only if tenant has a separate database)
result = await MigrateDatabaseSchemaWithDbContextAsync();
databaseName = ConnectionStringNameAttribute.GetConnStringName(dbContext.GetType());
}
(dbContext as AbpMongoDbContext)?.InitializeCollections(client.GetDatabase(databaseName));
}
await uow.CompleteAsync();
return true;
}
//Migrating the host database
result = await MigrateDatabaseSchemaWithDbContextAsync();
await uow.CompleteAsync();
}
return result;
@ -140,50 +124,6 @@ public abstract class DatabaseMongoDbMigrationEventHandler<TDbContext> : Databas
}
}
protected virtual async Task HandleErrorTenantCreatedAsync(
TenantCreatedEto eventData,
Exception exception)
{
var tryCount = IncrementEventTryCount(eventData);
if (tryCount <= MaxEventTryCount)
{
Logger.LogWarning(
$"Could not perform tenant created event. Re-queueing the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception, LogLevel.Warning);
await Task.Delay(RandomHelper.GetRandom(5000, 15000));
await DistributedEventBus.PublishAsync(eventData);
}
else
{
Logger.LogError(
$"Could not perform tenant created event. Canceling the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception);
}
}
protected virtual async Task HandleErrorTenantConnectionStringUpdatedAsync(
TenantConnectionStringUpdatedEto eventData,
Exception exception)
{
var tryCount = IncrementEventTryCount(eventData);
if (tryCount <= MaxEventTryCount)
{
Logger.LogWarning(
$"Could not perform tenant connection string updated event. Re-queueing the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception, LogLevel.Warning);
await Task.Delay(RandomHelper.GetRandom(5000, 15000));
await DistributedEventBus.PublishAsync(eventData);
}
else
{
Logger.LogError(
$"Could not perform tenant connection string updated event. Canceling the operation. TenantId = {eventData.Id}, TenantName = {eventData.Name}.");
Logger.LogException(exception);
}
}
private static int GetEventTryCount(EtoBase eventData)
{
var tryCountAsString = eventData.Properties.GetOrDefault(TryCountPropertyName);

Loading…
Cancel
Save