From 5470629b354d89abbbd5fc813dcf2ae166c0cfc1 Mon Sep 17 00:00:00 2001 From: malik masis Date: Thu, 24 Mar 2022 16:54:35 +0300 Subject: [PATCH] Added null check for DistributedLock --- .../EfCore/PendingEfCoreMigrationsChecker.cs | 18 ++++++++++++------ .../MongoDb/PendingMongoDbMigrationsChecker.cs | 10 ++++++++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/PendingEfCoreMigrationsChecker.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/PendingEfCoreMigrationsChecker.cs index f43f3aee..82bd596c 100644 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/PendingEfCoreMigrationsChecker.cs +++ b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/PendingEfCoreMigrationsChecker.cs @@ -1,15 +1,14 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Serilog; +using System; +using System.Linq; +using System.Threading.Tasks; using Volo.Abp.Data; using Volo.Abp.DistributedLocking; using Volo.Abp.EventBus.Distributed; using Volo.Abp.MultiTenancy; using Volo.Abp.Uow; -using Volo.Abp.VirtualFileSystem; namespace EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore; @@ -41,7 +40,7 @@ public abstract class PendingEfCoreMigrationsChecker : PendingMigrat public virtual async Task CheckAndApplyDatabaseMigrationsAsync() { - await TryAsync(LockAndApplyDatabaseMigrationsAsync); + await TryAsync(LockAndApplyDatabaseMigrationsAsync); } protected virtual async Task LockAndApplyDatabaseMigrationsAsync() @@ -49,6 +48,13 @@ public abstract class PendingEfCoreMigrationsChecker : PendingMigrat await using (var handle = await DistributedLockProvider.TryAcquireAsync("Migration_" + DatabaseName)) { Log.Information($"Lock is acquired for db migration and seeding on database named: {DatabaseName}..."); + + if (handle is null) + { + Log.Information($"Handle is null because of the locking for : {DatabaseName}"); + return; + } + using (CurrentTenant.Change(null)) { // Create database tables if needed diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/PendingMongoDbMigrationsChecker.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/PendingMongoDbMigrationsChecker.cs index 1b27a8c3..ed8cb227 100644 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/PendingMongoDbMigrationsChecker.cs +++ b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/PendingMongoDbMigrationsChecker.cs @@ -64,10 +64,16 @@ public class PendingMongoDbMigrationsChecker : PendingMigrationsChec var result = false; await using (var handle = await DistributedLockProvider.TryAcquireAsync("Migration_" + DatabaseName)) { - Log.Information($"Lock is acquired for db migration and seeding on database named: {DatabaseName}..."); - using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false)) { + Log.Information($"Lock is acquired for db migration and seeding on database named: {DatabaseName}..."); + + if (handle is null) + { + Log.Information($"Handle is null because of the locking for : {DatabaseName}"); + return false; + } + async Task MigrateDatabaseSchemaWithDbContextAsync() { var dbContexts = ServiceProvider.GetServices();