Refactoring SweepGenerator

- Removed IOrchardShellEvents base interface to prevent every request from
instanciating it (as it would be an event)
- Call ITransactionManager.Demand() before executing tasks or nested
transaction scopes with RequiresNew would fail

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2012-02-06 12:53:32 -08:00
parent 5189ca3969
commit 18ff368ac5
2 changed files with 30 additions and 10 deletions
+14 -6
View File
@@ -5,6 +5,7 @@ using Autofac.Features.OwnedInstances;
using Orchard.Logging; using Orchard.Logging;
using Orchard.Mvc.ModelBinders; using Orchard.Mvc.ModelBinders;
using Orchard.Mvc.Routes; using Orchard.Mvc.Routes;
using Orchard.Tasks;
using IModelBinderProvider = Orchard.Mvc.ModelBinders.IModelBinderProvider; using IModelBinderProvider = Orchard.Mvc.ModelBinders.IModelBinderProvider;
namespace Orchard.Environment { namespace Orchard.Environment {
@@ -14,18 +15,21 @@ namespace Orchard.Environment {
private readonly IRoutePublisher _routePublisher; private readonly IRoutePublisher _routePublisher;
private readonly IEnumerable<IModelBinderProvider> _modelBinderProviders; private readonly IEnumerable<IModelBinderProvider> _modelBinderProviders;
private readonly IModelBinderPublisher _modelBinderPublisher; private readonly IModelBinderPublisher _modelBinderPublisher;
private readonly ISweepGenerator _sweepGenerator;
public DefaultOrchardShell( public DefaultOrchardShell(
Func<Owned<IOrchardShellEvents>> eventsFactory, Func<Owned<IOrchardShellEvents>> eventsFactory,
IEnumerable<IRouteProvider> routeProviders, IEnumerable<IRouteProvider> routeProviders,
IRoutePublisher routePublisher, IRoutePublisher routePublisher,
IEnumerable<IModelBinderProvider> modelBinderProviders, IEnumerable<IModelBinderProvider> modelBinderProviders,
IModelBinderPublisher modelBinderPublisher) { IModelBinderPublisher modelBinderPublisher,
ISweepGenerator sweepGenerator) {
_eventsFactory = eventsFactory; _eventsFactory = eventsFactory;
_routeProviders = routeProviders; _routeProviders = routeProviders;
_routePublisher = routePublisher; _routePublisher = routePublisher;
_modelBinderProviders = modelBinderProviders; _modelBinderProviders = modelBinderProviders;
_modelBinderPublisher = modelBinderPublisher; _modelBinderPublisher = modelBinderPublisher;
_sweepGenerator = sweepGenerator;
Logger = NullLogger.Instance; Logger = NullLogger.Instance;
} }
@@ -36,19 +40,23 @@ namespace Orchard.Environment {
_routePublisher.Publish(_routeProviders.SelectMany(provider => provider.GetRoutes())); _routePublisher.Publish(_routeProviders.SelectMany(provider => provider.GetRoutes()));
_modelBinderPublisher.Publish(_modelBinderProviders.SelectMany(provider => provider.GetModelBinders())); _modelBinderPublisher.Publish(_modelBinderProviders.SelectMany(provider => provider.GetModelBinders()));
_sweepGenerator.Activate();
using (var events = _eventsFactory()) { using (var events = _eventsFactory()) {
events.Value.Activated(); events.Value.Activated();
} }
} }
public void Terminate() { public void Terminate() {
try { using (var events = _eventsFactory()) {
using (var events = _eventsFactory()) { try {
events.Value.Terminating(); events.Value.Terminating();
} }
} catch {
catch { // ignore exceptions while terminating the application
// ignore exceptions while terminating the application }
_sweepGenerator.Terminate();
} }
} }
} }
+16 -4
View File
@@ -1,11 +1,16 @@
using System; using System;
using System.Timers; using System.Timers;
using Orchard.Data; using Orchard.Data;
using Orchard.Environment;
using Orchard.Logging; using Orchard.Logging;
namespace Orchard.Tasks { namespace Orchard.Tasks {
public class SweepGenerator : IOrchardShellEvents {
public interface ISweepGenerator : ISingletonDependency {
void Activate();
void Terminate();
}
public class SweepGenerator : ISweepGenerator, IDisposable {
private readonly IWorkContextAccessor _workContextAccessor; private readonly IWorkContextAccessor _workContextAccessor;
private readonly Timer _timer; private readonly Timer _timer;
@@ -24,13 +29,13 @@ namespace Orchard.Tasks {
set { _timer.Interval = value.TotalMilliseconds; } set { _timer.Interval = value.TotalMilliseconds; }
} }
public void Activated() { public void Activate() {
lock (_timer) { lock (_timer) {
_timer.Start(); _timer.Start();
} }
} }
public void Terminating() { public void Terminate() {
lock (_timer) { lock (_timer) {
_timer.Stop(); _timer.Stop();
} }
@@ -56,10 +61,17 @@ namespace Orchard.Tasks {
public void DoWork() { public void DoWork() {
using (var scope = _workContextAccessor.CreateWorkContextScope()) { using (var scope = _workContextAccessor.CreateWorkContextScope()) {
var transactionManager = scope.Resolve<ITransactionManager>();
transactionManager.Demand();
// resolve the manager and invoke it // resolve the manager and invoke it
var manager = scope.Resolve<IBackgroundService>(); var manager = scope.Resolve<IBackgroundService>();
manager.Sweep(); manager.Sweep();
} }
} }
public void Dispose() {
_timer.Dispose();
}
} }
} }