Fix background tasks processing on command line

* Don't run background tasks in orchard command line host
* Run background tasks in WebApp host

This required introducing the notion of "ShellRegistrations" through
a new IShellContainerRegistrations insterface, which is executed
everytime a new shell container is created, so that a host (e.g.
command line in this case) has the ability to override default
IXxxDependency registrations.

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-07-29 19:09:48 -07:00
parent 57368adcd1
commit a2ee8eff70
8 changed files with 62 additions and 10 deletions

View File

@@ -1,7 +1,13 @@
using Orchard.Tasks;
namespace Orchard.Commands {
public class CommandBackgroundService : IBackgroundService {
/// <summary>
/// Command line specific "no-op" background service implementation.
/// Note that we make this class "internal" so that it's not auto-registered
/// by the Orchard Framework (it is registered explicitly by the command
/// line host).
/// </summary>
internal class CommandBackgroundService : IBackgroundService {
public void Sweep() {
// Don't run any background service in command line
}

View File

@@ -5,6 +5,7 @@ using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Orchard.Caching;
using Orchard.Environment;
using Orchard.Environment.Configuration;
using Orchard.Environment.State;
@@ -152,9 +153,24 @@ namespace Orchard.Commands {
protected void ContainerRegistrations(ContainerBuilder builder) {
MvcSingletons(builder);
builder.RegisterType<CommandHostEnvironment>().As<IHostEnvironment>();
builder.RegisterType<CommandBackgroundService>().As<IBackgroundService>();
builder.RegisterType<CommandHostVirtualPathMonitor>().As<IVirtualPathMonitor>();
builder.RegisterType<CommandHostEnvironment>().As<IHostEnvironment>().SingleInstance();
builder.RegisterType<CommandHostVirtualPathMonitor>().As<IVirtualPathMonitor>().As<IVolatileProvider>().SingleInstance();
builder.RegisterInstance(CreateShellRegistrations()).As<IShellContainerRegistrations>();
}
private CommandHostShellContainerRegistrations CreateShellRegistrations() {
return new CommandHostShellContainerRegistrations {
Registrations = shellBuilder => {
shellBuilder.RegisterType<CommandHostVirtualPathMonitor>()
.As<IVirtualPathMonitor>()
.As<IVolatileProvider>()
.InstancePerMatchingLifetimeScope("shell");
shellBuilder.RegisterType<CommandBackgroundService>()
.As<IBackgroundService>()
.InstancePerLifetimeScope();
}
};
}
protected void MvcSingletons(ContainerBuilder builder) {
@@ -164,5 +180,9 @@ namespace Orchard.Commands {
builder.RegisterInstance(ModelMetadataProviders.Current);
builder.RegisterInstance(ViewEngines.Engines);
}
private class CommandHostShellContainerRegistrations : IShellContainerRegistrations {
public Action<ContainerBuilder> Registrations { get; set; }
}
}
}

View File

@@ -5,7 +5,7 @@ using Orchard.Environment;
using Orchard.Localization;
namespace Orchard.Commands {
public class CommandHostEnvironment : IHostEnvironment {
internal class CommandHostEnvironment : IHostEnvironment {
public CommandHostEnvironment() {
T = NullLocalizer.Instance;
}

View File

@@ -1,12 +1,16 @@
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 {
/// <summary>
/// Command line specific virtual path monitor.
/// Note that we make this class "internal" so that it's not auto-registered
/// by the Orchard Framework (it is registered explicitly by the command
/// line host).
/// </summary>
internal class CommandHostVirtualPathMonitor : IVirtualPathMonitor {
private readonly IVirtualPathProvider _virtualPathProvider;
public CommandHostVirtualPathMonitor(IVirtualPathProvider virtualPathProvider) {

View File

@@ -0,0 +1,16 @@
using System;
using Autofac;
namespace Orchard.Environment {
public interface IShellContainerRegistrations {
Action<ContainerBuilder> Registrations { get; }
}
public class ShellContainerRegistrations : IShellContainerRegistrations {
public ShellContainerRegistrations() {
Registrations = builder => { return; };
}
public Action<ContainerBuilder> Registrations { get; private set; }
}
}

View File

@@ -6,7 +6,6 @@ using System.Web.Hosting;
using Autofac;
using Autofac.Configuration;
using Orchard.Caching;
using Orchard.Data;
using Orchard.Environment.AutofacUtil;
using Orchard.Environment.Configuration;
using Orchard.Environment.Extensions;
@@ -63,6 +62,7 @@ namespace Orchard.Environment {
.As<ICompositionStrategy>()
.SingleInstance();
{
builder.RegisterType<ShellContainerRegistrations>().As<IShellContainerRegistrations>().SingleInstance();
builder.RegisterType<ExtensionLoaderCoordinator>().As<IExtensionLoaderCoordinator>().SingleInstance();
builder.RegisterType<ExtensionManager>().As<IExtensionManager>().SingleInstance();
{

View File

@@ -23,9 +23,11 @@ namespace Orchard.Environment.ShellBuilders {
public class ShellContainerFactory : IShellContainerFactory {
private readonly ILifetimeScope _lifetimeScope;
private readonly IShellContainerRegistrations _shellContainerRegistrations;
public ShellContainerFactory(ILifetimeScope lifetimeScope) {
public ShellContainerFactory(ILifetimeScope lifetimeScope, IShellContainerRegistrations shellContainerRegistrations) {
_lifetimeScope = lifetimeScope;
_shellContainerRegistrations = shellContainerRegistrations;
}
public ILifetimeScope CreateContainer(ShellSettings settings, ShellBlueprint blueprint) {
@@ -95,6 +97,9 @@ namespace Orchard.Environment.ShellBuilders {
.InjectActionInvoker();
}
// Register code-only registrations specific to a shell
_shellContainerRegistrations.Registrations(builder);
var optionalShellConfig = HostingEnvironment.MapPath("~/Config/Sites.config");
if (File.Exists(optionalShellConfig))
builder.RegisterModule(new ConfigurationSettingsReader(ConfigurationSettingsReader.DefaultSectionName, optionalShellConfig));

View File

@@ -368,6 +368,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="ContentManagement\DataMigrations\FrameworkDataMigration.cs" />
<Compile Include="Environment\IShellContainerRegistrations.cs" />
<Compile Include="FileSystems\Dependencies\DynamicModuleVirtualPathProvider.cs" />
<Compile Include="Services\IHtmlFilter.cs" />
<Compile Include="Utility\Hash.cs" />