From b13003498ae18743d429643c8eab52a56341a4d4 Mon Sep 17 00:00:00 2001 From: Thomas Nind Date: Tue, 16 Mar 2021 13:16:28 +0000 Subject: [PATCH] Added lock to prevent parallel checks attempting to create database at same time --- .../FoDicomBased/FoDicomAnonymiser.cs | 59 ++++++++++--------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/Rdmp.Dicom/Extraction/FoDicomBased/FoDicomAnonymiser.cs b/Rdmp.Dicom/Extraction/FoDicomBased/FoDicomAnonymiser.cs index 2e9cd742..80f1f07b 100644 --- a/Rdmp.Dicom/Extraction/FoDicomBased/FoDicomAnonymiser.cs +++ b/Rdmp.Dicom/Extraction/FoDicomBased/FoDicomAnonymiser.cs @@ -184,54 +184,55 @@ public void PreInitialize(IExtractCommand value, IDataLoadEventListener listener _extractCommand = value as IExtractDatasetCommand; } + private static object CreateServersOneAtATime = new object(); + public void Check(ICheckNotifier notifier) { - if(UIDMappingServer == null) + lock(CreateServersOneAtATime) { - throw new Exception($"{nameof(UIDMappingServer)} not set, set it existing UID mapping server or to an empty database to create a new one"); - } + if (UIDMappingServer == null) + { + throw new Exception($"{nameof(UIDMappingServer)} not set, set it existing UID mapping server or to an empty database to create a new one"); + } - var patcher = new SMIDatabasePatcher(); + var patcher = new SMIDatabasePatcher(); - if(!UIDMappingServer.WasCreatedBy(patcher)) - { - if (string.IsNullOrWhiteSpace(UIDMappingServer.CreatedByAssembly)) + if (!UIDMappingServer.WasCreatedBy(patcher)) { - bool create = notifier.OnCheckPerformed(new CheckEventArgs($"{nameof(UIDMappingServer)} is not set up yet", CheckResult.Warning, null, "Attempt to create UID mapping schema")); - - if(create) + if (string.IsNullOrWhiteSpace(UIDMappingServer.CreatedByAssembly)) { - var db = UIDMappingServer.Discover(ReusableLibraryCode.DataAccess.DataAccessContext.DataExport); + bool create = notifier.OnCheckPerformed(new CheckEventArgs($"{nameof(UIDMappingServer)} is not set up yet", CheckResult.Warning, null, "Attempt to create UID mapping schema")); - if (!db.Exists()) + if (create) { - notifier.OnCheckPerformed(new CheckEventArgs($"About to create {db}", CheckResult.Success)); - db.Create(); - } + var db = UIDMappingServer.Discover(ReusableLibraryCode.DataAccess.DataAccessContext.DataExport); - notifier.OnCheckPerformed(new CheckEventArgs($"Creating UID Mapping schema in {db}", CheckResult.Success)); + if (!db.Exists()) + { + notifier.OnCheckPerformed(new CheckEventArgs($"About to create {db}", CheckResult.Success)); + db.Create(); + } - var scripter = new MasterDatabaseScriptExecutor(db); - scripter.CreateAndPatchDatabase(patcher, new AcceptAllCheckNotifier()); + notifier.OnCheckPerformed(new CheckEventArgs($"Creating UID Mapping schema in {db}", CheckResult.Success)); - UIDMappingServer.CreatedByAssembly = patcher.Name; - UIDMappingServer.SaveToDatabase(); + var scripter = new MasterDatabaseScriptExecutor(db); + scripter.CreateAndPatchDatabase(patcher, new AcceptAllCheckNotifier()); + + UIDMappingServer.CreatedByAssembly = patcher.Name; + UIDMappingServer.SaveToDatabase(); + } + else + { + return; + } } else { + notifier.OnCheckPerformed(new CheckEventArgs($"{nameof(UIDMappingServer)} '{UIDMappingServer}' was created by '{UIDMappingServer.CreatedByAssembly}' not a UID patcher. Try creating a new server reference to a blank database", CheckResult.Fail)); return; } } - else - { - notifier.OnCheckPerformed(new CheckEventArgs($"{nameof(UIDMappingServer)} '{UIDMappingServer}' was created by '{UIDMappingServer.CreatedByAssembly}' not a UID patcher. Try creating a new server reference to a blank database",CheckResult.Fail)); - return; - } } - - - - } } }