Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed TenantCreatedEto handling #88

Merged
merged 2 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +16,6 @@ namespace EShopOnAbp.AdministrationService.DbMigrations
{
public class AdministrationServiceDatabaseMigrationEventHandler
: DatabaseEfCoreMigrationEventHandler<AdministrationServiceDbContext>,
IDistributedEventHandler<TenantCreatedEto>,
IDistributedEventHandler<ApplyDatabaseMigrationsEto>
{
private readonly IPermissionDefinitionManager _permissionDefinitionManager;
Expand Down Expand Up @@ -59,8 +57,8 @@ public async Task HandleEventAsync(ApplyDatabaseMigrationsEto eventData)

if (handle != null)
{
await MigrateDatabaseSchemaAsync(eventData.TenantId);
await SeedDataAsync(eventData.TenantId);
await MigrateDatabaseSchemaAsync();
await SeedDataAsync();
}
}
}
Expand All @@ -70,48 +68,30 @@ public async Task HandleEventAsync(ApplyDatabaseMigrationsEto eventData)
}
}

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();
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task HandleEventAsync(ApplyDatabaseMigrationsEto eventData)
if (handle != null)
{
Log.Information("CatalogService is migrating database...");
await MigrateDatabaseSchemaAsync(null);
await MigrateDatabaseSchemaAsync();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -57,14 +55,13 @@ public async Task HandleEventAsync(ApplyDatabaseMigrationsEto eventData)
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
);
Expand All @@ -79,38 +76,17 @@ await SeedDataAsync(
}
}

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
);

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task HandleEventAsync(ApplyDatabaseMigrationsEto eventData)
if (handle != null)
{
Log.Information("OrderingService is migrating database...");
await MigrateDatabaseSchemaAsync(null);
await MigrateDatabaseSchemaAsync();
Log.Information("OrderingService is seeding data...");
await _dataSeeder.SeedAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task HandleEventAsync(ApplyDatabaseMigrationsEto eventData)
if (handle != null)
{
Log.Information("PaymentService is migrating database...");
await MigrateDatabaseSchemaAsync(null);
await MigrateDatabaseSchemaAsync();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -56,51 +55,36 @@ IAbpDistributedLock distributedLockProvider
/// 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;
}

Expand All @@ -127,50 +111,6 @@ protected virtual async Task HandleErrorOnApplyDatabaseMigrationAsync(
}
}

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);
Expand Down
Loading