mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-23 04:43:35 +08:00
Refactoring scheduling implementation. Removing scheduled aspect. Moving interfaces into Orchard.Tasks.Scheduling namespace.
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045426
This commit is contained in:
@@ -72,6 +72,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Common\Providers\CommonAspectProviderTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Scheduling\ScheduledTaskManagerTests.cs" />
|
||||
<Compile Include="Scheduling\ScheduledTaskExecutorTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
@@ -5,14 +5,12 @@ 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.Records;
|
||||
using Orchard.Core.Scheduling.Services;
|
||||
using Orchard.Data;
|
||||
using Orchard.Services;
|
||||
using Orchard.Tasks;
|
||||
using Orchard.Tasks.Scheduling;
|
||||
using Orchard.Tests.Packages;
|
||||
using Orchard.Tests.Stubs;
|
||||
|
||||
namespace Orchard.Core.Tests.Scheduling {
|
||||
[TestFixture]
|
||||
@@ -24,16 +22,27 @@ namespace Orchard.Core.Tests.Scheduling {
|
||||
public override void Init() {
|
||||
base.Init();
|
||||
_repository = _container.Resolve<IRepository<ScheduledTaskRecord>>();
|
||||
_executor = _container.Resolve<IBackgroundTask>("ScheduledBackgroundTask");
|
||||
_executor = _container.Resolve<IBackgroundTask>("ScheduledTaskExecutor");
|
||||
}
|
||||
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<ScheduledTaskExecutor>().As<IBackgroundTask>().Named("ScheduledTaskExecutor");
|
||||
builder.Register(_handler).As<IScheduledTaskHandler>();
|
||||
}
|
||||
|
||||
protected override IEnumerable<Type> DatabaseTypes {
|
||||
get {
|
||||
return new[] {
|
||||
typeof(ContentTypeRecord),
|
||||
typeof(ContentItemRecord),
|
||||
typeof(ContentItemVersionRecord),
|
||||
typeof(ScheduledTaskRecord),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class StubTaskHandler : IScheduledTaskHandler {
|
||||
public void Process(ScheduledTaskContext context) {
|
||||
TaskContext = context;
|
||||
@@ -42,17 +51,6 @@ namespace Orchard.Core.Tests.Scheduling {
|
||||
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() {
|
||||
@@ -71,14 +69,14 @@ namespace Orchard.Core.Tests.Scheduling {
|
||||
|
||||
[Test]
|
||||
public void RecordsWhenTheyAreExecutedShouldBeDeleted() {
|
||||
var task = new ScheduledTaskRecord { Action = "Ignore", ScheduledUtc = _clock.UtcNow.Add(TimeSpan.FromHours(2)) };
|
||||
var task = new ScheduledTaskRecord { TaskType = "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));
|
||||
Assert.That(_repository.Count(x => x.TaskType == "Ignore"), Is.EqualTo(1));
|
||||
|
||||
_clock.Advance(TimeSpan.FromHours(3));
|
||||
|
||||
@@ -86,12 +84,12 @@ namespace Orchard.Core.Tests.Scheduling {
|
||||
_executor.Sweep();
|
||||
|
||||
_repository.Flush();
|
||||
Assert.That(_repository.Count(x => x.Action == "Ignore"), Is.EqualTo(0));
|
||||
Assert.That(_repository.Count(x => x.TaskType == "Ignore"), Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ScheduledTaskHandlersShouldBeCalledWhenTasksAreExecuted() {
|
||||
var task = new ScheduledTaskRecord { Action = "Ignore", ScheduledUtc = _clock.UtcNow.Add(TimeSpan.FromHours(2)) };
|
||||
var task = new ScheduledTaskRecord { TaskType = "Ignore", ScheduledUtc = _clock.UtcNow.Add(TimeSpan.FromHours(2)) };
|
||||
_repository.Create(task);
|
||||
|
||||
_repository.Flush();
|
||||
@@ -101,8 +99,8 @@ namespace Orchard.Core.Tests.Scheduling {
|
||||
_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);
|
||||
Assert.That(_handler.TaskContext.Task.TaskType, Is.EqualTo("Ignore"));
|
||||
Assert.That(_handler.TaskContext.Task.ContentItem, Is.Null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
114
src/Orchard.Core.Tests/Scheduling/ScheduledTaskManagerTests.cs
Normal file
114
src/Orchard.Core.Tests/Scheduling/ScheduledTaskManagerTests.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Autofac.Builder;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Records;
|
||||
using Orchard.Core.Scheduling.Records;
|
||||
using Orchard.Core.Scheduling.Services;
|
||||
using Orchard.Data;
|
||||
using Orchard.Tasks;
|
||||
using Orchard.Tasks.Scheduling;
|
||||
using Orchard.Tests.Packages;
|
||||
|
||||
namespace Orchard.Core.Tests.Scheduling {
|
||||
[TestFixture]
|
||||
public class ScheduledTaskManagerTests : DatabaseEnabledTestsBase {
|
||||
private IRepository<ScheduledTaskRecord> _repository;
|
||||
private IScheduledTaskManager _scheduledTaskManager;
|
||||
private IContentManager _contentManager;
|
||||
private Mock<IOrchardServices> _mockServices;
|
||||
|
||||
public override void Init() {
|
||||
_mockServices = new Mock<IOrchardServices>();
|
||||
base.Init();
|
||||
_repository = _container.Resolve<IRepository<ScheduledTaskRecord>>();
|
||||
_scheduledTaskManager = _container.Resolve<IScheduledTaskManager>();
|
||||
_contentManager = _container.Resolve<IContentManager>();
|
||||
_mockServices.SetupGet(x=>x.ContentManager).Returns(_contentManager);
|
||||
}
|
||||
|
||||
public override void Register(ContainerBuilder builder) {
|
||||
builder.Register(_mockServices.Object);
|
||||
builder.Register<DefaultContentManager>().As<IContentManager>();
|
||||
builder.Register<ScheduledTaskManager>().As<IScheduledTaskManager>();
|
||||
}
|
||||
|
||||
protected override IEnumerable<Type> DatabaseTypes {
|
||||
get {
|
||||
return new[] {
|
||||
typeof(ContentTypeRecord),
|
||||
typeof(ContentItemRecord),
|
||||
typeof(ContentItemVersionRecord),
|
||||
typeof(ScheduledTaskRecord),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestFixtureShouldBeAbleToCreateContentItem() {
|
||||
var hello = _contentManager.New("hello");
|
||||
_contentManager.Create(hello);
|
||||
_session.Flush();
|
||||
_session.Clear();
|
||||
|
||||
var hello2 = _contentManager.Get(hello.Id);
|
||||
Assert.That(hello2, Is.Not.Null);
|
||||
Assert.That(hello2.Record, Is.Not.SameAs(hello.Record));
|
||||
Assert.That(hello2.Id, Is.EqualTo(hello.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TaskManagerShouldCreateTaskRecordsWithOrWithoutContentItem() {
|
||||
var hello = _contentManager.New("hello");
|
||||
_contentManager.Create(hello);
|
||||
|
||||
_scheduledTaskManager.CreateTask("Ignore", _clock.UtcNow.AddHours(1), null);
|
||||
_scheduledTaskManager.CreateTask("Ignore", _clock.UtcNow.AddHours(2), hello);
|
||||
_session.Flush();
|
||||
_session.Clear();
|
||||
|
||||
var tasks = _repository.Fetch(x => true);
|
||||
Assert.That(tasks.Count(), Is.EqualTo(2));
|
||||
Assert.That(tasks, Has.All.Property("TaskType").EqualTo("Ignore"));
|
||||
|
||||
var noContentItemTask = tasks.Single(x => x.ContentItemVersionRecord == null);
|
||||
Assert.That(noContentItemTask.ScheduledUtc, Is.EqualTo(_clock.UtcNow.AddHours(1)));
|
||||
Assert.That(noContentItemTask.ContentItemVersionRecord, Is.Null);
|
||||
|
||||
var hasContentItemTask = tasks.Single(x => x.ContentItemVersionRecord != null);
|
||||
Assert.That(hasContentItemTask.ContentItemVersionRecord.ContentItemRecord.Id, Is.EqualTo(hello.Id));
|
||||
Assert.That(hasContentItemTask.ContentItemVersionRecord.Id, Is.EqualTo(hello.VersionRecord.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TasksForAllVersionsOfContenItemShouldBeReturned() {
|
||||
var hello1 = _contentManager.New("hello");
|
||||
_contentManager.Create(hello1);
|
||||
|
||||
var hello2 = _contentManager.GetDraftRequired(hello1.Id);
|
||||
|
||||
Assert.That(hello1.Version, Is.EqualTo(1));
|
||||
Assert.That(hello2.Version, Is.EqualTo(2));
|
||||
|
||||
_scheduledTaskManager.CreateTask("First", _clock.UtcNow.AddHours(1), hello1);
|
||||
_scheduledTaskManager.CreateTask("Second", _clock.UtcNow.AddHours(2), hello2);
|
||||
_scheduledTaskManager.CreateTask("Third", _clock.UtcNow.AddHours(3), null);
|
||||
|
||||
_session.Flush();
|
||||
_session.Clear();
|
||||
|
||||
var hello = _contentManager.Get(hello1.Id);
|
||||
var tasks = _scheduledTaskManager.GetTasks(hello);
|
||||
Assert.That(tasks.Count(), Is.EqualTo(2));
|
||||
|
||||
var firstTask = tasks.Single(x => x.TaskType == "First");
|
||||
Assert.That(firstTask.ContentItem.Version, Is.EqualTo(1));
|
||||
|
||||
var secondTask = tasks.Single(x => x.TaskType == "Second");
|
||||
Assert.That(secondTask.ContentItem.Version, Is.EqualTo(2));
|
||||
}
|
||||
}
|
||||
}
|
@@ -82,11 +82,11 @@
|
||||
<Compile Include="Common\ViewModels\RoutableEditorViewModel.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\Records\ScheduledTaskRecord.cs" />
|
||||
<Compile Include="Scheduling\Services\PublishingTaskHandler.cs" />
|
||||
<Compile Include="Scheduling\Services\SchedulingBackgroundTask.cs" />
|
||||
<Compile Include="Scheduling\ScheduledTaskContext.cs" />
|
||||
<Compile Include="Scheduling\Services\ScheduledTaskManager.cs" />
|
||||
<Compile Include="Scheduling\Services\ScheduledTaskExecutor.cs" />
|
||||
<Compile Include="Scheduling\Models\Task.cs" />
|
||||
<Compile Include="Settings\Controllers\SiteSettingsDriver.cs" />
|
||||
<Compile Include="Themes\Services\AdminThemeSelector.cs" />
|
||||
<Compile Include="Themes\Services\SafeModeThemeSelector.cs" />
|
||||
|
@@ -1,28 +0,0 @@
|
||||
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; }
|
||||
}
|
||||
}
|
42
src/Orchard.Web/Core/Scheduling/Models/Task.cs
Normal file
42
src/Orchard.Web/Core/Scheduling/Models/Task.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Aspects;
|
||||
using Orchard.Core.Scheduling.Records;
|
||||
|
||||
namespace Orchard.Core.Scheduling.Models {
|
||||
public class Task : IScheduledTask {
|
||||
private readonly IContentManager _contentManager;
|
||||
private readonly ScheduledTaskRecord _record;
|
||||
private ContentItem _item;
|
||||
private bool _itemInitialized;
|
||||
|
||||
public Task(IContentManager contentManager, ScheduledTaskRecord record) {
|
||||
// in spite of appearances, this is actually a created class, not IoC,
|
||||
// but dependencies are passed in for lazy initialization purposes
|
||||
_contentManager = contentManager;
|
||||
_record = record;
|
||||
}
|
||||
|
||||
public string TaskType {
|
||||
get { return _record.TaskType; }
|
||||
}
|
||||
|
||||
public DateTime? ScheduledUtc {
|
||||
get { return _record.ScheduledUtc; }
|
||||
}
|
||||
|
||||
public ContentItem ContentItem {
|
||||
get {
|
||||
if (!_itemInitialized) {
|
||||
if (_record.ContentItemVersionRecord != null) {
|
||||
_item = _contentManager.Get(
|
||||
_record.ContentItemVersionRecord.ContentItemRecord.Id,
|
||||
VersionOptions.VersionRecord(_record.ContentItemVersionRecord.Id));
|
||||
}
|
||||
_itemInitialized = true;
|
||||
}
|
||||
return _item;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using Orchard.ContentManagement.Records;
|
||||
|
||||
namespace Orchard.Core.Scheduling.Records {
|
||||
public class ScheduledTaskRecord {
|
||||
public virtual int Id { get; set; }
|
||||
public virtual string TaskType { get; set; }
|
||||
public virtual DateTime? ScheduledUtc { get; set; }
|
||||
public virtual ContentItemVersionRecord ContentItemVersionRecord { get; set; }
|
||||
}
|
||||
}
|
@@ -1,9 +0,0 @@
|
||||
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; }
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
using Orchard.Logging;
|
||||
using Orchard.Tasks.Scheduling;
|
||||
|
||||
namespace Orchard.Core.Scheduling.Services {
|
||||
public class PublishingTaskHandler : IScheduledTaskHandler {
|
||||
@@ -11,21 +12,21 @@ namespace Orchard.Core.Scheduling.Services {
|
||||
public ILogger Logger { get; set; }
|
||||
|
||||
public void Process(ScheduledTaskContext context) {
|
||||
if (context.ScheduledTaskRecord.Action == "Publish") {
|
||||
if (context.Task.TaskType == "Publish") {
|
||||
Logger.Information("Publishing item #{0} version {1} scheduled at {2} utc",
|
||||
context.ContentItem.Id,
|
||||
context.ContentItem.Version,
|
||||
context.ScheduledTaskRecord.ScheduledUtc);
|
||||
context.Task.ContentItem.Id,
|
||||
context.Task.ContentItem.Version,
|
||||
context.Task.ScheduledUtc);
|
||||
|
||||
Services.ContentManager.Publish(context.ContentItem);
|
||||
Services.ContentManager.Publish(context.Task.ContentItem);
|
||||
}
|
||||
else if (context.ScheduledTaskRecord.Action == "Unpublish") {
|
||||
else if (context.Task.TaskType == "Unpublish") {
|
||||
Logger.Information("Unpublishing item #{0} version {1} scheduled at {2} utc",
|
||||
context.ContentItem.Id,
|
||||
context.ContentItem.Version,
|
||||
context.ScheduledTaskRecord.ScheduledUtc);
|
||||
context.Task.ContentItem.Id,
|
||||
context.Task.ContentItem.Version,
|
||||
context.Task.ScheduledUtc);
|
||||
|
||||
Services.ContentManager.Unpublish(context.ContentItem);
|
||||
Services.ContentManager.Unpublish(context.Task.ContentItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -4,19 +4,21 @@ using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Core.Scheduling.Models;
|
||||
using Orchard.Core.Scheduling.Records;
|
||||
using Orchard.Data;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Services;
|
||||
using Orchard.Tasks;
|
||||
using Orchard.Tasks.Scheduling;
|
||||
|
||||
namespace Orchard.Core.Scheduling.Services {
|
||||
[UsedImplicitly]
|
||||
public class SchedulingBackgroundTask : IBackgroundTask {
|
||||
public class ScheduledTaskExecutor : IBackgroundTask {
|
||||
private readonly IClock _clock;
|
||||
private readonly IRepository<ScheduledTaskRecord> _repository;
|
||||
private readonly IEnumerable<IScheduledTaskHandler> _handlers;
|
||||
|
||||
public SchedulingBackgroundTask(
|
||||
public ScheduledTaskExecutor(
|
||||
IOrchardServices services,
|
||||
IClock clock,
|
||||
IRepository<ScheduledTaskRecord> repository,
|
||||
@@ -33,7 +35,7 @@ namespace Orchard.Core.Scheduling.Services {
|
||||
|
||||
public void Sweep() {
|
||||
var taskEntries = _repository.Fetch(x => x.ScheduledUtc <= _clock.UtcNow)
|
||||
.Select(x => new { x.Id, x.Action })
|
||||
.Select(x => new { x.Id, Action = x.TaskType })
|
||||
.ToArray();
|
||||
|
||||
foreach (var taskEntry in taskEntries) {
|
||||
@@ -41,30 +43,22 @@ namespace Orchard.Core.Scheduling.Services {
|
||||
|
||||
try {
|
||||
// fetch the task
|
||||
var context = new ScheduledTaskContext {
|
||||
ScheduledTaskRecord = _repository.Get(taskEntry.Id)
|
||||
};
|
||||
var taskRecord = _repository.Get(taskEntry.Id);
|
||||
|
||||
// another node in the farm has performed this work before us
|
||||
if (context.ScheduledTaskRecord == null) {
|
||||
// another server or thread has performed this work before us
|
||||
if (taskRecord == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// removing record first helps avoid concurrent execution
|
||||
_repository.Delete(context.ScheduledTaskRecord);
|
||||
_repository.Delete(taskRecord);
|
||||
|
||||
// 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));
|
||||
}
|
||||
var context = new ScheduledTaskContext {
|
||||
Task = new Task(Services.ContentManager, taskRecord)
|
||||
};
|
||||
|
||||
// dispatch to standard or custom handlers
|
||||
foreach(var handler in _handlers) {
|
||||
foreach (var handler in _handlers) {
|
||||
handler.Process(context);
|
||||
}
|
||||
|
@@ -2,18 +2,18 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Aspects;
|
||||
using Orchard.Core.Scheduling.Models;
|
||||
using Orchard.Core.Scheduling.Records;
|
||||
using Orchard.Data;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Tasks.Scheduling;
|
||||
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);
|
||||
}
|
||||
|
||||
[UsedImplicitly]
|
||||
public class ScheduledTaskManager : IScheduledTaskManager {
|
||||
private readonly IRepository<ScheduledTaskRecord> _repository;
|
||||
|
||||
@@ -30,21 +30,20 @@ namespace Orchard.Core.Scheduling.Services {
|
||||
|
||||
public void CreateTask(string action, DateTime scheduledUtc, ContentItem contentItem) {
|
||||
var taskRecord = new ScheduledTaskRecord {
|
||||
Action = action,
|
||||
TaskType = action,
|
||||
ScheduledUtc = scheduledUtc,
|
||||
};
|
||||
if (contentItem != null) {
|
||||
var part = contentItem.Get<ContentPart<ScheduledAspectRecord>>();
|
||||
if (part != null) {
|
||||
taskRecord.ScheduledAspectRecord = part.Record;
|
||||
}
|
||||
taskRecord.ContentItemVersionRecord = contentItem.VersionRecord;
|
||||
}
|
||||
_repository.Create(taskRecord);
|
||||
}
|
||||
|
||||
public IEnumerable<ScheduledTaskRecord> GetTasks(ContentItem contentItem) {
|
||||
public IEnumerable<IScheduledTask> GetTasks(ContentItem contentItem) {
|
||||
return _repository
|
||||
.Fetch(x => x.ScheduledAspectRecord.ContentItemRecord == contentItem.Record)
|
||||
.Fetch(x => x.ContentItemVersionRecord.ContentItemRecord == contentItem.Record)
|
||||
.Select(x => new Task(Services.ContentManager, x))
|
||||
.Cast<IScheduledTask>()
|
||||
.ToReadOnlyCollection();
|
||||
}
|
||||
}
|
||||
|
@@ -1,12 +0,0 @@
|
||||
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; }
|
||||
}
|
||||
}
|
@@ -127,7 +127,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ContentManagement\Aspects\ICommonAspect.cs" />
|
||||
<Compile Include="ContentManagement\Aspects\IScheduledAspect.cs" />
|
||||
<Compile Include="Tasks\Scheduling\IScheduledTask.cs" />
|
||||
<Compile Include="ContentManagement\ContentExtensions.cs" />
|
||||
<Compile Include="ContentManagement\ContentItem.cs" />
|
||||
<Compile Include="ContentManagement\ContentItemMetadata.cs" />
|
||||
@@ -200,6 +200,9 @@
|
||||
<Compile Include="Mvc\Html\SiteServiceExtensions.cs" />
|
||||
<Compile Include="Permissions.cs" />
|
||||
<Compile Include="Tasks\FiniteContainerProvider.cs" />
|
||||
<Compile Include="Tasks\Scheduling\IScheduledTaskHandler.cs" />
|
||||
<Compile Include="Tasks\Scheduling\IScheduledTaskManager.cs" />
|
||||
<Compile Include="Tasks\Scheduling\ScheduledTaskContext.cs" />
|
||||
<Compile Include="Themes\ExtensionManagerExtensions.cs" />
|
||||
<Compile Include="Extensions\Helpers\PathHelpers.cs" />
|
||||
<Compile Include="Extensions\IExtensionManager.cs" />
|
||||
|
9
src/Orchard/Tasks/Scheduling/IScheduledTask.cs
Normal file
9
src/Orchard/Tasks/Scheduling/IScheduledTask.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
namespace Orchard.ContentManagement.Aspects {
|
||||
public interface IScheduledTask {
|
||||
string TaskType { get; }
|
||||
DateTime? ScheduledUtc { get; }
|
||||
ContentItem ContentItem { get; }
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
namespace Orchard.Core.Scheduling {
|
||||
namespace Orchard.Tasks.Scheduling {
|
||||
public interface IScheduledTaskHandler : IDependency {
|
||||
void Process(ScheduledTaskContext context);
|
||||
}
|
11
src/Orchard/Tasks/Scheduling/IScheduledTaskManager.cs
Normal file
11
src/Orchard/Tasks/Scheduling/IScheduledTaskManager.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Aspects;
|
||||
|
||||
namespace Orchard.Tasks.Scheduling {
|
||||
public interface IScheduledTaskManager : IDependency {
|
||||
void CreateTask(string taskType, DateTime scheduledUtc, ContentItem contentItem);
|
||||
IEnumerable<IScheduledTask> GetTasks(ContentItem contentItem);
|
||||
}
|
||||
}
|
7
src/Orchard/Tasks/Scheduling/ScheduledTaskContext.cs
Normal file
7
src/Orchard/Tasks/Scheduling/ScheduledTaskContext.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using Orchard.ContentManagement.Aspects;
|
||||
|
||||
namespace Orchard.Tasks.Scheduling {
|
||||
public class ScheduledTaskContext {
|
||||
public IScheduledTask Task { get; set; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user