Refactor scheduled tasks & publishing cancellation
This commit is contained in:
@@ -805,7 +805,7 @@ namespace Umbraco.Tests.Scheduling
|
||||
HasRun = true;
|
||||
}
|
||||
|
||||
public override Task PerformRunAsync()
|
||||
public override Task PerformRunAsync(CancellationToken token)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
@@ -889,7 +889,7 @@ namespace Umbraco.Tests.Scheduling
|
||||
Thread.Sleep(_runMilliseconds);
|
||||
}
|
||||
|
||||
public override Task PerformRunAsync()
|
||||
public override Task PerformRunAsync(CancellationToken token)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@@ -96,21 +96,6 @@ namespace Umbraco.Web.Scheduling
|
||||
StartUp();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current cancellation token
|
||||
/// </summary>
|
||||
public CancellationToken CurrentCancellationToken
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_tokenSource == null)
|
||||
{
|
||||
throw new InvalidOperationException("The token source has not been created which means the task runner has not been started");
|
||||
}
|
||||
return _tokenSource.Token;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of tasks in the queue.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Web.Caching;
|
||||
@@ -73,7 +74,7 @@ namespace Umbraco.Web.Scheduling
|
||||
}
|
||||
}
|
||||
|
||||
public override Task PerformRunAsync()
|
||||
public override Task PerformRunAsync(CancellationToken token)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ namespace Umbraco.Web.Scheduling
|
||||
/// <remarks>Classes inheriting from <c>RecurringTaskBase</c> must implement <c>PerformRun</c>.</remarks>
|
||||
public virtual async Task RunAsync(CancellationToken token)
|
||||
{
|
||||
await PerformRunAsync();
|
||||
await PerformRunAsync(token);
|
||||
Repeat();
|
||||
}
|
||||
|
||||
@@ -95,8 +95,9 @@ namespace Umbraco.Web.Scheduling
|
||||
/// <summary>
|
||||
/// Runs the task asynchronously.
|
||||
/// </summary>
|
||||
/// <param name="token">A cancellation token.</param>
|
||||
/// <returns>A <see cref="Task"/> instance representing the execution of the background task.</returns>
|
||||
public abstract Task PerformRunAsync();
|
||||
public abstract Task PerformRunAsync(CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a new occurence of the recurring task.
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Configuration.UmbracoSettings;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Publishing;
|
||||
using Umbraco.Core.Sync;
|
||||
using Umbraco.Web.Mvc;
|
||||
|
||||
@@ -19,17 +14,15 @@ namespace Umbraco.Web.Scheduling
|
||||
{
|
||||
private readonly ApplicationContext _appContext;
|
||||
private readonly IUmbracoSettingsSection _settings;
|
||||
private readonly Func<CancellationToken> _cancellationToken;
|
||||
|
||||
private static bool _isPublishingRunning;
|
||||
|
||||
public ScheduledPublishing(IBackgroundTaskRunner<ScheduledPublishing> runner, int delayMilliseconds, int periodMilliseconds,
|
||||
ApplicationContext appContext, IUmbracoSettingsSection settings, Func<CancellationToken> cancellationToken)
|
||||
ApplicationContext appContext, IUmbracoSettingsSection settings)
|
||||
: base(runner, delayMilliseconds, periodMilliseconds)
|
||||
{
|
||||
_appContext = appContext;
|
||||
_settings = settings;
|
||||
_cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
private ScheduledPublishing(ScheduledPublishing source)
|
||||
@@ -49,7 +42,7 @@ namespace Umbraco.Web.Scheduling
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override async Task PerformRunAsync()
|
||||
public override async Task PerformRunAsync(CancellationToken token)
|
||||
{
|
||||
|
||||
if (_appContext == null) return;
|
||||
@@ -88,16 +81,6 @@ namespace Umbraco.Web.Scheduling
|
||||
//pass custom the authorization header
|
||||
request.Headers.Authorization = AdminTokenAuthorizeAttribute.GetAuthenticationHeaderValue(_appContext);
|
||||
|
||||
var token = new CancellationToken();
|
||||
try
|
||||
{
|
||||
token = _cancellationToken();
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
//There is no valid token, so we'll continue with the empty one
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = await wc.SendAsync(request, token);
|
||||
|
||||
@@ -1,17 +1,13 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Configuration.UmbracoSettings;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Publishing;
|
||||
using Umbraco.Core.Sync;
|
||||
using Umbraco.Core;
|
||||
|
||||
namespace Umbraco.Web.Scheduling
|
||||
{
|
||||
@@ -23,17 +19,15 @@ namespace Umbraco.Web.Scheduling
|
||||
{
|
||||
private readonly ApplicationContext _appContext;
|
||||
private readonly IUmbracoSettingsSection _settings;
|
||||
private readonly Func<CancellationToken> _cancellationToken;
|
||||
private static readonly Hashtable ScheduledTaskTimes = new Hashtable();
|
||||
private static bool _isPublishingRunning = false;
|
||||
|
||||
public ScheduledTasks(IBackgroundTaskRunner<ScheduledTasks> runner, int delayMilliseconds, int periodMilliseconds,
|
||||
ApplicationContext appContext, IUmbracoSettingsSection settings, Func<CancellationToken> cancellationToken)
|
||||
ApplicationContext appContext, IUmbracoSettingsSection settings)
|
||||
: base(runner, delayMilliseconds, periodMilliseconds)
|
||||
{
|
||||
_appContext = appContext;
|
||||
_settings = settings;
|
||||
_cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public ScheduledTasks(ScheduledTasks source)
|
||||
@@ -48,7 +42,7 @@ namespace Umbraco.Web.Scheduling
|
||||
return new ScheduledTasks(this);
|
||||
}
|
||||
|
||||
private async Task ProcessTasksAsync()
|
||||
private async Task ProcessTasksAsync(CancellationToken token)
|
||||
{
|
||||
var scheduledTasks = _settings.ScheduledTasks.Tasks;
|
||||
foreach (var t in scheduledTasks)
|
||||
@@ -71,14 +65,14 @@ namespace Umbraco.Web.Scheduling
|
||||
|
||||
if (runTask)
|
||||
{
|
||||
bool taskResult = await GetTaskByHttpAync(t.Url);
|
||||
var taskResult = await GetTaskByHttpAync(t.Url, token);
|
||||
if (t.Log)
|
||||
LogHelper.Info<ScheduledTasks>(string.Format("{0} has been called with response: {1}", t.Alias, taskResult));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<bool> GetTaskByHttpAync(string url)
|
||||
private async Task<bool> GetTaskByHttpAync(string url, CancellationToken token)
|
||||
{
|
||||
using (var wc = new HttpClient())
|
||||
{
|
||||
@@ -88,19 +82,10 @@ namespace Umbraco.Web.Scheduling
|
||||
Method = HttpMethod.Get,
|
||||
Content = new StringContent(string.Empty)
|
||||
};
|
||||
|
||||
//TODO: pass custom the authorization header, currently these aren't really secured!
|
||||
//request.Headers.Authorization = AdminTokenAuthorizeAttribute.GetAuthenticationHeaderValue(_appContext);
|
||||
|
||||
var token = new CancellationToken();
|
||||
try
|
||||
{
|
||||
token = _cancellationToken();
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
//There is no valid token, so we'll continue with the empty one
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = await wc.SendAsync(request, token);
|
||||
@@ -119,7 +104,7 @@ namespace Umbraco.Web.Scheduling
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override async Task PerformRunAsync()
|
||||
public override async Task PerformRunAsync(CancellationToken token)
|
||||
{
|
||||
if (ServerEnvironmentHelper.GetStatus(_settings) == CurrentServerEnvironmentStatus.Slave)
|
||||
{
|
||||
@@ -135,7 +120,7 @@ namespace Umbraco.Web.Scheduling
|
||||
|
||||
try
|
||||
{
|
||||
await ProcessTasksAsync();
|
||||
await ProcessTasksAsync(token);
|
||||
}
|
||||
catch (Exception ee)
|
||||
{
|
||||
|
||||
@@ -66,8 +66,8 @@ namespace Umbraco.Web.Scheduling
|
||||
// scheduled publishing/unpublishing
|
||||
// install on all, will only run on non-slaves servers
|
||||
// both are delayed recurring tasks
|
||||
_publishingRunner.Add(new ScheduledPublishing(_publishingRunner, 60000, 60000, applicationContext, settings, () => _publishingRunner.CurrentCancellationToken));
|
||||
_tasksRunner.Add(new ScheduledTasks(_tasksRunner, 60000, 60000, applicationContext, settings, () => _tasksRunner.CurrentCancellationToken));
|
||||
_publishingRunner.Add(new ScheduledPublishing(_publishingRunner, 60000, 60000, applicationContext, settings));
|
||||
_tasksRunner.Add(new ScheduledTasks(_tasksRunner, 60000, 60000, applicationContext, settings));
|
||||
|
||||
// log scrubbing
|
||||
// install & run on all servers
|
||||
|
||||
Reference in New Issue
Block a user