Merge pull request #5752 from OrchardCMS/feature/distributedlocking

Refactored distributed locks to support existing installations.
This commit is contained in:
Sipke Schoorstra
2015-09-10 21:55:16 +01:00
5 changed files with 102 additions and 51 deletions

View File

@@ -1,6 +1,9 @@
using System;
using System.Linq;
using Orchard.Data.Migration.Interpreters;
using Orchard.Data.Migration.Schema;
using Orchard.Environment;
using Orchard.Environment.Configuration;
using Orchard.Environment.Features;
using Orchard.Logging;
using Orchard.Tasks.Locking.Services;
@@ -14,15 +17,24 @@ namespace Orchard.Data.Migration {
private readonly IDataMigrationManager _dataMigrationManager;
private readonly IFeatureManager _featureManager;
private readonly IDistributedLockService _distributedLockService;
private readonly IDataMigrationInterpreter _dataMigrationInterpreter;
private readonly ShellSettings _shellSettings;
private readonly ITransactionManager _transactionManager;
public AutomaticDataMigrations(
IDataMigrationManager dataMigrationManager,
IDataMigrationInterpreter dataMigrationInterpreter,
IFeatureManager featureManager,
IDistributedLockService distributedLockService) {
IDistributedLockService distributedLockService,
ITransactionManager transactionManager,
ShellSettings shellSettings) {
_dataMigrationManager = dataMigrationManager;
_featureManager = featureManager;
_distributedLockService = distributedLockService;
_shellSettings = shellSettings;
_transactionManager = transactionManager;
_dataMigrationInterpreter = dataMigrationInterpreter;
Logger = NullLogger.Instance;
}
@@ -30,6 +42,8 @@ namespace Orchard.Data.Migration {
public ILogger Logger { get; set; }
public void Activated() {
EnsureDistributedLockSchemaExists();
IDistributedLock @lock;
if (_distributedLockService.TryAcquireLock(GetType().FullName, TimeSpan.FromMinutes(30), TimeSpan.FromMilliseconds(250), out @lock)) {
using (@lock) {
@@ -62,5 +76,16 @@ namespace Orchard.Data.Migration {
public void Terminating() {
// No-op.
}
/// <summary>
/// This ensures that the framework migrations have run for the distributed locking feature, as existing Orchard installations will not have the required tables when upgrading.
/// </summary>
private void EnsureDistributedLockSchemaExists() {
// Ensure the distributed lock record schema exists.
var schemaBuilder = new SchemaBuilder(_dataMigrationInterpreter);
var distributedLockSchemaBuilder = new DistributedLockSchemaBuilder(_shellSettings, schemaBuilder);
if (distributedLockSchemaBuilder.EnsureSchema())
_transactionManager.RequireNew();
}
}
}

View File

@@ -401,7 +401,7 @@
<Compile Include="Services\IJsonConverter.cs" />
<Compile Include="Settings\CurrentSiteWorkContext.cs" />
<Compile Include="Settings\ResourceDebugMode.cs" />
<Compile Include="Tasks\Locking\Migrations\FrameworkMigrations.cs" />
<Compile Include="Tasks\Locking\Services\DistributedLockSchemaBuilder.cs" />
<Compile Include="Tasks\Locking\Services\IDistributedLock.cs" />
<Compile Include="Tasks\Locking\Services\DistributedLock.cs" />
<Compile Include="Tasks\Locking\Services\IDistributedLockService.cs" />

View File

@@ -1,22 +0,0 @@
using System;
using Orchard.Data.Migration;
namespace Orchard.Tasks.Locking.Migrations {
public class FrameworkMigrations : DataMigrationImpl {
public int Create() {
SchemaBuilder.CreateTable("DistributedLockRecord", table => table
.Column<int>("Id", column => column.PrimaryKey().Identity())
.Column<string>("Name", column => column.NotNull().WithLength(512).Unique())
.Column<string>("MachineName", column => column.WithLength(256))
.Column<DateTime>("CreatedUtc")
.Column<DateTime>("ValidUntilUtc", column => column.Nullable()));
SchemaBuilder.AlterTable("DistributedLockRecord", table => {
table.CreateIndex("IDX_DistributedLockRecord_Name", "Name");
});
return 1;
}
}
}

View File

@@ -0,0 +1,48 @@
using System;
using Orchard.Data.Migration.Schema;
using Orchard.Environment.Configuration;
namespace Orchard.Tasks.Locking.Services {
public class DistributedLockSchemaBuilder {
private readonly ShellSettings _shellSettings;
private readonly SchemaBuilder _schemaBuilder;
private const string TableName = "Orchard_Framework_DistributedLockRecord";
public DistributedLockSchemaBuilder(ShellSettings shellSettings, SchemaBuilder schemaBuilder) {
_shellSettings = shellSettings;
_schemaBuilder = schemaBuilder;
}
public bool EnsureSchema() {
if (SchemaExists())
return false;
CreateSchema();
return true;
}
public void CreateSchema() {
_schemaBuilder.CreateTable(TableName, table => table
.Column<int>("Id", column => column.PrimaryKey().Identity())
.Column<string>("Name", column => column.NotNull().WithLength(512).Unique())
.Column<string>("MachineName", column => column.WithLength(256))
.Column<DateTime>("CreatedUtc")
.Column<DateTime>("ValidUntilUtc", column => column.Nullable()));
_schemaBuilder.AlterTable(TableName, table => {
table.CreateIndex("IDX_DistributedLockRecord_Name", "Name");
});
}
public bool SchemaExists() {
try {
var tablePrefix = String.IsNullOrEmpty(_shellSettings.DataTablePrefix) ? "" : _shellSettings.DataTablePrefix + "_";
_schemaBuilder.ExecuteSql(String.Format("select * from {0}{1}", tablePrefix, TableName));
return true;
}
catch {
return false;
}
}
}
}