From 7eb29671413234b14f9c877e9062dab0c4e30505 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Wed, 14 Sep 2011 17:13:33 -0700 Subject: [PATCH] #17868: Isolating background tasks Work Items: 17868 --HG-- branch : 1.x --- src/Orchard/Tasks/BackgroundService.cs | 22 +++++++++++++++++++--- src/Orchard/Tasks/SweepGenerator.cs | 26 ++++---------------------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/Orchard/Tasks/BackgroundService.cs b/src/Orchard/Tasks/BackgroundService.cs index ee76fbca7..c3e08beaa 100644 --- a/src/Orchard/Tasks/BackgroundService.cs +++ b/src/Orchard/Tasks/BackgroundService.cs @@ -1,4 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Transactions; using JetBrains.Annotations; +using Orchard.Data; +using Orchard.Events; using Orchard.Logging; namespace Orchard.Tasks { @@ -9,9 +15,9 @@ namespace Orchard.Tasks { [UsedImplicitly] public class BackgroundService : IBackgroundService { - private readonly IBackgroundTask _tasks; + private readonly IEnumerable _tasks; - public BackgroundService(IBackgroundTask tasks) { + public BackgroundService(IEnumerable tasks) { _tasks = tasks; Logger = NullLogger.Instance; } @@ -19,7 +25,17 @@ namespace Orchard.Tasks { public ILogger Logger { get; set; } public void Sweep() { - _tasks.Sweep(); + foreach(var task in _tasks.OfType()) { + using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew)) { + try { + task.Sweep(); + scope.Complete(); + } + catch (Exception e) { + Logger.Error(e, "Error while processing background task"); + } + } + } } } } diff --git a/src/Orchard/Tasks/SweepGenerator.cs b/src/Orchard/Tasks/SweepGenerator.cs index a48d568ef..b6dacb231 100644 --- a/src/Orchard/Tasks/SweepGenerator.cs +++ b/src/Orchard/Tasks/SweepGenerator.cs @@ -5,7 +5,7 @@ using Orchard.Environment; using Orchard.Logging; namespace Orchard.Tasks { - public class SweepGenerator : IOrchardShellEvents, IDisposable { + public class SweepGenerator : IOrchardShellEvents { private readonly IWorkContextAccessor _workContextAccessor; private readonly Timer _timer; @@ -55,29 +55,11 @@ namespace Orchard.Tasks { } public void DoWork() { - // makes an inner container, similar to the per-request container using (var scope = _workContextAccessor.CreateWorkContextScope()) { - var transactionManager = scope.Resolve(); - - try { - // resolve the manager and invoke it - var manager = scope.Resolve(); - manager.Sweep(); - } - catch { - // any database changes in this using scope are invalidated - transactionManager.Cancel(); - - // pass exception along to actual handler - throw; - } + // resolve the manager and invoke it + var manager = scope.Resolve(); + manager.Sweep(); } } - - public void Dispose() - { - _timer.Elapsed -= Elapsed; - _timer.Dispose(); - } } }