mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-22 20:13:50 +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:
@@ -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,5 +0,0 @@
|
||||
namespace Orchard.Core.Scheduling {
|
||||
public interface IScheduledTaskHandler : IDependency {
|
||||
void Process(ScheduledTaskContext context);
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user