Implementing a general scheduled task subsystem... Main top level actors include IScheduledTaskManager and SchedulingBackgroundTask. Specific needs of interfaces into subsystem expected to evolve as used from modules.

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045364
This commit is contained in:
loudej
2010-01-13 22:40:30 +00:00
parent 8817fbc020
commit 9d9f4b1669
17 changed files with 358 additions and 9 deletions

View File

@@ -58,6 +58,7 @@
<Reference Include="System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\sqlite\System.Data.SQLite.DLL</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
@@ -71,6 +72,7 @@
<ItemGroup>
<Compile Include="Common\Providers\CommonAspectProviderTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scheduling\ScheduledTaskExecutorTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Orchard.Tests.Packages\Orchard.Tests.Packages.csproj">

View File

@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using Autofac.Builder;
using Moq;
using NUnit.Framework;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;
using Orchard.Core.Scheduling;
using Orchard.Core.Scheduling.Models;
using Orchard.Core.Scheduling.Services;
using Orchard.Data;
using Orchard.Services;
using Orchard.Tasks;
using Orchard.Tests.Packages;
using Orchard.Tests.Stubs;
namespace Orchard.Core.Tests.Scheduling {
[TestFixture]
public class ScheduledTaskExecutorTests : DatabaseEnabledTestsBase {
private StubTaskHandler _handler;
private IBackgroundTask _executor;
private IRepository<ScheduledTaskRecord> _repository;
public override void Init() {
base.Init();
_repository = _container.Resolve<IRepository<ScheduledTaskRecord>>();
_executor = _container.Resolve<IBackgroundTask>("ScheduledBackgroundTask");
}
public override void Register(ContainerBuilder builder) {
_handler = new StubTaskHandler();
builder.Register(new Mock<IOrchardServices>().Object);
builder.Register<DefaultContentManager>().As<IContentManager>();
builder.Register<SchedulingBackgroundTask>().As<IBackgroundTask>().Named("ScheduledBackgroundTask");
builder.Register(_handler).As<IScheduledTaskHandler>();
}
public class StubTaskHandler : IScheduledTaskHandler {
public void Process(ScheduledTaskContext context) {
TaskContext = context;
}
public ScheduledTaskContext TaskContext { get; private set; }
}
protected override IEnumerable<Type> DatabaseTypes {
get {
return new[] {
typeof(ContentTypeRecord),
typeof(ContentItemRecord),
typeof(ContentItemVersionRecord),
typeof(ScheduledAspectRecord),
typeof(ScheduledTaskRecord),
};
}
}
[Test]
public void SweepShouldBeCallable() {
_executor.Sweep();
}
[Test]
public void RecordsForTheFutureShouldBeIgnored() {
_repository.Create(new ScheduledTaskRecord { ScheduledUtc = _clock.UtcNow.Add(TimeSpan.FromHours(2)) });
_repository.Flush();
_executor.Sweep();
_repository.Flush();
Assert.That(_repository.Count(x => true), Is.EqualTo(1));
}
[Test]
public void RecordsWhenTheyAreExecutedShouldBeDeleted() {
var task = new ScheduledTaskRecord { Action = "Ignore", ScheduledUtc = _clock.UtcNow.Add(TimeSpan.FromHours(2)) };
_repository.Create(task);
_repository.Flush();
_executor.Sweep();
_repository.Flush();
Assert.That(_repository.Count(x => x.Action == "Ignore"), Is.EqualTo(1));
_clock.Advance(TimeSpan.FromHours(3));
_repository.Flush();
_executor.Sweep();
_repository.Flush();
Assert.That(_repository.Count(x => x.Action == "Ignore"), Is.EqualTo(0));
}
[Test]
public void ScheduledTaskHandlersShouldBeCalledWhenTasksAreExecuted() {
var task = new ScheduledTaskRecord { Action = "Ignore", ScheduledUtc = _clock.UtcNow.Add(TimeSpan.FromHours(2)) };
_repository.Create(task);
_repository.Flush();
_clock.Advance(TimeSpan.FromHours(3));
Assert.That(_handler.TaskContext, Is.Null);
_executor.Sweep();
Assert.That(_handler.TaskContext, Is.Not.Null);
Assert.That(_handler.TaskContext.ScheduledTaskRecord.Action, Is.EqualTo("Ignore"));
Assert.That(_handler.TaskContext.ContentItem, Is.Null);
}
}
}

View File

@@ -78,6 +78,11 @@
<Compile Include="Common\ViewModels\BodyEditorViewModel.cs" />
<Compile Include="Common\ViewModels\OwnerEditorViewModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scheduling\Models\ScheduledAspect.cs" />
<Compile Include="Scheduling\IScheduledTaskHandler.cs" />
<Compile Include="Scheduling\Services\PublishingTaskHandler.cs" />
<Compile Include="Scheduling\Services\SchedulingBackgroundTask.cs" />
<Compile Include="Scheduling\ScheduledTaskContext.cs" />
<Compile Include="Settings\Controllers\SiteSettingsDriver.cs" />
<Compile Include="Themes\Services\AdminThemeSelector.cs" />
<Compile Include="Themes\Services\SafeModeThemeSelector.cs" />
@@ -141,6 +146,7 @@
<Content Include="Common\Views\DisplayTemplates\Parts\Common.Body.ascx" />
<Content Include="Common\Views\EditorTemplates\Parts\Common.Body.ascx" />
<Content Include="Common\Views\EditorTemplates\Parts\Common.Owner.ascx" />
<Content Include="Scheduling\Package.txt" />
<Content Include="Settings\Views\EditorTemplates\Items\Settings.Site.ascx" />
<Content Include="Themes\Theme.gif" />
<Content Include="Themes\Theme.txt" />
@@ -160,6 +166,9 @@
<Content Include="Themes\Views\menu.ascx" />
<Content Include="Themes\Views\Web.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="Scheduling\Controllers\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@@ -0,0 +1,5 @@
namespace Orchard.Core.Scheduling {
public interface IScheduledTaskHandler : IDependency {
void Process(ScheduledTaskContext context);
}
}

View File

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.ContentManagement.Records;
namespace Orchard.Core.Scheduling.Models {
public class ScheduledAspect : ContentPart<ScheduledAspectRecord>, IScheduledAspect {
public IScheduledTask Tasks {
get { throw new NotImplementedException(); }
}
}
public class ScheduledAspectRecord : ContentPartVersionRecord {
public ScheduledAspectRecord() {
Tasks = new List<ScheduledTaskRecord>();
}
public virtual IList<ScheduledTaskRecord> Tasks { get; set; }
}
public class ScheduledTaskRecord {
public virtual int Id { get; set; }
public virtual ScheduledAspectRecord ScheduledAspectRecord { get; set; }
public virtual string Action { get; set; }
public virtual DateTime? ScheduledUtc { get; set; }
}
}

View File

@@ -0,0 +1 @@
name: Scheduling

View File

@@ -0,0 +1,9 @@
using Orchard.ContentManagement;
using Orchard.Core.Scheduling.Models;
namespace Orchard.Core.Scheduling {
public class ScheduledTaskContext {
public ScheduledTaskRecord ScheduledTaskRecord { get; set; }
public ContentItem ContentItem { get; set; }
}
}

View File

@@ -0,0 +1,32 @@
using Orchard.Logging;
namespace Orchard.Core.Scheduling.Services {
public class PublishingTaskHandler : IScheduledTaskHandler {
public PublishingTaskHandler(IOrchardServices services) {
Services = services;
Logger = NullLogger.Instance;
}
public IOrchardServices Services { get; set; }
public ILogger Logger { get; set; }
public void Process(ScheduledTaskContext context) {
if (context.ScheduledTaskRecord.Action == "Publish") {
Logger.Information("Publishing item #{0} version {1} scheduled at {2} utc",
context.ContentItem.Id,
context.ContentItem.Version,
context.ScheduledTaskRecord.ScheduledUtc);
Services.ContentManager.Publish(context.ContentItem);
}
else if (context.ScheduledTaskRecord.Action == "Unpublish") {
Logger.Information("Unpublishing item #{0} version {1} scheduled at {2} utc",
context.ContentItem.Id,
context.ContentItem.Version,
context.ScheduledTaskRecord.ScheduledUtc);
Services.ContentManager.Unpublish(context.ContentItem);
}
}
}
}

View File

@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement;
using Orchard.Core.Scheduling.Models;
using Orchard.Data;
using Orchard.Logging;
using Orchard.Utility;
namespace Orchard.Core.Scheduling.Services {
public interface IScheduledTaskManager : IDependency {
void CreateTask(string action, DateTime scheduledUtc, ContentItem contentItem);
IEnumerable<ScheduledTaskRecord> GetTasks(ContentItem contentItem);
}
public class ScheduledTaskManager : IScheduledTaskManager {
private readonly IRepository<ScheduledTaskRecord> _repository;
public ScheduledTaskManager(
IOrchardServices services,
IRepository<ScheduledTaskRecord> repository) {
_repository = repository;
Services = services;
Logger = NullLogger.Instance;
}
public IOrchardServices Services { get; set; }
public ILogger Logger { get; set; }
public void CreateTask(string action, DateTime scheduledUtc, ContentItem contentItem) {
var taskRecord = new ScheduledTaskRecord {
Action = action,
ScheduledUtc = scheduledUtc,
};
if (contentItem != null) {
var part = contentItem.Get<ContentPart<ScheduledAspectRecord>>();
if (part != null) {
taskRecord.ScheduledAspectRecord = part.Record;
}
}
_repository.Create(taskRecord);
}
public IEnumerable<ScheduledTaskRecord> GetTasks(ContentItem contentItem) {
return _repository
.Fetch(x => x.ScheduledAspectRecord.ContentItemRecord == contentItem.Record)
.ToReadOnlyCollection();
}
}
}

View File

@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.Core.Scheduling.Models;
using Orchard.Data;
using Orchard.Logging;
using Orchard.Services;
using Orchard.Tasks;
namespace Orchard.Core.Scheduling.Services {
[UsedImplicitly]
public class SchedulingBackgroundTask : IBackgroundTask {
private readonly IClock _clock;
private readonly IRepository<ScheduledTaskRecord> _repository;
private readonly IEnumerable<IScheduledTaskHandler> _handlers;
public SchedulingBackgroundTask(
IOrchardServices services,
IClock clock,
IRepository<ScheduledTaskRecord> repository,
IEnumerable<IScheduledTaskHandler> handlers) {
_clock = clock;
_repository = repository;
_handlers = handlers;
Services = services;
Logger = NullLogger.Instance;
}
public IOrchardServices Services { get; set; }
public ILogger Logger { get; set; }
public void Sweep() {
var taskEntries = _repository.Fetch(x => x.ScheduledUtc <= _clock.UtcNow)
.Select(x => new { x.Id, x.Action })
.ToArray();
foreach (var taskEntry in taskEntries) {
//TODO: start a dedicated transaction scope
try {
// fetch the task
var context = new ScheduledTaskContext {
ScheduledTaskRecord = _repository.Get(taskEntry.Id)
};
// another node in the farm has performed this work before us
if (context.ScheduledTaskRecord == null) {
continue;
}
// removing record first helps avoid concurrent execution
_repository.Delete(context.ScheduledTaskRecord);
// if it's associaged with a version of a content item
if (context.ScheduledTaskRecord.ScheduledAspectRecord != null) {
var versionRecord = context.ScheduledTaskRecord.ScheduledAspectRecord.ContentItemVersionRecord;
// hydrate that item as part of the task context
context.ContentItem = Services.ContentManager.Get(
versionRecord.ContentItemRecord.Id,
VersionOptions.VersionRecord(versionRecord.Id));
}
// dispatch to standard or custom handlers
foreach(var handler in _handlers) {
handler.Process(context);
}
//TODO: commit dedicated scope
}
catch (Exception ex) {
Logger.Warning(ex, "Unable to process scheduled task #{0} of type {1}", taskEntry.Id, taskEntry.Action);
//TODO: handle exception to rollback dedicated xact, and re-delete task record.
// does this also need some retry logic?
}
}
}
}
}

View File

@@ -5,16 +5,19 @@ Model.Zones.AddRenderPartial("header", "header", Model);
Model.Zones.AddRenderPartial("header:after", "user", Model);
Model.Zones.AddRenderPartial("menu", "menu", Model);
Model.Zones.AddRenderPartial("content:before", "messages", Model.Messages);
Model.Zones.AddAction("content", html=>html.ViewContext.Writer.Write());
%>
<div class="page">
<div id="header"><%
Html.Zone("header");
<div id="header"><%Html.Zone("header");%>.ToHtmlString();
Html.Zone("header").Render();
Html.Zone("menu"); %>
</div>
<div id="main"><%
Html.ZoneBody("content"); %>
Html.Zone("content"); %>
<div id="footer"><%
Html.Zone("footer");
Html.Zone("footer", ()=>Html.RenderPartial("footer", Model));
%></div>
</div>
</div>

View File

@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Orchard.Security;
namespace Orchard.ContentManagement.Aspects {

View File

@@ -0,0 +1,12 @@
using System;
namespace Orchard.ContentManagement.Aspects {
public interface IScheduledAspect : IContent {
IScheduledTask Tasks { get; }
}
public interface IScheduledTask : IContent {
string Action { get; set; }
DateTime? ScheduledUtc { get; set; }
}
}

View File

@@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Orchard.ContentManagement.Records;
namespace Orchard.Data {
public interface IRepository<T> {
@@ -10,6 +9,7 @@ namespace Orchard.Data {
void Update(T entity);
void Delete(T entity);
void Copy(T source, T target);
void Flush();
T Get(int id);
T Get(Expression<Func<T, bool>> predicate);

View File

@@ -48,6 +48,10 @@ namespace Orchard.Data {
Copy(source, target);
}
void IRepository<T>.Flush() {
Flush();
}
T IRepository<T>.Get(int id) {
return Get(id);
}
@@ -126,6 +130,9 @@ namespace Orchard.Data {
metadata.SetPropertyValues(target, values, EntityMode.Poco);
}
public virtual void Flush() {
Session.Flush();
}
public virtual int Count(Expression<Func<T, bool>> predicate) {
return Fetch(predicate).Count();

View File

@@ -7,6 +7,7 @@ using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using Orchard.Services;
using Orchard.Settings;
using Orchard.Utility;
@@ -96,7 +97,7 @@ namespace Orchard.Mvc.Html {
//TODO: (erikpo) This method needs localized
public static string DateTimeRelative(this HtmlHelper htmlHelper, DateTime value) {
TimeSpan time = System.DateTime.UtcNow - value;
TimeSpan time = htmlHelper.Resolve<IClock>().UtcNow - value;
if (time.TotalDays > 7)
return "at " + htmlHelper.DateTime(value);

View File

@@ -127,6 +127,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ContentManagement\Aspects\ICommonAspect.cs" />
<Compile Include="ContentManagement\Aspects\IScheduledAspect.cs" />
<Compile Include="ContentManagement\ContentExtensions.cs" />
<Compile Include="ContentManagement\ContentItem.cs" />
<Compile Include="ContentManagement\ContentItemMetadata.cs" />