Skip to content

Commit

Permalink
Fixed error with simultaneous multiple platform instances initializat…
Browse files Browse the repository at this point in the history
…ion, manifested by the use of azure Scale out.
  • Loading branch information
tatarincev committed May 17, 2017
1 parent 2d986ad commit 53d8d66
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
54 changes: 41 additions & 13 deletions VirtoCommerce.Platform.Web/Modularity/ManifestModuleCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using FileLock;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.Platform.Core.Modularity;
using VirtoCommerce.Platform.Core.Properties;
Expand All @@ -27,7 +29,7 @@ public ManifestModuleCatalog(string modulesLocalPath, string contentVirtualPath,
}

protected override void InnerLoad()
{
{
var contentPhysicalPath = _modulesLocalPath;

if (string.IsNullOrEmpty(_assembliesPath))
Expand All @@ -37,33 +39,48 @@ protected override void InnerLoad()
if (string.IsNullOrEmpty(contentPhysicalPath))
throw new InvalidOperationException("The ContentPhysicalPath cannot contain a null value or be empty");

if (!Directory.Exists(_assembliesPath))
Directory.CreateDirectory(_assembliesPath);
//Use lock file in file system to synhronize multiple platform instances initialization (to avoid collisions on initialization process)
var fileLock = SimpleFileLock.Create("vc-lock", TimeSpan.FromMinutes(1));
var needCopyAssemblies = fileLock.TryAcquireLock();

if (!contentPhysicalPath.EndsWith("\\", StringComparison.OrdinalIgnoreCase))
contentPhysicalPath += "\\";

var rootUri = new Uri(contentPhysicalPath);

// Clear directory
// Ignore any errors, because shadow copy may not work
try
{
Array.ForEach(Directory.GetFiles(_assembliesPath), File.Delete);
}
catch (Exception)
if (needCopyAssemblies)
{
}
if (!Directory.Exists(_assembliesPath))
{
Directory.CreateDirectory(_assembliesPath);
}

// Clear ~/moules directory from old assemblies
// Ignore any errors, because shadow copy may not work
try
{
foreach(var assembly in Directory.GetFiles(_assembliesPath))
{
File.Delete(assembly);
}
}
catch (Exception)
{
}

CopyAssemblies(_modulesLocalPath, _assembliesPath);
CopyAssemblies(_modulesLocalPath, _assembliesPath);
}

foreach (var pair in GetModuleManifests())
{
var manifest = pair.Value;
var manifestPath = pair.Key;

var modulePath = Path.GetDirectoryName(manifestPath);
CopyAssemblies(modulePath, _assembliesPath);
if (needCopyAssemblies)
{
CopyAssemblies(modulePath, _assembliesPath);
}

var moduleVirtualPath = GetModuleVirtualPath(rootUri, modulePath);
ConvertVirtualPath(manifest.Scripts, moduleVirtualPath);
Expand All @@ -80,6 +97,17 @@ protected override void InnerLoad()
moduleInfo.IsInstalled = true;
AddModule(moduleInfo);
}

//Wait until other (first) platform instance finished initialization (copying assemblies)
if(!needCopyAssemblies)
{
while (!fileLock.TryAcquireLock())
{
Thread.Sleep(500);
}
}
//Release file system lock
fileLock.ReleaseLock();
}

public override void Validate()
Expand Down
4 changes: 4 additions & 0 deletions VirtoCommerce.Platform.Web/VirtoCommerce.Platform.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FileLock, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\FileLock.1.0.0\lib\net45\FileLock.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Hangfire.Core, Version=1.6.9.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Hangfire.Core.1.6.9\lib\net45\Hangfire.Core.dll</HintPath>
<Private>True</Private>
Expand Down
1 change: 1 addition & 0 deletions VirtoCommerce.Platform.Web/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<package id="CommonServiceLocator" version="1.3" targetFramework="net461" />
<package id="DotLiquid" version="1.8.0" targetFramework="net461" />
<package id="EntityFramework" version="6.1.3" targetFramework="net461" />
<package id="FileLock" version="1.0.0" targetFramework="net461" />
<package id="Hangfire.Core" version="1.6.9" targetFramework="net461" />
<package id="Hangfire.MemoryStorage" version="1.5.0" targetFramework="net461" />
<package id="Hangfire.SqlServer" version="1.6.9" targetFramework="net461" />
Expand Down

0 comments on commit 53d8d66

Please sign in to comment.