mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
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:
@@ -5,6 +5,7 @@ using Autofac.Features.OwnedInstances;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Mvc.ModelBinders;
|
||||
using Orchard.Mvc.Routes;
|
||||
using Orchard.Tasks;
|
||||
using IModelBinderProvider = Orchard.Mvc.ModelBinders.IModelBinderProvider;
|
||||
|
||||
namespace Orchard.Environment {
|
||||
@@ -14,18 +15,21 @@ namespace Orchard.Environment {
|
||||
private readonly IRoutePublisher _routePublisher;
|
||||
private readonly IEnumerable<IModelBinderProvider> _modelBinderProviders;
|
||||
private readonly IModelBinderPublisher _modelBinderPublisher;
|
||||
private readonly ISweepGenerator _sweepGenerator;
|
||||
|
||||
public DefaultOrchardShell(
|
||||
Func<Owned<IOrchardShellEvents>> eventsFactory,
|
||||
IEnumerable<IRouteProvider> routeProviders,
|
||||
IRoutePublisher routePublisher,
|
||||
IEnumerable<IModelBinderProvider> modelBinderProviders,
|
||||
IModelBinderPublisher modelBinderPublisher) {
|
||||
IModelBinderPublisher modelBinderPublisher,
|
||||
ISweepGenerator sweepGenerator) {
|
||||
_eventsFactory = eventsFactory;
|
||||
_routeProviders = routeProviders;
|
||||
_routePublisher = routePublisher;
|
||||
_modelBinderProviders = modelBinderProviders;
|
||||
_modelBinderPublisher = modelBinderPublisher;
|
||||
_sweepGenerator = sweepGenerator;
|
||||
|
||||
Logger = NullLogger.Instance;
|
||||
}
|
||||
@@ -36,19 +40,23 @@ namespace Orchard.Environment {
|
||||
_routePublisher.Publish(_routeProviders.SelectMany(provider => provider.GetRoutes()));
|
||||
_modelBinderPublisher.Publish(_modelBinderProviders.SelectMany(provider => provider.GetModelBinders()));
|
||||
|
||||
_sweepGenerator.Activate();
|
||||
|
||||
using (var events = _eventsFactory()) {
|
||||
events.Value.Activated();
|
||||
}
|
||||
}
|
||||
|
||||
public void Terminate() {
|
||||
try {
|
||||
using (var events = _eventsFactory()) {
|
||||
using (var events = _eventsFactory()) {
|
||||
try {
|
||||
events.Value.Terminating();
|
||||
}
|
||||
}
|
||||
catch {
|
||||
// ignore exceptions while terminating the application
|
||||
catch {
|
||||
// ignore exceptions while terminating the application
|
||||
}
|
||||
|
||||
_sweepGenerator.Terminate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,11 +1,16 @@
|
||||
using System;
|
||||
using System.Timers;
|
||||
using Orchard.Data;
|
||||
using Orchard.Environment;
|
||||
using Orchard.Logging;
|
||||
|
||||
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 Timer _timer;
|
||||
|
||||
@@ -24,13 +29,13 @@ namespace Orchard.Tasks {
|
||||
set { _timer.Interval = value.TotalMilliseconds; }
|
||||
}
|
||||
|
||||
public void Activated() {
|
||||
public void Activate() {
|
||||
lock (_timer) {
|
||||
_timer.Start();
|
||||
}
|
||||
}
|
||||
|
||||
public void Terminating() {
|
||||
public void Terminate() {
|
||||
lock (_timer) {
|
||||
_timer.Stop();
|
||||
}
|
||||
@@ -56,10 +61,17 @@ namespace Orchard.Tasks {
|
||||
|
||||
public void DoWork() {
|
||||
using (var scope = _workContextAccessor.CreateWorkContextScope()) {
|
||||
var transactionManager = scope.Resolve<ITransactionManager>();
|
||||
transactionManager.Demand();
|
||||
|
||||
// resolve the manager and invoke it
|
||||
var manager = scope.Resolve<IBackgroundService>();
|
||||
manager.Sweep();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
_timer.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user