Specialize IVirtualPathMonitor for command line host

More deterministic behavior and no file locking at
the expense of performance.

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-07-24 18:24:44 -07:00
parent 72d52caa03
commit db8d38224a
5 changed files with 116 additions and 15 deletions

View File

@@ -8,6 +8,7 @@ using Autofac;
using Orchard.Environment;
using Orchard.Environment.Configuration;
using Orchard.Environment.State;
using Orchard.FileSystems.VirtualPath;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Tasks;
@@ -153,6 +154,7 @@ namespace Orchard.Commands {
MvcSingletons(builder);
builder.RegisterType<CommandHostEnvironment>().As<IHostEnvironment>();
builder.RegisterType<CommandBackgroundService>().As<IBackgroundService>();
builder.RegisterType<CommandHostVirtualPathMonitor>().As<IVirtualPathMonitor>();
}
protected void MvcSingletons(ContainerBuilder builder) {

View File

@@ -1,25 +1,10 @@
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Web.Hosting;
using Orchard.Environment;
using Orchard.Localization;
namespace Orchard.Commands {
public class OrchardCommandHostRetryException : OrchardCoreException {
public OrchardCommandHostRetryException(LocalizedString message)
: base(message) {
}
public OrchardCommandHostRetryException(LocalizedString message, Exception innerException)
: base(message, innerException) {
}
protected OrchardCommandHostRetryException(SerializationInfo info, StreamingContext context)
: base(info, context) {
}
}
public class CommandHostEnvironment : IHostEnvironment {
public CommandHostEnvironment() {
T = NullLocalizer.Instance;

View File

@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Orchard.Caching;
using Orchard.FileSystems.VirtualPath;
namespace Orchard.Commands {
public class CommandHostVirtualPathMonitor : IVirtualPathMonitor {
private readonly IVirtualPathProvider _virtualPathProvider;
public CommandHostVirtualPathMonitor(IVirtualPathProvider virtualPathProvider) {
_virtualPathProvider = virtualPathProvider;
}
public IVolatileToken WhenPathChanges(string virtualPath) {
var filename = _virtualPathProvider.MapPath(virtualPath);
if (File.Exists(filename)) {
return new FileToken(filename);
}
if (Directory.Exists(filename)) {
return new DirectoryToken(filename);
}
return new EmptyVolativeToken(filename);
}
public class EmptyVolativeToken : IVolatileToken {
private readonly string _filename;
public EmptyVolativeToken(string filename) {
_filename = filename;
}
public bool IsCurrent {
get {
if (Directory.Exists(_filename)) {
return false;
}
if (File.Exists(_filename)) {
return false;
}
return true;
}
}
}
public class FileToken : IVolatileToken {
private readonly string _filename;
private readonly DateTime _lastWriteTimeUtc;
public FileToken(string filename) {
_filename = filename;
_lastWriteTimeUtc = File.GetLastWriteTimeUtc(filename);
}
public bool IsCurrent {
get {
try {
if (_lastWriteTimeUtc != File.GetLastWriteTimeUtc(_filename))
return false;
}
catch {
return false;
}
return true;
}
}
}
public class DirectoryToken : IVolatileToken {
private readonly string _filename;
private readonly DateTime _lastWriteTimeUtc;
public DirectoryToken(string filename) {
_filename = filename;
_lastWriteTimeUtc = Directory.GetLastWriteTimeUtc(filename);
}
public bool IsCurrent {
get {
try {
if (_lastWriteTimeUtc != Directory.GetLastWriteTimeUtc(_filename))
return false;
}
catch {
return false;
}
return true;
}
}
}
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Runtime.Serialization;
using Orchard.Localization;
namespace Orchard.Commands {
public class OrchardCommandHostRetryException : OrchardCoreException {
public OrchardCommandHostRetryException(LocalizedString message)
: base(message) {
}
public OrchardCommandHostRetryException(LocalizedString message, Exception innerException)
: base(message, innerException) {
}
protected OrchardCommandHostRetryException(SerializationInfo info, StreamingContext context)
: base(info, context) {
}
}
}

View File

@@ -135,6 +135,8 @@
<Compile Include="Collections\PageOfItems.cs" />
<Compile Include="Commands\CommandBackgroundService.cs" />
<Compile Include="Commands\CommandHostEnvironment.cs" />
<Compile Include="Commands\CommandHostVirtualPathMonitor.cs" />
<Compile Include="Commands\OrchardCommandHostRetryException.cs" />
<Compile Include="ContentManagement\Aspects\ICommonPart.cs">
<SubType>Code</SubType>
</Compile>